blob: 49f328852667cacdbcb47734243c35773411563f [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"
Matthias Braun88e21312015-06-13 03:42:11 +000031#include "llvm/CodeGen/TargetSchedule.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000036#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000037#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000038#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000041#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000042
David Goodwinaf7451b2009-07-08 16:09:28 +000043using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "arm-instrinfo"
46
Chandler Carruthd174b722014-04-22 02:03:14 +000047#define GET_INSTRINFO_CTOR_DTOR
48#include "ARMGenInstrInfo.inc"
49
David Goodwinaf7451b2009-07-08 16:09:28 +000050static cl::opt<bool>
51EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
52 cl::desc("Enable ARM 2-addr to 3-addr conv"));
53
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000054static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000055WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000056 cl::desc("Widen ARM vmovs to vmovd when possible"));
57
Bob Wilsone8a549c2012-09-29 21:43:49 +000058static cl::opt<unsigned>
59SwiftPartialUpdateClearance("swift-partial-update-clearance",
60 cl::Hidden, cl::init(12),
61 cl::desc("Clearance before partial register updates"));
62
Evan Cheng62c7b5b2010-12-05 22:04:16 +000063/// ARM_MLxEntry - Record information about MLA / MLS instructions.
64struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000065 uint16_t MLxOpc; // MLA / MLS opcode
66 uint16_t MulOpc; // Expanded multiplication opcode
67 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000068 bool NegAcc; // True if the acc is negated before the add / sub.
69 bool HasLane; // True if instruction has an extra "lane" operand.
70};
71
72static const ARM_MLxEntry ARM_MLxTable[] = {
73 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
74 // fp scalar ops
75 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
76 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
77 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
78 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000079 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
81 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
82 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
83
84 // fp SIMD ops
85 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
86 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
87 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
88 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
89 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
90 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
91 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
92 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
93};
94
Anton Korobeynikov14635da2009-11-02 00:10:38 +000095ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000096 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000097 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000098 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
99 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000100 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000101 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
102 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
103 }
104}
105
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000106// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
107// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000108ScheduleHazardRecognizer *
109ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
110 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000111 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000112 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000113 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000114 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
115 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000116 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000117}
118
119ScheduleHazardRecognizer *ARMBaseInstrInfo::
120CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
121 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000122 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000123 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000124 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000125}
126
127MachineInstr *
128ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
129 MachineBasicBlock::iterator &MBBI,
130 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000131 // FIXME: Thumb2 support.
132
David Goodwinaf7451b2009-07-08 16:09:28 +0000133 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000134 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000135
136 MachineInstr *MI = MBBI;
137 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000138 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000139 bool isPre = false;
140 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000142 case ARMII::IndexModePre:
143 isPre = true;
144 break;
145 case ARMII::IndexModePost:
146 break;
147 }
148
149 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
150 // operation.
151 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
152 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000154
Craig Topper062a2ba2014-04-25 05:30:21 +0000155 MachineInstr *UpdateMI = nullptr;
156 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000157 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000158 const MCInstrDesc &MCID = MI->getDesc();
159 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000160 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000161 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
162 const MachineOperand &Base = MI->getOperand(2);
163 const MachineOperand &Offset = MI->getOperand(NumOps-3);
164 unsigned WBReg = WB.getReg();
165 unsigned BaseReg = Base.getReg();
166 unsigned OffReg = Offset.getReg();
167 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
168 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
169 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000170 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000171 case ARMII::AddrMode2: {
172 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
173 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
174 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000175 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 // Can't encode it in a so_imm operand. This transformation will
177 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000178 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000179 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000180 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000181 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000182 .addImm(Pred).addReg(0).addReg(0);
183 } else if (Amt != 0) {
184 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
185 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
186 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000187 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000188 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
189 .addImm(Pred).addReg(0).addReg(0);
190 } else
191 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000192 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000193 .addReg(BaseReg).addReg(OffReg)
194 .addImm(Pred).addReg(0).addReg(0);
195 break;
196 }
197 case ARMII::AddrMode3 : {
198 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
199 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
200 if (OffReg == 0)
201 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
202 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000203 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000204 .addReg(BaseReg).addImm(Amt)
205 .addImm(Pred).addReg(0).addReg(0);
206 else
207 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000208 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000209 .addReg(BaseReg).addReg(OffReg)
210 .addImm(Pred).addReg(0).addReg(0);
211 break;
212 }
213 }
214
215 std::vector<MachineInstr*> NewMIs;
216 if (isPre) {
217 if (isLoad)
218 MemMI = BuildMI(MF, MI->getDebugLoc(),
219 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000220 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000221 else
222 MemMI = BuildMI(MF, MI->getDebugLoc(),
223 get(MemOpc)).addReg(MI->getOperand(1).getReg())
224 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
225 NewMIs.push_back(MemMI);
226 NewMIs.push_back(UpdateMI);
227 } else {
228 if (isLoad)
229 MemMI = BuildMI(MF, MI->getDebugLoc(),
230 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000231 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000232 else
233 MemMI = BuildMI(MF, MI->getDebugLoc(),
234 get(MemOpc)).addReg(MI->getOperand(1).getReg())
235 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
236 if (WB.isDead())
237 UpdateMI->getOperand(0).setIsDead();
238 NewMIs.push_back(UpdateMI);
239 NewMIs.push_back(MemMI);
240 }
241
242 // Transfer LiveVariables states, kill / dead info.
243 if (LV) {
244 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
245 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000246 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000247 unsigned Reg = MO.getReg();
248
249 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
250 if (MO.isDef()) {
251 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
252 if (MO.isDead())
253 LV->addVirtualRegisterDead(Reg, NewMI);
254 }
255 if (MO.isUse() && MO.isKill()) {
256 for (unsigned j = 0; j < 2; ++j) {
257 // Look at the two new MI's in reverse order.
258 MachineInstr *NewMI = NewMIs[j];
259 if (!NewMI->readsRegister(Reg))
260 continue;
261 LV->addVirtualRegisterKilled(Reg, NewMI);
262 if (VI.removeKill(MI))
263 VI.Kills.push_back(NewMI);
264 break;
265 }
266 }
267 }
268 }
269 }
270
271 MFI->insert(MBBI, NewMIs[1]);
272 MFI->insert(MBBI, NewMIs[0]);
273 return NewMIs[0];
274}
275
276// Branch analysis.
277bool
278ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
279 MachineBasicBlock *&FBB,
280 SmallVectorImpl<MachineOperand> &Cond,
281 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000282 TBB = nullptr;
283 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000284
David Goodwinaf7451b2009-07-08 16:09:28 +0000285 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000286 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000287 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000288 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000289
290 // Walk backwards from the end of the basic block until the branch is
291 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000292 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000293
294 // Flag to be raised on unanalyzeable instructions. This is useful in cases
295 // where we want to clean up on the end of the basic block before we bail
296 // out.
297 bool CantAnalyze = false;
298
299 // Skip over DEBUG values and predicated nonterminators.
300 while (I->isDebugValue() || !I->isTerminator()) {
301 if (I == MBB.begin())
302 return false;
303 --I;
304 }
305
306 if (isIndirectBranchOpcode(I->getOpcode()) ||
307 isJumpTableBranchOpcode(I->getOpcode())) {
308 // Indirect branches and jump tables can't be analyzed, but we still want
309 // to clean up any instructions at the tail of the basic block.
310 CantAnalyze = true;
311 } else if (isUncondBranchOpcode(I->getOpcode())) {
312 TBB = I->getOperand(0).getMBB();
313 } else if (isCondBranchOpcode(I->getOpcode())) {
314 // Bail out if we encounter multiple conditional branches.
315 if (!Cond.empty())
316 return true;
317
318 assert(!FBB && "FBB should have been null.");
319 FBB = TBB;
320 TBB = I->getOperand(0).getMBB();
321 Cond.push_back(I->getOperand(1));
322 Cond.push_back(I->getOperand(2));
323 } else if (I->isReturn()) {
324 // Returns can't be analyzed, but we should run cleanup.
325 CantAnalyze = !isPredicated(I);
326 } else {
327 // We encountered other unrecognized terminator. Bail out immediately.
328 return true;
329 }
330
331 // Cleanup code - to be run for unpredicated unconditional branches and
332 // returns.
333 if (!isPredicated(I) &&
334 (isUncondBranchOpcode(I->getOpcode()) ||
335 isIndirectBranchOpcode(I->getOpcode()) ||
336 isJumpTableBranchOpcode(I->getOpcode()) ||
337 I->isReturn())) {
338 // Forget any previous condition branch information - it no longer applies.
339 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000340 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000341
342 // If we can modify the function, delete everything below this
343 // unconditional branch.
344 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000345 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000346 while (DI != MBB.end()) {
347 MachineInstr *InstToDelete = DI;
348 ++DI;
349 InstToDelete->eraseFromParent();
350 }
351 }
352 }
353
354 if (CantAnalyze)
355 return true;
356
Dale Johannesen4244d122010-04-02 01:38:09 +0000357 if (I == MBB.begin())
358 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000359
Dale Johannesen4244d122010-04-02 01:38:09 +0000360 --I;
361 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000362
Lang Hames24864fe2013-07-19 23:52:47 +0000363 // We made it past the terminators without bailing out - we must have
364 // analyzed this branch successfully.
365 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000366}
367
368
369unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000370 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
371 if (I == MBB.end())
372 return 0;
373
Evan Cheng056c6692009-07-27 18:20:05 +0000374 if (!isUncondBranchOpcode(I->getOpcode()) &&
375 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000376 return 0;
377
378 // Remove the branch.
379 I->eraseFromParent();
380
381 I = MBB.end();
382
383 if (I == MBB.begin()) return 1;
384 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000385 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000386 return 1;
387
388 // Remove the branch.
389 I->eraseFromParent();
390 return 2;
391}
392
393unsigned
394ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000395 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000396 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000397 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000398 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
399 int BOpc = !AFI->isThumbFunction()
400 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
401 int BccOpc = !AFI->isThumbFunction()
402 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000403 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000404
David Goodwinaf7451b2009-07-08 16:09:28 +0000405 // Shouldn't be a fall through.
406 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
407 assert((Cond.size() == 2 || Cond.size() == 0) &&
408 "ARM branch conditions have two components!");
409
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000410 // For conditional branches, we use addOperand to preserve CPSR flags.
411
Craig Topper062a2ba2014-04-25 05:30:21 +0000412 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000413 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000414 if (isThumb)
415 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
416 else
417 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000418 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000419 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000420 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000421 return 1;
422 }
423
424 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000425 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000426 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000427 if (isThumb)
428 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
429 else
430 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000431 return 2;
432}
433
434bool ARMBaseInstrInfo::
435ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
436 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
437 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
438 return false;
439}
440
Evan Cheng7fae11b2011-12-14 02:11:42 +0000441bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
442 if (MI->isBundle()) {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000443 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
444 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000445 while (++I != E && I->isInsideBundle()) {
446 int PIdx = I->findFirstPredOperandIdx();
447 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
448 return true;
449 }
450 return false;
451 }
452
453 int PIdx = MI->findFirstPredOperandIdx();
454 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
455}
456
David Goodwinaf7451b2009-07-08 16:09:28 +0000457bool ARMBaseInstrInfo::
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000458PredicateInstruction(MachineInstr *MI, ArrayRef<MachineOperand> Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000459 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000460 if (isUncondBranchOpcode(Opc)) {
461 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000462 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
463 .addImm(Pred[0].getImm())
464 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000465 return true;
466 }
467
468 int PIdx = MI->findFirstPredOperandIdx();
469 if (PIdx != -1) {
470 MachineOperand &PMO = MI->getOperand(PIdx);
471 PMO.setImm(Pred[0].getImm());
472 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
473 return true;
474 }
475 return false;
476}
477
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000478bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
479 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000480 if (Pred1.size() > 2 || Pred2.size() > 2)
481 return false;
482
483 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
484 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
485 if (CC1 == CC2)
486 return true;
487
488 switch (CC1) {
489 default:
490 return false;
491 case ARMCC::AL:
492 return true;
493 case ARMCC::HS:
494 return CC2 == ARMCC::HI;
495 case ARMCC::LS:
496 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
497 case ARMCC::GE:
498 return CC2 == ARMCC::GT;
499 case ARMCC::LE:
500 return CC2 == ARMCC::LT;
501 }
502}
503
504bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
505 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000506 bool Found = false;
507 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
508 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000509 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
510 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000511 Pred.push_back(MO);
512 Found = true;
513 }
514 }
515
516 return Found;
517}
518
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000519static bool isCPSRDefined(const MachineInstr *MI) {
520 for (const auto &MO : MI->operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000521 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000522 return true;
523 return false;
524}
525
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000526static bool isEligibleForITBlock(const MachineInstr *MI) {
527 switch (MI->getOpcode()) {
528 default: return true;
529 case ARM::tADC: // ADC (register) T1
530 case ARM::tADDi3: // ADD (immediate) T1
531 case ARM::tADDi8: // ADD (immediate) T2
532 case ARM::tADDrr: // ADD (register) T1
533 case ARM::tAND: // AND (register) T1
534 case ARM::tASRri: // ASR (immediate) T1
535 case ARM::tASRrr: // ASR (register) T1
536 case ARM::tBIC: // BIC (register) T1
537 case ARM::tEOR: // EOR (register) T1
538 case ARM::tLSLri: // LSL (immediate) T1
539 case ARM::tLSLrr: // LSL (register) T1
540 case ARM::tLSRri: // LSR (immediate) T1
541 case ARM::tLSRrr: // LSR (register) T1
542 case ARM::tMUL: // MUL T1
543 case ARM::tMVN: // MVN (register) T1
544 case ARM::tORR: // ORR (register) T1
545 case ARM::tROR: // ROR (register) T1
546 case ARM::tRSB: // RSB (immediate) T1
547 case ARM::tSBC: // SBC (register) T1
548 case ARM::tSUBi3: // SUB (immediate) T1
549 case ARM::tSUBi8: // SUB (immediate) T2
550 case ARM::tSUBrr: // SUB (register) T1
551 return !isCPSRDefined(MI);
552 }
553}
554
Evan Chenga33fc862009-11-21 06:21:52 +0000555/// isPredicable - Return true if the specified instruction can be predicated.
556/// By default, this returns true for every instruction with a
557/// PredicateOperand.
558bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000559 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000560 return false;
561
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000562 if (!isEligibleForITBlock(MI))
563 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000564
Joey Goulya5153cb2013-09-09 14:21:49 +0000565 ARMFunctionInfo *AFI =
566 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
567
568 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000569 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000570 return isV8EligibleForIT(MI);
571 } else { // non-Thumb
572 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
573 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000574 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000575
Evan Chenga33fc862009-11-21 06:21:52 +0000576 return true;
577}
David Goodwinaf7451b2009-07-08 16:09:28 +0000578
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000579namespace llvm {
580template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000581 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
582 const MachineOperand &MO = MI->getOperand(i);
583 if (!MO.isReg() || MO.isUndef() || MO.isUse())
584 continue;
585 if (MO.getReg() != ARM::CPSR)
586 continue;
587 if (!MO.isDead())
588 return false;
589 }
590 // all definitions of CPSR are dead
591 return true;
592}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000593}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000594
David Goodwinaf7451b2009-07-08 16:09:28 +0000595/// GetInstSize - Return the size of the specified MachineInstr.
596///
597unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
598 const MachineBasicBlock &MBB = *MI->getParent();
599 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000600 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000601
Evan Cheng6cc775f2011-06-28 19:10:37 +0000602 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000603 if (MCID.getSize())
604 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000605
David Blaikie46a9f012012-01-20 21:51:11 +0000606 // If this machine instr is an inline asm, measure it.
607 if (MI->getOpcode() == ARM::INLINEASM)
608 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000609 unsigned Opc = MI->getOpcode();
610 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000611 default:
612 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000613 return 0;
614 case TargetOpcode::BUNDLE:
615 return getInstBundleLength(MI);
616 case ARM::MOVi16_ga_pcrel:
617 case ARM::MOVTi16_ga_pcrel:
618 case ARM::t2MOVi16_ga_pcrel:
619 case ARM::t2MOVTi16_ga_pcrel:
620 return 4;
621 case ARM::MOVi32imm:
622 case ARM::t2MOVi32imm:
623 return 8;
624 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000625 case ARM::JUMPTABLE_INSTS:
626 case ARM::JUMPTABLE_ADDRS:
627 case ARM::JUMPTABLE_TBB:
628 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000629 // If this machine instr is a constant pool entry, its size is recorded as
630 // operand #2.
631 return MI->getOperand(2).getImm();
632 case ARM::Int_eh_sjlj_longjmp:
633 return 16;
634 case ARM::tInt_eh_sjlj_longjmp:
635 return 10;
636 case ARM::Int_eh_sjlj_setjmp:
637 case ARM::Int_eh_sjlj_setjmp_nofp:
638 return 20;
639 case ARM::tInt_eh_sjlj_setjmp:
640 case ARM::t2Int_eh_sjlj_setjmp:
641 case ARM::t2Int_eh_sjlj_setjmp_nofp:
642 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000643 case ARM::SPACE:
644 return MI->getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000645 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000646}
647
Evan Cheng7fae11b2011-12-14 02:11:42 +0000648unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
649 unsigned Size = 0;
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000650 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
651 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000652 while (++I != E && I->isInsideBundle()) {
653 assert(!I->isBundle() && "No nested bundle!");
654 Size += GetInstSizeInBytes(&*I);
655 }
656 return Size;
657}
658
Tim Northover5d72c5d2014-10-01 19:21:03 +0000659void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
660 MachineBasicBlock::iterator I,
661 unsigned DestReg, bool KillSrc,
662 const ARMSubtarget &Subtarget) const {
663 unsigned Opc = Subtarget.isThumb()
664 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
665 : ARM::MRS;
666
667 MachineInstrBuilder MIB =
668 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
669
670 // There is only 1 A/R class MRS instruction, and it always refers to
671 // APSR. However, there are lots of other possibilities on M-class cores.
672 if (Subtarget.isMClass())
673 MIB.addImm(0x800);
674
675 AddDefaultPred(MIB);
676
677 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
678}
679
680void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
681 MachineBasicBlock::iterator I,
682 unsigned SrcReg, bool KillSrc,
683 const ARMSubtarget &Subtarget) const {
684 unsigned Opc = Subtarget.isThumb()
685 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
686 : ARM::MSR;
687
688 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
689
690 if (Subtarget.isMClass())
691 MIB.addImm(0x800);
692 else
693 MIB.addImm(8);
694
695 MIB.addReg(SrcReg, getKillRegState(KillSrc));
696
697 AddDefaultPred(MIB);
698
699 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
700}
701
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000702void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
703 MachineBasicBlock::iterator I, DebugLoc DL,
704 unsigned DestReg, unsigned SrcReg,
705 bool KillSrc) const {
706 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000707 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000708
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000709 if (GPRDest && GPRSrc) {
710 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000711 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000712 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000713 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000714
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000715 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000716 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000717
Chad Rosierbe762512011-08-20 00:17:25 +0000718 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000719 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000720 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000721 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000722 Opc = ARM::VMOVRS;
723 else if (SPRDest && GPRSrc)
724 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000725 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000726 Opc = ARM::VMOVD;
727 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000728 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000729
Chad Rosierbe762512011-08-20 00:17:25 +0000730 if (Opc) {
731 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000732 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000733 if (Opc == ARM::VORRq)
734 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000735 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000736 return;
737 }
738
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000739 // Handle register classes that require multiple instructions.
740 unsigned BeginIdx = 0;
741 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000742 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000743
744 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000745 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
746 Opc = ARM::VORRq;
747 BeginIdx = ARM::qsub_0;
748 SubRegs = 2;
749 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
750 Opc = ARM::VORRq;
751 BeginIdx = ARM::qsub_0;
752 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000753 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000754 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
755 Opc = ARM::VMOVD;
756 BeginIdx = ARM::dsub_0;
757 SubRegs = 2;
758 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
759 Opc = ARM::VMOVD;
760 BeginIdx = ARM::dsub_0;
761 SubRegs = 3;
762 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
763 Opc = ARM::VMOVD;
764 BeginIdx = ARM::dsub_0;
765 SubRegs = 4;
766 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000767 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000768 BeginIdx = ARM::gsub_0;
769 SubRegs = 2;
770 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
771 Opc = ARM::VMOVD;
772 BeginIdx = ARM::dsub_0;
773 SubRegs = 2;
774 Spacing = 2;
775 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
776 Opc = ARM::VMOVD;
777 BeginIdx = ARM::dsub_0;
778 SubRegs = 3;
779 Spacing = 2;
780 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
781 Opc = ARM::VMOVD;
782 BeginIdx = ARM::dsub_0;
783 SubRegs = 4;
784 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000785 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
786 Opc = ARM::VMOVS;
787 BeginIdx = ARM::ssub_0;
788 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000789 } else if (SrcReg == ARM::CPSR) {
790 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
791 return;
792 } else if (DestReg == ARM::CPSR) {
793 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
794 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000795 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000796
Andrew Trickb57e2252012-08-29 04:41:37 +0000797 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000798
Andrew Trick4cc69492012-08-29 01:58:52 +0000799 const TargetRegisterInfo *TRI = &getRegisterInfo();
800 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000801
802 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
803 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000804 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000805 Spacing = -Spacing;
806 }
807#ifndef NDEBUG
808 SmallSet<unsigned, 4> DstRegs;
809#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000810 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000811 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
812 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000813 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000814#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000815 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000816 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000817#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000818 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000819 // VORR takes two source operands.
820 if (Opc == ARM::VORRq)
821 Mov.addReg(Src);
822 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000823 // MOVr can set CC.
824 if (Opc == ARM::MOVr)
825 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000826 }
827 // Add implicit super-register defs and kills to the last instruction.
828 Mov->addRegisterDefined(DestReg, TRI);
829 if (KillSrc)
830 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000831}
832
Tim Northover798697d2013-04-21 11:57:07 +0000833const MachineInstrBuilder &
834ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
835 unsigned SubIdx, unsigned State,
836 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000837 if (!SubIdx)
838 return MIB.addReg(Reg, State);
839
840 if (TargetRegisterInfo::isPhysicalRegister(Reg))
841 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
842 return MIB.addReg(Reg, State, SubIdx);
843}
844
David Goodwinaf7451b2009-07-08 16:09:28 +0000845void ARMBaseInstrInfo::
846storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
847 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000848 const TargetRegisterClass *RC,
849 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000850 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000851 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000852 MachineFunction &MF = *MBB.getParent();
853 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000854 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000855
Alex Lorenze40c8a22015-08-11 23:09:45 +0000856 MachineMemOperand *MMO = MF.getMachineMemOperand(
857 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
858 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000859
Owen Anderson732f82c2011-08-10 17:21:20 +0000860 switch (RC->getSize()) {
861 case 4:
862 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
863 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000864 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000865 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000866 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
867 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000868 .addReg(SrcReg, getKillRegState(isKill))
869 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000870 } else
871 llvm_unreachable("Unknown reg class!");
872 break;
873 case 8:
874 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
875 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000876 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000877 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000878 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000879 if (Subtarget.hasV5TEOps()) {
880 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
881 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
882 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
883 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
884
885 AddDefaultPred(MIB);
886 } else {
887 // Fallback to STM instruction, which has existed since the dawn of
888 // time.
889 MachineInstrBuilder MIB =
890 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
891 .addFrameIndex(FI).addMemOperand(MMO));
892 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
893 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
894 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000895 } else
896 llvm_unreachable("Unknown reg class!");
897 break;
898 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000899 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000900 // Use aligned spills if the stack can be realigned.
901 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000902 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000903 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000904 .addReg(SrcReg, getKillRegState(isKill))
905 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000906 } else {
907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000908 .addReg(SrcReg, getKillRegState(isKill))
909 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000910 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000911 }
912 } else
913 llvm_unreachable("Unknown reg class!");
914 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000915 case 24:
916 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
917 // Use aligned spills if the stack can be realigned.
918 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
919 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
920 .addFrameIndex(FI).addImm(16)
921 .addReg(SrcReg, getKillRegState(isKill))
922 .addMemOperand(MMO));
923 } else {
924 MachineInstrBuilder MIB =
925 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
926 .addFrameIndex(FI))
927 .addMemOperand(MMO);
928 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
929 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
930 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
931 }
932 } else
933 llvm_unreachable("Unknown reg class!");
934 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000935 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000936 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000937 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
938 // FIXME: It's possible to only store part of the QQ register if the
939 // spilled def has a sub-register index.
940 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000941 .addFrameIndex(FI).addImm(16)
942 .addReg(SrcReg, getKillRegState(isKill))
943 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000944 } else {
945 MachineInstrBuilder MIB =
946 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000947 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000948 .addMemOperand(MMO);
949 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
950 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
951 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
952 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
953 }
954 } else
955 llvm_unreachable("Unknown reg class!");
956 break;
957 case 64:
958 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
959 MachineInstrBuilder MIB =
960 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
961 .addFrameIndex(FI))
962 .addMemOperand(MMO);
963 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
964 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
965 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
966 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
967 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
969 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
970 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
971 } else
972 llvm_unreachable("Unknown reg class!");
973 break;
974 default:
975 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000976 }
977}
978
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000979unsigned
980ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
981 int &FrameIndex) const {
982 switch (MI->getOpcode()) {
983 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000984 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000985 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
986 if (MI->getOperand(1).isFI() &&
987 MI->getOperand(2).isReg() &&
988 MI->getOperand(3).isImm() &&
989 MI->getOperand(2).getReg() == 0 &&
990 MI->getOperand(3).getImm() == 0) {
991 FrameIndex = MI->getOperand(1).getIndex();
992 return MI->getOperand(0).getReg();
993 }
994 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000995 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000996 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000997 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000998 case ARM::VSTRD:
999 case ARM::VSTRS:
1000 if (MI->getOperand(1).isFI() &&
1001 MI->getOperand(2).isImm() &&
1002 MI->getOperand(2).getImm() == 0) {
1003 FrameIndex = MI->getOperand(1).getIndex();
1004 return MI->getOperand(0).getReg();
1005 }
1006 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001007 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001008 case ARM::VST1d64TPseudo:
1009 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001010 if (MI->getOperand(0).isFI() &&
1011 MI->getOperand(2).getSubReg() == 0) {
1012 FrameIndex = MI->getOperand(0).getIndex();
1013 return MI->getOperand(2).getReg();
1014 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001015 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001016 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001017 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001018 MI->getOperand(0).getSubReg() == 0) {
1019 FrameIndex = MI->getOperand(1).getIndex();
1020 return MI->getOperand(0).getReg();
1021 }
1022 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001023 }
1024
1025 return 0;
1026}
1027
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001028unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1029 int &FrameIndex) const {
1030 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001031 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001032}
1033
David Goodwinaf7451b2009-07-08 16:09:28 +00001034void ARMBaseInstrInfo::
1035loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1036 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001037 const TargetRegisterClass *RC,
1038 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001039 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001040 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001041 MachineFunction &MF = *MBB.getParent();
1042 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001043 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001044 MachineMemOperand *MMO = MF.getMachineMemOperand(
1045 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1046 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001047
Owen Anderson732f82c2011-08-10 17:21:20 +00001048 switch (RC->getSize()) {
1049 case 4:
1050 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1051 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1052 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001053
Owen Anderson732f82c2011-08-10 17:21:20 +00001054 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1055 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001056 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001057 } else
1058 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001059 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001060 case 8:
1061 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1062 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001063 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001064 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001065 MachineInstrBuilder MIB;
1066
1067 if (Subtarget.hasV5TEOps()) {
1068 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1069 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1070 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1071 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1072
1073 AddDefaultPred(MIB);
1074 } else {
1075 // Fallback to LDM instruction, which has existed since the dawn of
1076 // time.
1077 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1078 .addFrameIndex(FI).addMemOperand(MMO));
1079 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1080 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1081 }
1082
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001083 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1084 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001085 } else
1086 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001087 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001088 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001089 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001090 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001091 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001092 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001093 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001094 } else {
1095 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1096 .addFrameIndex(FI)
1097 .addMemOperand(MMO));
1098 }
1099 } else
1100 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001101 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001102 case 24:
1103 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1104 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1105 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1106 .addFrameIndex(FI).addImm(16)
1107 .addMemOperand(MMO));
1108 } else {
1109 MachineInstrBuilder MIB =
1110 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1111 .addFrameIndex(FI)
1112 .addMemOperand(MMO));
1113 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1114 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1115 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1116 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1117 MIB.addReg(DestReg, RegState::ImplicitDefine);
1118 }
1119 } else
1120 llvm_unreachable("Unknown reg class!");
1121 break;
1122 case 32:
1123 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001124 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1125 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001126 .addFrameIndex(FI).addImm(16)
1127 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001128 } else {
1129 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001130 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1131 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001132 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001133 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1134 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1135 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1136 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001137 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1138 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001139 }
1140 } else
1141 llvm_unreachable("Unknown reg class!");
1142 break;
1143 case 64:
1144 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1145 MachineInstrBuilder MIB =
1146 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1147 .addFrameIndex(FI))
1148 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001149 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1150 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1151 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1152 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1153 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1154 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1155 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1156 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001157 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1158 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001159 } else
1160 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001161 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001162 default:
1163 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001164 }
1165}
1166
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001167unsigned
1168ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1169 int &FrameIndex) const {
1170 switch (MI->getOpcode()) {
1171 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001172 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001173 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1174 if (MI->getOperand(1).isFI() &&
1175 MI->getOperand(2).isReg() &&
1176 MI->getOperand(3).isImm() &&
1177 MI->getOperand(2).getReg() == 0 &&
1178 MI->getOperand(3).getImm() == 0) {
1179 FrameIndex = MI->getOperand(1).getIndex();
1180 return MI->getOperand(0).getReg();
1181 }
1182 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001183 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001184 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001185 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001186 case ARM::VLDRD:
1187 case ARM::VLDRS:
1188 if (MI->getOperand(1).isFI() &&
1189 MI->getOperand(2).isImm() &&
1190 MI->getOperand(2).getImm() == 0) {
1191 FrameIndex = MI->getOperand(1).getIndex();
1192 return MI->getOperand(0).getReg();
1193 }
1194 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001195 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001196 case ARM::VLD1d64TPseudo:
1197 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001198 if (MI->getOperand(1).isFI() &&
1199 MI->getOperand(0).getSubReg() == 0) {
1200 FrameIndex = MI->getOperand(1).getIndex();
1201 return MI->getOperand(0).getReg();
1202 }
1203 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001204 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001205 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001206 MI->getOperand(0).getSubReg() == 0) {
1207 FrameIndex = MI->getOperand(1).getIndex();
1208 return MI->getOperand(0).getReg();
1209 }
1210 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001211 }
1212
1213 return 0;
1214}
1215
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001216unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1217 int &FrameIndex) const {
1218 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001219 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001220}
1221
Scott Douglass953f9082015-10-05 14:49:54 +00001222/// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1223/// depending on whether the result is used.
1224void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MBBI) const {
1225 bool isThumb1 = Subtarget.isThumb1Only();
1226 bool isThumb2 = Subtarget.isThumb2();
1227 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1228
1229 MachineInstr *MI = MBBI;
1230 DebugLoc dl = MI->getDebugLoc();
1231 MachineBasicBlock *BB = MI->getParent();
1232
1233 MachineInstrBuilder LDM, STM;
1234 if (isThumb1 || !MI->getOperand(1).isDead()) {
1235 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1236 : isThumb1 ? ARM::tLDMIA_UPD
1237 : ARM::LDMIA_UPD))
1238 .addOperand(MI->getOperand(1));
1239 } else {
1240 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1241 }
1242
1243 if (isThumb1 || !MI->getOperand(0).isDead()) {
1244 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1245 : isThumb1 ? ARM::tSTMIA_UPD
1246 : ARM::STMIA_UPD))
1247 .addOperand(MI->getOperand(0));
1248 } else {
1249 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1250 }
1251
1252 AddDefaultPred(LDM.addOperand(MI->getOperand(3)));
1253 AddDefaultPred(STM.addOperand(MI->getOperand(2)));
1254
1255 // Sort the scratch registers into ascending order.
1256 const TargetRegisterInfo &TRI = getRegisterInfo();
1257 llvm::SmallVector<unsigned, 6> ScratchRegs;
1258 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1259 ScratchRegs.push_back(MI->getOperand(I).getReg());
1260 std::sort(ScratchRegs.begin(), ScratchRegs.end(),
1261 [&TRI](const unsigned &Reg1,
1262 const unsigned &Reg2) -> bool {
1263 return TRI.getEncodingValue(Reg1) <
1264 TRI.getEncodingValue(Reg2);
1265 });
1266
1267 for (const auto &Reg : ScratchRegs) {
1268 LDM.addReg(Reg, RegState::Define);
1269 STM.addReg(Reg, RegState::Kill);
1270 }
1271
1272 BB->erase(MBBI);
1273}
1274
1275
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001276bool
1277ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1278 MachineFunction &MF = *MI->getParent()->getParent();
1279 Reloc::Model RM = MF.getTarget().getRelocationModel();
1280
1281 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001282 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001283 "LOAD_STACK_GUARD currently supported only for MachO.");
1284 expandLoadStackGuard(MI, RM);
1285 MI->getParent()->erase(MI);
1286 return true;
1287 }
1288
Scott Douglass953f9082015-10-05 14:49:54 +00001289 if (MI->getOpcode() == ARM::MEMCPY) {
1290 expandMEMCPY(MI);
1291 return true;
1292 }
1293
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001294 // This hook gets to expand COPY instructions before they become
1295 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1296 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1297 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001298 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1299 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001300 return false;
1301
1302 // Look for a copy between even S-registers. That is where we keep floats
1303 // when using NEON v2f32 instructions for f32 arithmetic.
1304 unsigned DstRegS = MI->getOperand(0).getReg();
1305 unsigned SrcRegS = MI->getOperand(1).getReg();
1306 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1307 return false;
1308
1309 const TargetRegisterInfo *TRI = &getRegisterInfo();
1310 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1311 &ARM::DPRRegClass);
1312 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1313 &ARM::DPRRegClass);
1314 if (!DstRegD || !SrcRegD)
1315 return false;
1316
1317 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1318 // legal if the COPY already defines the full DstRegD, and it isn't a
1319 // sub-register insertion.
1320 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1321 return false;
1322
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001323 // A dead copy shouldn't show up here, but reject it just in case.
1324 if (MI->getOperand(0).isDead())
1325 return false;
1326
1327 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001328 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001329 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001330
1331 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1332 // or some other super-register.
1333 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1334 if (ImpDefIdx != -1)
1335 MI->RemoveOperand(ImpDefIdx);
1336
1337 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001338 MI->setDesc(get(ARM::VMOVD));
1339 MI->getOperand(0).setReg(DstRegD);
1340 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001341 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001342
1343 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1344 // register scavenger and machine verifier, so we need to indicate that we
1345 // are reading an undefined value from SrcRegD, but a proper value from
1346 // SrcRegS.
1347 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001348 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001349
1350 // SrcRegD may actually contain an unrelated value in the ssub_1
1351 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1352 if (MI->getOperand(1).isKill()) {
1353 MI->getOperand(1).setIsKill(false);
1354 MI->addRegisterKilled(SrcRegS, TRI, true);
1355 }
1356
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001357 DEBUG(dbgs() << "replaced by: " << *MI);
1358 return true;
1359}
1360
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001361/// Create a copy of a const pool value. Update CPI to the new index and return
1362/// the label UID.
1363static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1364 MachineConstantPool *MCP = MF.getConstantPool();
1365 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1366
1367 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1368 assert(MCPE.isMachineConstantPoolEntry() &&
1369 "Expecting a machine constantpool entry!");
1370 ARMConstantPoolValue *ACPV =
1371 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1372
Evan Chengdfce83c2011-01-17 08:03:18 +00001373 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001374 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001375
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001376 // FIXME: The below assumes PIC relocation model and that the function
1377 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1378 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1379 // instructions, so that's probably OK, but is PIC always correct when
1380 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001381 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001382 NewCPV = ARMConstantPoolConstant::Create(
1383 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1384 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001385 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001386 NewCPV = ARMConstantPoolSymbol::
1387 Create(MF.getFunction()->getContext(),
1388 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001389 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001390 NewCPV = ARMConstantPoolConstant::
1391 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1392 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001393 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001394 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1395 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001396 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001397 NewCPV = ARMConstantPoolMBB::
1398 Create(MF.getFunction()->getContext(),
1399 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001400 else
1401 llvm_unreachable("Unexpected ARM constantpool value type!!");
1402 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1403 return PCLabelId;
1404}
1405
Evan Chengfe864422009-11-08 00:15:23 +00001406void ARMBaseInstrInfo::
1407reMaterialize(MachineBasicBlock &MBB,
1408 MachineBasicBlock::iterator I,
1409 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001410 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001411 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001412 unsigned Opcode = Orig->getOpcode();
1413 switch (Opcode) {
1414 default: {
1415 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001416 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001417 MBB.insert(I, MI);
1418 break;
1419 }
1420 case ARM::tLDRpci_pic:
1421 case ARM::t2LDRpci_pic: {
1422 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001423 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001424 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001425 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1426 DestReg)
1427 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001428 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001429 break;
1430 }
1431 }
Evan Chengfe864422009-11-08 00:15:23 +00001432}
1433
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001434MachineInstr *
1435ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001436 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001437 switch(Orig->getOpcode()) {
1438 case ARM::tLDRpci_pic:
1439 case ARM::t2LDRpci_pic: {
1440 unsigned CPI = Orig->getOperand(1).getIndex();
1441 unsigned PCLabelId = duplicateCPV(MF, CPI);
1442 Orig->getOperand(1).setIndex(CPI);
1443 Orig->getOperand(2).setImm(PCLabelId);
1444 break;
1445 }
1446 }
1447 return MI;
1448}
1449
Evan Chenge9c46c22010-03-03 01:44:33 +00001450bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001451 const MachineInstr *MI1,
1452 const MachineRegisterInfo *MRI) const {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001453 unsigned Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001454 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001455 Opcode == ARM::t2LDRpci_pic ||
1456 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001457 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001458 Opcode == ARM::LDRLIT_ga_pcrel ||
1459 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1460 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001461 Opcode == ARM::MOV_ga_pcrel ||
1462 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001463 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001464 if (MI1->getOpcode() != Opcode)
1465 return false;
1466 if (MI0->getNumOperands() != MI1->getNumOperands())
1467 return false;
1468
1469 const MachineOperand &MO0 = MI0->getOperand(1);
1470 const MachineOperand &MO1 = MI1->getOperand(1);
1471 if (MO0.getOffset() != MO1.getOffset())
1472 return false;
1473
Tim Northover72360d22013-12-02 10:35:41 +00001474 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1475 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1476 Opcode == ARM::tLDRLIT_ga_pcrel ||
1477 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001478 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001479 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001480 // Ignore the PC labels.
1481 return MO0.getGlobal() == MO1.getGlobal();
1482
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001483 const MachineFunction *MF = MI0->getParent()->getParent();
1484 const MachineConstantPool *MCP = MF->getConstantPool();
1485 int CPI0 = MO0.getIndex();
1486 int CPI1 = MO1.getIndex();
1487 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1488 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001489 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1490 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1491 if (isARMCP0 && isARMCP1) {
1492 ARMConstantPoolValue *ACPV0 =
1493 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1494 ARMConstantPoolValue *ACPV1 =
1495 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1496 return ACPV0->hasSameValue(ACPV1);
1497 } else if (!isARMCP0 && !isARMCP1) {
1498 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1499 }
1500 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001501 } else if (Opcode == ARM::PICLDR) {
1502 if (MI1->getOpcode() != Opcode)
1503 return false;
1504 if (MI0->getNumOperands() != MI1->getNumOperands())
1505 return false;
1506
1507 unsigned Addr0 = MI0->getOperand(1).getReg();
1508 unsigned Addr1 = MI1->getOperand(1).getReg();
1509 if (Addr0 != Addr1) {
1510 if (!MRI ||
1511 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1512 !TargetRegisterInfo::isVirtualRegister(Addr1))
1513 return false;
1514
1515 // This assumes SSA form.
1516 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1517 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1518 // Check if the loaded value, e.g. a constantpool of a global address, are
1519 // the same.
1520 if (!produceSameValue(Def0, Def1, MRI))
1521 return false;
1522 }
1523
1524 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1525 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1526 const MachineOperand &MO0 = MI0->getOperand(i);
1527 const MachineOperand &MO1 = MI1->getOperand(i);
1528 if (!MO0.isIdenticalTo(MO1))
1529 return false;
1530 }
1531 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001532 }
1533
Evan Chenge9c46c22010-03-03 01:44:33 +00001534 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001535}
1536
Bill Wendlingf4707472010-06-23 23:00:16 +00001537/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1538/// determine if two loads are loading from the same base address. It should
1539/// only return true if the base pointers are the same and the only differences
1540/// between the two addresses is the offset. It also returns the offsets by
1541/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001542///
1543/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1544/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001545bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1546 int64_t &Offset1,
1547 int64_t &Offset2) const {
1548 // Don't worry about Thumb: just ARM and Thumb2.
1549 if (Subtarget.isThumb1Only()) return false;
1550
1551 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1552 return false;
1553
1554 switch (Load1->getMachineOpcode()) {
1555 default:
1556 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001557 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001558 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001559 case ARM::LDRD:
1560 case ARM::LDRH:
1561 case ARM::LDRSB:
1562 case ARM::LDRSH:
1563 case ARM::VLDRD:
1564 case ARM::VLDRS:
1565 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001566 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001567 case ARM::t2LDRDi8:
1568 case ARM::t2LDRSHi8:
1569 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001570 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001571 case ARM::t2LDRSHi12:
1572 break;
1573 }
1574
1575 switch (Load2->getMachineOpcode()) {
1576 default:
1577 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001578 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001579 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001580 case ARM::LDRD:
1581 case ARM::LDRH:
1582 case ARM::LDRSB:
1583 case ARM::LDRSH:
1584 case ARM::VLDRD:
1585 case ARM::VLDRS:
1586 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001587 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001588 case ARM::t2LDRSHi8:
1589 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001590 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001591 case ARM::t2LDRSHi12:
1592 break;
1593 }
1594
1595 // Check if base addresses and chain operands match.
1596 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1597 Load1->getOperand(4) != Load2->getOperand(4))
1598 return false;
1599
1600 // Index should be Reg0.
1601 if (Load1->getOperand(3) != Load2->getOperand(3))
1602 return false;
1603
1604 // Determine the offsets.
1605 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1606 isa<ConstantSDNode>(Load2->getOperand(1))) {
1607 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1608 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1609 return true;
1610 }
1611
1612 return false;
1613}
1614
1615/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001616/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001617/// be scheduled togther. On some targets if two loads are loading from
1618/// addresses in the same cache line, it's better if they are scheduled
1619/// together. This function takes two integers that represent the load offsets
1620/// from the common base address. It returns true if it decides it's desirable
1621/// to schedule the two loads together. "NumLoads" is the number of loads that
1622/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001623///
1624/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1625/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001626bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1627 int64_t Offset1, int64_t Offset2,
1628 unsigned NumLoads) const {
1629 // Don't worry about Thumb: just ARM and Thumb2.
1630 if (Subtarget.isThumb1Only()) return false;
1631
1632 assert(Offset2 > Offset1);
1633
1634 if ((Offset2 - Offset1) / 8 > 64)
1635 return false;
1636
Renato Golinb184cd92013-08-14 16:35:29 +00001637 // Check if the machine opcodes are different. If they are different
1638 // then we consider them to not be of the same base address,
1639 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1640 // In this case, they are considered to be the same because they are different
1641 // encoding forms of the same basic instruction.
1642 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1643 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1644 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1645 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1646 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001647 return false; // FIXME: overly conservative?
1648
1649 // Four loads in a row should be sufficient.
1650 if (NumLoads >= 3)
1651 return false;
1652
1653 return true;
1654}
1655
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001656bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1657 const MachineBasicBlock *MBB,
1658 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001659 // Debug info is never a scheduling boundary. It's necessary to be explicit
1660 // due to the special treatment of IT instructions below, otherwise a
1661 // dbg_value followed by an IT will result in the IT instruction being
1662 // considered a scheduling hazard, which is wrong. It should be the actual
1663 // instruction preceding the dbg_value instruction(s), just like it is
1664 // when debug info is not present.
1665 if (MI->isDebugValue())
1666 return false;
1667
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001668 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001669 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001670 return true;
1671
1672 // Treat the start of the IT block as a scheduling boundary, but schedule
1673 // t2IT along with all instructions following it.
1674 // FIXME: This is a big hammer. But the alternative is to add all potential
1675 // true and anti dependencies to IT block instructions as implicit operands
1676 // to the t2IT instruction. The added compile time and complexity does not
1677 // seem worth it.
1678 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001679 // Make sure to skip any dbg_value instructions
1680 while (++I != MBB->end() && I->isDebugValue())
1681 ;
1682 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001683 return true;
1684
1685 // Don't attempt to schedule around any instruction that defines
1686 // a stack-oriented pointer, as it's unlikely to be profitable. This
1687 // saves compile time, because it doesn't require every single
1688 // stack slot reference to depend on the instruction that does the
1689 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001690 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001691 // No ARM calling conventions change the stack pointer. (X86 calling
1692 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001693 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001694 return true;
1695
1696 return false;
1697}
1698
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001699bool ARMBaseInstrInfo::
1700isProfitableToIfCvt(MachineBasicBlock &MBB,
1701 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001702 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001703 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001704 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001705
Peter Collingbourne65295232015-04-23 20:31:30 +00001706 // If we are optimizing for size, see if the branch in the predecessor can be
1707 // lowered to cbn?z by the constant island lowering pass, and return false if
1708 // so. This results in a shorter instruction sequence.
Sanjay Patel924879a2015-08-04 15:49:57 +00001709 if (MBB.getParent()->getFunction()->optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001710 MachineBasicBlock *Pred = *MBB.pred_begin();
1711 if (!Pred->empty()) {
1712 MachineInstr *LastMI = &*Pred->rbegin();
1713 if (LastMI->getOpcode() == ARM::t2Bcc) {
1714 MachineBasicBlock::iterator CmpMI = LastMI;
1715 if (CmpMI != Pred->begin()) {
1716 --CmpMI;
1717 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1718 CmpMI->getOpcode() == ARM::t2CMPri) {
1719 unsigned Reg = CmpMI->getOperand(0).getReg();
1720 unsigned PredReg = 0;
1721 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1722 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1723 isARMLowRegister(Reg))
1724 return false;
1725 }
1726 }
1727 }
1728 }
1729 }
1730
Owen Anderson88af7d02010-09-28 18:32:13 +00001731 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001732 // Here we scale up each component of UnpredCost to avoid precision issue when
1733 // scaling NumCycles by Probability.
1734 const unsigned ScalingUpFactor = 1024;
1735 unsigned UnpredCost = Probability.scale(NumCycles * ScalingUpFactor);
1736 UnpredCost += ScalingUpFactor; // The branch itself
1737 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001738
Cong Houf9f9ffb2015-09-18 18:19:40 +00001739 return (NumCycles + ExtraPredCycles) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001740}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001741
Evan Cheng02b184d2010-06-25 22:42:03 +00001742bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001743isProfitableToIfCvt(MachineBasicBlock &TMBB,
1744 unsigned TCycles, unsigned TExtra,
1745 MachineBasicBlock &FMBB,
1746 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001747 BranchProbability Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001748 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001749 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001750
Owen Anderson88af7d02010-09-28 18:32:13 +00001751 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001752 // Here we scale up each component of UnpredCost to avoid precision issue when
1753 // scaling TCycles/FCycles by Probability.
1754 const unsigned ScalingUpFactor = 1024;
1755 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1756 unsigned FUnpredCost =
1757 Probability.getCompl().scale(FCycles * ScalingUpFactor);
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001758 unsigned UnpredCost = TUnpredCost + FUnpredCost;
Cong Houf9f9ffb2015-09-18 18:19:40 +00001759 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1760 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001761
Cong Houf9f9ffb2015-09-18 18:19:40 +00001762 return (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001763}
1764
Bob Wilsone8a549c2012-09-29 21:43:49 +00001765bool
1766ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1767 MachineBasicBlock &FMBB) const {
1768 // Reduce false anti-dependencies to let Swift's out-of-order execution
1769 // engine do its thing.
1770 return Subtarget.isSwift();
1771}
1772
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001773/// getInstrPredicate - If instruction is predicated, returns its predicate
1774/// condition, otherwise returns AL. It also returns the condition code
1775/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001776ARMCC::CondCodes
1777llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001778 int PIdx = MI->findFirstPredOperandIdx();
1779 if (PIdx == -1) {
1780 PredReg = 0;
1781 return ARMCC::AL;
1782 }
1783
1784 PredReg = MI->getOperand(PIdx+1).getReg();
1785 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1786}
1787
1788
Matthias Braunfa3872e2015-05-18 20:27:55 +00001789unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001790 if (Opc == ARM::B)
1791 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001792 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001793 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001794 if (Opc == ARM::t2B)
1795 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001796
1797 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001798}
1799
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001800MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr *MI,
1801 bool NewMI,
1802 unsigned OpIdx1,
1803 unsigned OpIdx2) const {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001804 switch (MI->getOpcode()) {
1805 case ARM::MOVCCr:
1806 case ARM::t2MOVCCr: {
1807 // MOVCC can be commuted by inverting the condition.
1808 unsigned PredReg = 0;
1809 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1810 // MOVCC AL can't be inverted. Shouldn't happen.
1811 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001812 return nullptr;
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001813 MI = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001814 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001815 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001816 // After swapping the MOVCC operands, also invert the condition.
1817 MI->getOperand(MI->findFirstPredOperandIdx())
1818 .setImm(ARMCC::getOppositeCondition(CC));
1819 return MI;
1820 }
1821 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001822 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001823}
Evan Cheng780748d2009-07-28 05:48:47 +00001824
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001825/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001826/// return the defining instruction.
1827static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1828 const MachineRegisterInfo &MRI,
1829 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001830 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001831 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001832 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001833 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001834 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001835 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001836 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001837 // MI is folded into the MOVCC by predicating it.
1838 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001839 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001840 // Check if MI has any non-dead defs or physreg uses. This also detects
1841 // predicated instructions which will be reading CPSR.
1842 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1843 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001844 // Reject frame index operands, PEI can't handle the predicated pseudos.
1845 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001846 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001847 if (!MO.isReg())
1848 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001849 // MI can't have any tied operands, that would conflict with predication.
1850 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001851 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001852 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001853 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001854 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001855 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001856 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001857 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001858 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001859 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001860 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001861}
1862
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001863bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1864 SmallVectorImpl<MachineOperand> &Cond,
1865 unsigned &TrueOp, unsigned &FalseOp,
1866 bool &Optimizable) const {
1867 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1868 "Unknown select instruction");
1869 // MOVCC operands:
1870 // 0: Def.
1871 // 1: True use.
1872 // 2: False use.
1873 // 3: Condition code.
1874 // 4: CPSR use.
1875 TrueOp = 1;
1876 FalseOp = 2;
1877 Cond.push_back(MI->getOperand(3));
1878 Cond.push_back(MI->getOperand(4));
1879 // We can always fold a def.
1880 Optimizable = true;
1881 return false;
1882}
1883
Mehdi Amini22e59742015-01-13 07:07:13 +00001884MachineInstr *
1885ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1886 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1887 bool PreferFalse) const {
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001888 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1889 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001890 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001891 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1892 bool Invert = !DefMI;
1893 if (!DefMI)
1894 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1895 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001896 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001897
Matthias Braun2f169f92013-10-04 16:52:56 +00001898 // Find new register class to use.
1899 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1900 unsigned DestReg = MI->getOperand(0).getReg();
1901 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1902 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001903 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001904
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001905 // Create a new predicated version of DefMI.
1906 // Rfalse is the first use.
1907 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001908 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001909
1910 // Copy all the DefMI operands, excluding its (null) predicate.
1911 const MCInstrDesc &DefDesc = DefMI->getDesc();
1912 for (unsigned i = 1, e = DefDesc.getNumOperands();
1913 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1914 NewMI.addOperand(DefMI->getOperand(i));
1915
1916 unsigned CondCode = MI->getOperand(3).getImm();
1917 if (Invert)
1918 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1919 else
1920 NewMI.addImm(CondCode);
1921 NewMI.addOperand(MI->getOperand(4));
1922
1923 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1924 if (NewMI->hasOptionalDef())
1925 AddDefaultCC(NewMI);
1926
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001927 // The output register value when the predicate is false is an implicit
1928 // register operand tied to the first def.
1929 // The tie makes the register allocator ensure the FalseReg is allocated the
1930 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001931 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001932 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001933 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1934
Mehdi Amini22e59742015-01-13 07:07:13 +00001935 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1936 SeenMIs.insert(NewMI);
1937 SeenMIs.erase(DefMI);
1938
Pete Cooper2127b002015-04-30 23:57:47 +00001939 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1940 // DefMI would be invalid when tranferred inside the loop. Checking for a
1941 // loop is expensive, but at least remove kill flags if they are in different
1942 // BBs.
1943 if (DefMI->getParent() != MI->getParent())
1944 NewMI->clearKillInfo();
1945
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001946 // The caller will erase MI, but not DefMI.
1947 DefMI->eraseFromParent();
1948 return NewMI;
1949}
1950
Andrew Trick924123a2011-09-21 02:20:46 +00001951/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1952/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1953/// def operand.
1954///
1955/// This will go away once we can teach tblgen how to set the optional CPSR def
1956/// operand itself.
1957struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001958 uint16_t PseudoOpc;
1959 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001960};
1961
Craig Topper2fbd1302012-05-24 03:59:11 +00001962static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001963 {ARM::ADDSri, ARM::ADDri},
1964 {ARM::ADDSrr, ARM::ADDrr},
1965 {ARM::ADDSrsi, ARM::ADDrsi},
1966 {ARM::ADDSrsr, ARM::ADDrsr},
1967
1968 {ARM::SUBSri, ARM::SUBri},
1969 {ARM::SUBSrr, ARM::SUBrr},
1970 {ARM::SUBSrsi, ARM::SUBrsi},
1971 {ARM::SUBSrsr, ARM::SUBrsr},
1972
1973 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001974 {ARM::RSBSrsi, ARM::RSBrsi},
1975 {ARM::RSBSrsr, ARM::RSBrsr},
1976
1977 {ARM::t2ADDSri, ARM::t2ADDri},
1978 {ARM::t2ADDSrr, ARM::t2ADDrr},
1979 {ARM::t2ADDSrs, ARM::t2ADDrs},
1980
1981 {ARM::t2SUBSri, ARM::t2SUBri},
1982 {ARM::t2SUBSrr, ARM::t2SUBrr},
1983 {ARM::t2SUBSrs, ARM::t2SUBrs},
1984
1985 {ARM::t2RSBSri, ARM::t2RSBri},
1986 {ARM::t2RSBSrs, ARM::t2RSBrs},
1987};
1988
1989unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001990 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1991 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1992 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001993 return 0;
1994}
1995
Evan Cheng780748d2009-07-28 05:48:47 +00001996void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1997 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1998 unsigned DestReg, unsigned BaseReg, int NumBytes,
1999 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002000 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002001 if (NumBytes == 0 && DestReg != BaseReg) {
2002 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
2003 .addReg(BaseReg, RegState::Kill)
2004 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2005 .setMIFlags(MIFlags);
2006 return;
2007 }
2008
Evan Cheng780748d2009-07-28 05:48:47 +00002009 bool isSub = NumBytes < 0;
2010 if (isSub) NumBytes = -NumBytes;
2011
2012 while (NumBytes) {
2013 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2014 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2015 assert(ThisVal && "Didn't extract field correctly");
2016
2017 // We will handle these bits from offset, clear them.
2018 NumBytes &= ~ThisVal;
2019
2020 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2021
2022 // Build the new ADD / SUB.
2023 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2024 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2025 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002026 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2027 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002028 BaseReg = DestReg;
2029 }
2030}
2031
Tim Northoverdee86042013-12-02 14:46:26 +00002032bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2033 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002034 unsigned NumBytes) {
2035 // This optimisation potentially adds lots of load and store
2036 // micro-operations, it's only really a great benefit to code-size.
Sanjay Patel924879a2015-08-04 15:49:57 +00002037 if (!MF.getFunction()->optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002038 return false;
2039
2040 // If only one register is pushed/popped, LLVM can use an LDR/STR
2041 // instead. We can't modify those so make sure we're dealing with an
2042 // instruction we understand.
2043 bool IsPop = isPopOpcode(MI->getOpcode());
2044 bool IsPush = isPushOpcode(MI->getOpcode());
2045 if (!IsPush && !IsPop)
2046 return false;
2047
2048 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2049 MI->getOpcode() == ARM::VLDMDIA_UPD;
2050 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2051 MI->getOpcode() == ARM::tPOP ||
2052 MI->getOpcode() == ARM::tPOP_RET;
2053
2054 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2055 MI->getOperand(1).getReg() == ARM::SP)) &&
2056 "trying to fold sp update into non-sp-updating push/pop");
2057
2058 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2059 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2060 // if this is violated.
2061 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2062 return false;
2063
2064 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2065 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2066 int RegListIdx = IsT1PushPop ? 2 : 4;
2067
2068 // Calculate the space we'll need in terms of registers.
2069 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2070 unsigned RD0Reg, RegsNeeded;
2071 if (IsVFPPushPop) {
2072 RD0Reg = ARM::D0;
2073 RegsNeeded = NumBytes / 8;
2074 } else {
2075 RD0Reg = ARM::R0;
2076 RegsNeeded = NumBytes / 4;
2077 }
2078
2079 // We're going to have to strip all list operands off before
2080 // re-adding them since the order matters, so save the existing ones
2081 // for later.
2082 SmallVector<MachineOperand, 4> RegList;
2083 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2084 RegList.push_back(MI->getOperand(i));
2085
Tim Northover93bcc662013-11-08 17:18:07 +00002086 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002087 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002088
2089 // Now try to find enough space in the reglist to allocate NumBytes.
2090 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002091 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002092 if (!IsPop) {
2093 // Pushing any register is completely harmless, mark the
2094 // register involved as undef since we don't care about it in
2095 // the slightest.
2096 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2097 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002098 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002099 continue;
2100 }
2101
Tim Northover45479dc2013-12-01 14:16:24 +00002102 // However, we can only pop an extra register if it's not live. For
2103 // registers live within the function we might clobber a return value
2104 // register; the other way a register can be live here is if it's
2105 // callee-saved.
2106 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002107 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2108 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002109 // VFP pops don't allow holes in the register list, so any skip is fatal
2110 // for our transformation. GPR pops do, so we should just keep looking.
2111 if (IsVFPPushPop)
2112 return false;
2113 else
2114 continue;
2115 }
Tim Northover93bcc662013-11-08 17:18:07 +00002116
2117 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002118 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2119 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002120 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002121 }
2122
2123 if (RegsNeeded > 0)
2124 return false;
2125
2126 // Finally we know we can profitably perform the optimisation so go
2127 // ahead: strip all existing registers off and add them back again
2128 // in the right order.
2129 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2130 MI->RemoveOperand(i);
2131
2132 // Add the complete list back in.
2133 MachineInstrBuilder MIB(MF, &*MI);
2134 for (int i = RegList.size() - 1; i >= 0; --i)
2135 MIB.addOperand(RegList[i]);
2136
2137 return true;
2138}
2139
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002140bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2141 unsigned FrameReg, int &Offset,
2142 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002143 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002144 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002145 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2146 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002147
Evan Cheng780748d2009-07-28 05:48:47 +00002148 // Memory operands in inline assembly always use AddrMode2.
2149 if (Opcode == ARM::INLINEASM)
2150 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002151
Evan Cheng780748d2009-07-28 05:48:47 +00002152 if (Opcode == ARM::ADDri) {
2153 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2154 if (Offset == 0) {
2155 // Turn it into a move.
2156 MI.setDesc(TII.get(ARM::MOVr));
2157 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2158 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002159 Offset = 0;
2160 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002161 } else if (Offset < 0) {
2162 Offset = -Offset;
2163 isSub = true;
2164 MI.setDesc(TII.get(ARM::SUBri));
2165 }
2166
2167 // Common case: small offset, fits into instruction.
2168 if (ARM_AM::getSOImmVal(Offset) != -1) {
2169 // Replace the FrameIndex with sp / fp
2170 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2171 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002172 Offset = 0;
2173 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002174 }
2175
2176 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2177 // as possible.
2178 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2179 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2180
2181 // We will handle these bits from offset, clear them.
2182 Offset &= ~ThisImmVal;
2183
2184 // Get the properly encoded SOImmVal field.
2185 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2186 "Bit extraction didn't work?");
2187 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2188 } else {
2189 unsigned ImmIdx = 0;
2190 int InstrOffs = 0;
2191 unsigned NumBits = 0;
2192 unsigned Scale = 1;
2193 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002194 case ARMII::AddrMode_i12: {
2195 ImmIdx = FrameRegIdx + 1;
2196 InstrOffs = MI.getOperand(ImmIdx).getImm();
2197 NumBits = 12;
2198 break;
2199 }
Evan Cheng780748d2009-07-28 05:48:47 +00002200 case ARMII::AddrMode2: {
2201 ImmIdx = FrameRegIdx+2;
2202 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2203 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2204 InstrOffs *= -1;
2205 NumBits = 12;
2206 break;
2207 }
2208 case ARMII::AddrMode3: {
2209 ImmIdx = FrameRegIdx+2;
2210 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2211 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2212 InstrOffs *= -1;
2213 NumBits = 8;
2214 break;
2215 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002216 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002217 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002218 // Can't fold any offset even if it's zero.
2219 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002220 case ARMII::AddrMode5: {
2221 ImmIdx = FrameRegIdx+1;
2222 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2223 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2224 InstrOffs *= -1;
2225 NumBits = 8;
2226 Scale = 4;
2227 break;
2228 }
2229 default:
2230 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002231 }
2232
2233 Offset += InstrOffs * Scale;
2234 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2235 if (Offset < 0) {
2236 Offset = -Offset;
2237 isSub = true;
2238 }
2239
2240 // Attempt to fold address comp. if opcode has offset bits
2241 if (NumBits > 0) {
2242 // Common case: small offset, fits into instruction.
2243 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2244 int ImmedOffset = Offset / Scale;
2245 unsigned Mask = (1 << NumBits) - 1;
2246 if ((unsigned)Offset <= Mask * Scale) {
2247 // Replace the FrameIndex with sp
2248 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002249 // FIXME: When addrmode2 goes away, this will simplify (like the
2250 // T2 version), as the LDR.i12 versions don't need the encoding
2251 // tricks for the offset value.
2252 if (isSub) {
2253 if (AddrMode == ARMII::AddrMode_i12)
2254 ImmedOffset = -ImmedOffset;
2255 else
2256 ImmedOffset |= 1 << NumBits;
2257 }
Evan Cheng780748d2009-07-28 05:48:47 +00002258 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002259 Offset = 0;
2260 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002261 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002262
Evan Cheng780748d2009-07-28 05:48:47 +00002263 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2264 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002265 if (isSub) {
2266 if (AddrMode == ARMII::AddrMode_i12)
2267 ImmedOffset = -ImmedOffset;
2268 else
2269 ImmedOffset |= 1 << NumBits;
2270 }
Evan Cheng780748d2009-07-28 05:48:47 +00002271 ImmOp.ChangeToImmediate(ImmedOffset);
2272 Offset &= ~(Mask*Scale);
2273 }
2274 }
2275
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002276 Offset = (isSub) ? -Offset : Offset;
2277 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002278}
Bill Wendling7de9d522010-08-06 01:32:48 +00002279
Manman Ren6fa76dc2012-06-29 21:33:59 +00002280/// analyzeCompare - For a comparison instruction, return the source registers
2281/// in SrcReg and SrcReg2 if having two register operands, and the value it
2282/// compares against in CmpValue. Return true if the comparison instruction
2283/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002284bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002285analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2286 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002287 switch (MI->getOpcode()) {
2288 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002289 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002290 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002291 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002292 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002293 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002294 CmpValue = MI->getOperand(1).getImm();
2295 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002296 case ARM::CMPrr:
2297 case ARM::t2CMPrr:
2298 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002299 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002300 CmpMask = ~0;
2301 CmpValue = 0;
2302 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002303 case ARM::TSTri:
2304 case ARM::t2TSTri:
2305 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002306 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002307 CmpMask = MI->getOperand(1).getImm();
2308 CmpValue = 0;
2309 return true;
2310 }
2311
2312 return false;
2313}
2314
Gabor Greifd36e3e82010-09-29 10:12:08 +00002315/// isSuitableForMask - Identify a suitable 'and' instruction that
2316/// operates on the given source register and applies the same mask
2317/// as a 'tst' instruction. Provide a limited look-through for copies.
2318/// When successful, MI will hold the found instruction.
2319static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002320 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002321 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002322 case ARM::ANDri:
2323 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002324 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002325 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002326 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002327 return true;
2328 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002329 }
2330
2331 return false;
2332}
2333
Manman Renb1b3db62012-06-29 22:06:19 +00002334/// getSwappedCondition - assume the flags are set by MI(a,b), return
2335/// the condition code if we modify the instructions such that flags are
2336/// set by MI(b,a).
2337inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2338 switch (CC) {
2339 default: return ARMCC::AL;
2340 case ARMCC::EQ: return ARMCC::EQ;
2341 case ARMCC::NE: return ARMCC::NE;
2342 case ARMCC::HS: return ARMCC::LS;
2343 case ARMCC::LO: return ARMCC::HI;
2344 case ARMCC::HI: return ARMCC::LO;
2345 case ARMCC::LS: return ARMCC::HS;
2346 case ARMCC::GE: return ARMCC::LE;
2347 case ARMCC::LT: return ARMCC::GT;
2348 case ARMCC::GT: return ARMCC::LT;
2349 case ARMCC::LE: return ARMCC::GE;
2350 }
2351}
2352
2353/// isRedundantFlagInstr - check whether the first instruction, whose only
2354/// purpose is to update flags, can be made redundant.
2355/// CMPrr can be made redundant by SUBrr if the operands are the same.
2356/// CMPri can be made redundant by SUBri if the operands are the same.
2357/// This function can be extended later on.
2358inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2359 unsigned SrcReg2, int ImmValue,
2360 MachineInstr *OI) {
2361 if ((CmpI->getOpcode() == ARM::CMPrr ||
2362 CmpI->getOpcode() == ARM::t2CMPrr) &&
2363 (OI->getOpcode() == ARM::SUBrr ||
2364 OI->getOpcode() == ARM::t2SUBrr) &&
2365 ((OI->getOperand(1).getReg() == SrcReg &&
2366 OI->getOperand(2).getReg() == SrcReg2) ||
2367 (OI->getOperand(1).getReg() == SrcReg2 &&
2368 OI->getOperand(2).getReg() == SrcReg)))
2369 return true;
2370
2371 if ((CmpI->getOpcode() == ARM::CMPri ||
2372 CmpI->getOpcode() == ARM::t2CMPri) &&
2373 (OI->getOpcode() == ARM::SUBri ||
2374 OI->getOpcode() == ARM::t2SUBri) &&
2375 OI->getOperand(1).getReg() == SrcReg &&
2376 OI->getOperand(2).getImm() == ImmValue)
2377 return true;
2378 return false;
2379}
2380
Manman Ren6fa76dc2012-06-29 21:33:59 +00002381/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2382/// comparison into one that sets the zero bit in the flags register;
2383/// Remove a redundant Compare instruction if an earlier instruction can set the
2384/// flags in the same way as Compare.
2385/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2386/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2387/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002388bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002389optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2390 int CmpMask, int CmpValue,
2391 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002392 // Get the unique definition of SrcReg.
2393 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2394 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002395
Gabor Greifadbbb932010-09-21 12:01:15 +00002396 // Masked compares sometimes use the same register as the corresponding 'and'.
2397 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002398 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002399 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002400 for (MachineRegisterInfo::use_instr_iterator
2401 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2402 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002403 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002404 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002405 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2406 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002407 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002408 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002409 break;
2410 }
2411 if (!MI) return false;
2412 }
2413 }
2414
Manman Rendc8ad002012-05-11 01:30:47 +00002415 // Get ready to iterate backward from CmpInstr.
2416 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2417 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002418
2419 // Early exit if CmpInstr is at the beginning of the BB.
2420 if (I == B) return false;
2421
Manman Rendc8ad002012-05-11 01:30:47 +00002422 // There are two possible candidates which can be changed to set CPSR:
2423 // One is MI, the other is a SUB instruction.
2424 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2425 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002426 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002427 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002428 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002429 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002430 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002431 // Conservatively refuse to convert an instruction which isn't in the same
2432 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002433 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2434 // Thus we cannot return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002435 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002436 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002437 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002438 else
2439 return false;
2440 }
2441
2442 // Check that CPSR isn't set between the comparison instruction and the one we
2443 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002444 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002445 --I;
2446 for (; I != E; --I) {
2447 const MachineInstr &Instr = *I;
2448
Manman Renb1b3db62012-06-29 22:06:19 +00002449 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2450 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002451 // This instruction modifies or uses CPSR after the one we want to
2452 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002453 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002454
Manman Renb1b3db62012-06-29 22:06:19 +00002455 // Check whether CmpInstr can be made redundant by the current instruction.
2456 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002457 Sub = &*I;
2458 break;
2459 }
2460
Evan Chengd757c882010-09-21 23:49:07 +00002461 if (I == B)
2462 // The 'and' is below the comparison instruction.
2463 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002464 }
2465
Manman Rendc8ad002012-05-11 01:30:47 +00002466 // Return false if no candidates exist.
2467 if (!MI && !Sub)
2468 return false;
2469
2470 // The single candidate is called MI.
2471 if (!MI) MI = Sub;
2472
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002473 // We can't use a predicated instruction - it doesn't always write the flags.
2474 if (isPredicated(MI))
2475 return false;
2476
Bill Wendling7de9d522010-08-06 01:32:48 +00002477 switch (MI->getOpcode()) {
2478 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002479 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002480 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002481 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002482 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002483 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002484 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002485 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002486 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002487 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002488 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002489 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002490 case ARM::SBCri:
2491 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002492 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002493 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002494 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002495 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002496 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002497 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002498 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002499 case ARM::t2SBCri:
2500 case ARM::ANDrr:
2501 case ARM::ANDri:
2502 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002503 case ARM::t2ANDri:
2504 case ARM::ORRrr:
2505 case ARM::ORRri:
2506 case ARM::t2ORRrr:
2507 case ARM::t2ORRri:
2508 case ARM::EORrr:
2509 case ARM::EORri:
2510 case ARM::t2EORrr:
2511 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002512 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002513 // When checking against MI: if it's a conditional code that requires
2514 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002515 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2516 // If we are done with the basic block, we need to check whether CPSR is
2517 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002518 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2519 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002520 bool isSafe = false;
2521 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002522 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002523 while (!isSafe && ++I != E) {
2524 const MachineInstr &Instr = *I;
2525 for (unsigned IO = 0, EO = Instr.getNumOperands();
2526 !isSafe && IO != EO; ++IO) {
2527 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002528 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2529 isSafe = true;
2530 break;
2531 }
Evan Cheng425489d2011-03-23 22:52:04 +00002532 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2533 continue;
2534 if (MO.isDef()) {
2535 isSafe = true;
2536 break;
2537 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002538 // Condition code is after the operand before CPSR except for VSELs.
2539 ARMCC::CondCodes CC;
2540 bool IsInstrVSel = true;
2541 switch (Instr.getOpcode()) {
2542 default:
2543 IsInstrVSel = false;
2544 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2545 break;
2546 case ARM::VSELEQD:
2547 case ARM::VSELEQS:
2548 CC = ARMCC::EQ;
2549 break;
2550 case ARM::VSELGTD:
2551 case ARM::VSELGTS:
2552 CC = ARMCC::GT;
2553 break;
2554 case ARM::VSELGED:
2555 case ARM::VSELGES:
2556 CC = ARMCC::GE;
2557 break;
2558 case ARM::VSELVSS:
2559 case ARM::VSELVSD:
2560 CC = ARMCC::VS;
2561 break;
2562 }
2563
Manman Renb1b3db62012-06-29 22:06:19 +00002564 if (Sub) {
2565 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2566 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002567 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002568 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2569 // on CMP needs to be updated to be based on SUB.
2570 // Push the condition code operands to OperandsToUpdate.
2571 // If it is safe to remove CmpInstr, the condition code of these
2572 // operands will be modified.
2573 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002574 Sub->getOperand(2).getReg() == SrcReg) {
2575 // VSel doesn't support condition code update.
2576 if (IsInstrVSel)
2577 return false;
2578 OperandsToUpdate.push_back(
2579 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2580 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002581 } else {
2582 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002583 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002584 case ARMCC::EQ: // Z
2585 case ARMCC::NE: // Z
2586 case ARMCC::MI: // N
2587 case ARMCC::PL: // N
2588 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002589 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002590 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002591 case ARMCC::HS: // C
2592 case ARMCC::LO: // C
2593 case ARMCC::VS: // V
2594 case ARMCC::VC: // V
2595 case ARMCC::HI: // C Z
2596 case ARMCC::LS: // C Z
2597 case ARMCC::GE: // N V
2598 case ARMCC::LT: // N V
2599 case ARMCC::GT: // Z N V
2600 case ARMCC::LE: // Z N V
2601 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002602 return false;
2603 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002604 }
Evan Cheng425489d2011-03-23 22:52:04 +00002605 }
2606 }
2607
Manman Ren34cb93e2012-07-11 22:51:44 +00002608 // If CPSR is not killed nor re-defined, we should check whether it is
2609 // live-out. If it is live-out, do not optimize.
2610 if (!isSafe) {
2611 MachineBasicBlock *MBB = CmpInstr->getParent();
2612 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2613 SE = MBB->succ_end(); SI != SE; ++SI)
2614 if ((*SI)->isLiveIn(ARM::CPSR))
2615 return false;
2616 }
Evan Cheng425489d2011-03-23 22:52:04 +00002617
Evan Cheng65536472010-11-17 08:06:50 +00002618 // Toggle the optional operand to CPSR.
2619 MI->getOperand(5).setReg(ARM::CPSR);
2620 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002621 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002622 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002623
2624 // Modify the condition code of operands in OperandsToUpdate.
2625 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2626 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002627 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2628 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002629 return true;
2630 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002631 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002632
2633 return false;
2634}
Evan Cheng367a5df2010-09-09 18:18:55 +00002635
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002636bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2637 MachineInstr *DefMI, unsigned Reg,
2638 MachineRegisterInfo *MRI) const {
2639 // Fold large immediates into add, sub, or, xor.
2640 unsigned DefOpc = DefMI->getOpcode();
2641 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2642 return false;
2643 if (!DefMI->getOperand(1).isImm())
2644 // Could be t2MOVi32imm <ga:xx>
2645 return false;
2646
2647 if (!MRI->hasOneNonDBGUse(Reg))
2648 return false;
2649
Evan Chenga2b48d92012-03-26 23:31:00 +00002650 const MCInstrDesc &DefMCID = DefMI->getDesc();
2651 if (DefMCID.hasOptionalDef()) {
2652 unsigned NumOps = DefMCID.getNumOperands();
2653 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2654 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2655 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2656 // to delete DefMI.
2657 return false;
2658 }
2659
2660 const MCInstrDesc &UseMCID = UseMI->getDesc();
2661 if (UseMCID.hasOptionalDef()) {
2662 unsigned NumOps = UseMCID.getNumOperands();
2663 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2664 // If the instruction sets the flag, do not attempt this optimization
2665 // since it may change the semantics of the code.
2666 return false;
2667 }
2668
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002669 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002670 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002671 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002672 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002673 bool Commute = false;
2674 switch (UseOpc) {
2675 default: return false;
2676 case ARM::SUBrr:
2677 case ARM::ADDrr:
2678 case ARM::ORRrr:
2679 case ARM::EORrr:
2680 case ARM::t2SUBrr:
2681 case ARM::t2ADDrr:
2682 case ARM::t2ORRrr:
2683 case ARM::t2EORrr: {
2684 Commute = UseMI->getOperand(2).getReg() != Reg;
2685 switch (UseOpc) {
2686 default: break;
2687 case ARM::SUBrr: {
2688 if (Commute)
2689 return false;
2690 ImmVal = -ImmVal;
2691 NewUseOpc = ARM::SUBri;
2692 // Fallthrough
2693 }
2694 case ARM::ADDrr:
2695 case ARM::ORRrr:
2696 case ARM::EORrr: {
2697 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2698 return false;
2699 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2700 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2701 switch (UseOpc) {
2702 default: break;
2703 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2704 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2705 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2706 }
2707 break;
2708 }
2709 case ARM::t2SUBrr: {
2710 if (Commute)
2711 return false;
2712 ImmVal = -ImmVal;
2713 NewUseOpc = ARM::t2SUBri;
2714 // Fallthrough
2715 }
2716 case ARM::t2ADDrr:
2717 case ARM::t2ORRrr:
2718 case ARM::t2EORrr: {
2719 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2720 return false;
2721 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2722 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2723 switch (UseOpc) {
2724 default: break;
2725 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2726 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2727 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2728 }
2729 break;
2730 }
2731 }
2732 }
2733 }
2734
2735 unsigned OpIdx = Commute ? 2 : 1;
2736 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2737 bool isKill = UseMI->getOperand(OpIdx).isKill();
2738 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2739 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002740 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002741 get(NewUseOpc), NewReg)
2742 .addReg(Reg1, getKillRegState(isKill))
2743 .addImm(SOImmValV1)));
2744 UseMI->setDesc(get(NewUseOpc));
2745 UseMI->getOperand(1).setReg(NewReg);
2746 UseMI->getOperand(1).setIsKill();
2747 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2748 DefMI->eraseFromParent();
2749 return true;
2750}
2751
Bob Wilsone8a549c2012-09-29 21:43:49 +00002752static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2753 const MachineInstr *MI) {
2754 switch (MI->getOpcode()) {
2755 default: {
2756 const MCInstrDesc &Desc = MI->getDesc();
2757 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2758 assert(UOps >= 0 && "bad # UOps");
2759 return UOps;
2760 }
2761
2762 case ARM::LDRrs:
2763 case ARM::LDRBrs:
2764 case ARM::STRrs:
2765 case ARM::STRBrs: {
2766 unsigned ShOpVal = MI->getOperand(3).getImm();
2767 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2768 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2769 if (!isSub &&
2770 (ShImm == 0 ||
2771 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2772 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2773 return 1;
2774 return 2;
2775 }
2776
2777 case ARM::LDRH:
2778 case ARM::STRH: {
2779 if (!MI->getOperand(2).getReg())
2780 return 1;
2781
2782 unsigned ShOpVal = MI->getOperand(3).getImm();
2783 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2784 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2785 if (!isSub &&
2786 (ShImm == 0 ||
2787 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2788 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2789 return 1;
2790 return 2;
2791 }
2792
2793 case ARM::LDRSB:
2794 case ARM::LDRSH:
2795 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2796
2797 case ARM::LDRSB_POST:
2798 case ARM::LDRSH_POST: {
2799 unsigned Rt = MI->getOperand(0).getReg();
2800 unsigned Rm = MI->getOperand(3).getReg();
2801 return (Rt == Rm) ? 4 : 3;
2802 }
2803
2804 case ARM::LDR_PRE_REG:
2805 case ARM::LDRB_PRE_REG: {
2806 unsigned Rt = MI->getOperand(0).getReg();
2807 unsigned Rm = MI->getOperand(3).getReg();
2808 if (Rt == Rm)
2809 return 3;
2810 unsigned ShOpVal = MI->getOperand(4).getImm();
2811 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2812 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2813 if (!isSub &&
2814 (ShImm == 0 ||
2815 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2816 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2817 return 2;
2818 return 3;
2819 }
2820
2821 case ARM::STR_PRE_REG:
2822 case ARM::STRB_PRE_REG: {
2823 unsigned ShOpVal = MI->getOperand(4).getImm();
2824 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2825 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2826 if (!isSub &&
2827 (ShImm == 0 ||
2828 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2829 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2830 return 2;
2831 return 3;
2832 }
2833
2834 case ARM::LDRH_PRE:
2835 case ARM::STRH_PRE: {
2836 unsigned Rt = MI->getOperand(0).getReg();
2837 unsigned Rm = MI->getOperand(3).getReg();
2838 if (!Rm)
2839 return 2;
2840 if (Rt == Rm)
2841 return 3;
2842 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2843 ? 3 : 2;
2844 }
2845
2846 case ARM::LDR_POST_REG:
2847 case ARM::LDRB_POST_REG:
2848 case ARM::LDRH_POST: {
2849 unsigned Rt = MI->getOperand(0).getReg();
2850 unsigned Rm = MI->getOperand(3).getReg();
2851 return (Rt == Rm) ? 3 : 2;
2852 }
2853
2854 case ARM::LDR_PRE_IMM:
2855 case ARM::LDRB_PRE_IMM:
2856 case ARM::LDR_POST_IMM:
2857 case ARM::LDRB_POST_IMM:
2858 case ARM::STRB_POST_IMM:
2859 case ARM::STRB_POST_REG:
2860 case ARM::STRB_PRE_IMM:
2861 case ARM::STRH_POST:
2862 case ARM::STR_POST_IMM:
2863 case ARM::STR_POST_REG:
2864 case ARM::STR_PRE_IMM:
2865 return 2;
2866
2867 case ARM::LDRSB_PRE:
2868 case ARM::LDRSH_PRE: {
2869 unsigned Rm = MI->getOperand(3).getReg();
2870 if (Rm == 0)
2871 return 3;
2872 unsigned Rt = MI->getOperand(0).getReg();
2873 if (Rt == Rm)
2874 return 4;
2875 unsigned ShOpVal = MI->getOperand(4).getImm();
2876 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2877 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2878 if (!isSub &&
2879 (ShImm == 0 ||
2880 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2881 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2882 return 3;
2883 return 4;
2884 }
2885
2886 case ARM::LDRD: {
2887 unsigned Rt = MI->getOperand(0).getReg();
2888 unsigned Rn = MI->getOperand(2).getReg();
2889 unsigned Rm = MI->getOperand(3).getReg();
2890 if (Rm)
2891 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2892 return (Rt == Rn) ? 3 : 2;
2893 }
2894
2895 case ARM::STRD: {
2896 unsigned Rm = MI->getOperand(3).getReg();
2897 if (Rm)
2898 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2899 return 2;
2900 }
2901
2902 case ARM::LDRD_POST:
2903 case ARM::t2LDRD_POST:
2904 return 3;
2905
2906 case ARM::STRD_POST:
2907 case ARM::t2STRD_POST:
2908 return 4;
2909
2910 case ARM::LDRD_PRE: {
2911 unsigned Rt = MI->getOperand(0).getReg();
2912 unsigned Rn = MI->getOperand(3).getReg();
2913 unsigned Rm = MI->getOperand(4).getReg();
2914 if (Rm)
2915 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2916 return (Rt == Rn) ? 4 : 3;
2917 }
2918
2919 case ARM::t2LDRD_PRE: {
2920 unsigned Rt = MI->getOperand(0).getReg();
2921 unsigned Rn = MI->getOperand(3).getReg();
2922 return (Rt == Rn) ? 4 : 3;
2923 }
2924
2925 case ARM::STRD_PRE: {
2926 unsigned Rm = MI->getOperand(4).getReg();
2927 if (Rm)
2928 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2929 return 3;
2930 }
2931
2932 case ARM::t2STRD_PRE:
2933 return 3;
2934
2935 case ARM::t2LDR_POST:
2936 case ARM::t2LDRB_POST:
2937 case ARM::t2LDRB_PRE:
2938 case ARM::t2LDRSBi12:
2939 case ARM::t2LDRSBi8:
2940 case ARM::t2LDRSBpci:
2941 case ARM::t2LDRSBs:
2942 case ARM::t2LDRH_POST:
2943 case ARM::t2LDRH_PRE:
2944 case ARM::t2LDRSBT:
2945 case ARM::t2LDRSB_POST:
2946 case ARM::t2LDRSB_PRE:
2947 case ARM::t2LDRSH_POST:
2948 case ARM::t2LDRSH_PRE:
2949 case ARM::t2LDRSHi12:
2950 case ARM::t2LDRSHi8:
2951 case ARM::t2LDRSHpci:
2952 case ARM::t2LDRSHs:
2953 return 2;
2954
2955 case ARM::t2LDRDi8: {
2956 unsigned Rt = MI->getOperand(0).getReg();
2957 unsigned Rn = MI->getOperand(2).getReg();
2958 return (Rt == Rn) ? 3 : 2;
2959 }
2960
2961 case ARM::t2STRB_POST:
2962 case ARM::t2STRB_PRE:
2963 case ARM::t2STRBs:
2964 case ARM::t2STRDi8:
2965 case ARM::t2STRH_POST:
2966 case ARM::t2STRH_PRE:
2967 case ARM::t2STRHs:
2968 case ARM::t2STR_POST:
2969 case ARM::t2STR_PRE:
2970 case ARM::t2STRs:
2971 return 2;
2972 }
2973}
2974
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002975// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2976// can't be easily determined return 0 (missing MachineMemOperand).
2977//
2978// FIXME: The current MachineInstr design does not support relying on machine
2979// mem operands to determine the width of a memory access. Instead, we expect
2980// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002981// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002982// two reasons:
2983//
2984// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2985// operands. This is much more dangerous than using the MachineMemOperand
2986// sizes because CodeGen passes can insert/remove optional machine operands. In
2987// fact, it's totally incorrect for preRA passes and appears to be wrong for
2988// postRA passes as well.
2989//
2990// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2991// machine model that calls this should handle the unknown (zero size) case.
2992//
2993// Long term, we should require a target hook that verifies MachineMemOperand
2994// sizes during MC lowering. That target hook should be local to MC lowering
2995// because we can't ensure that it is aware of other MI forms. Doing this will
2996// ensure that MachineMemOperands are correctly propagated through all passes.
2997unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2998 unsigned Size = 0;
2999 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
3000 E = MI->memoperands_end(); I != E; ++I) {
3001 Size += (*I)->getSize();
3002 }
3003 return Size / 4;
3004}
3005
Evan Cheng367a5df2010-09-09 18:18:55 +00003006unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00003007ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3008 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003009 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003010 return 1;
3011
Evan Cheng6cc775f2011-06-28 19:10:37 +00003012 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003013 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003014 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003015 if (ItinUOps >= 0) {
3016 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3017 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3018
Andrew Trickf161e392012-07-02 18:10:42 +00003019 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003020 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003021
3022 unsigned Opc = MI->getOpcode();
3023 switch (Opc) {
3024 default:
3025 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003026 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003027 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003028 return 2;
3029
3030 // The number of uOps for load / store multiple are determined by the number
3031 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003032 //
Evan Chengbf407072010-09-10 01:29:16 +00003033 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3034 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003035 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003036 //
Evan Chengbf407072010-09-10 01:29:16 +00003037 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003038 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3039 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3040 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003041 case ARM::VLDMDIA_UPD:
3042 case ARM::VLDMDDB_UPD:
3043 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003044 case ARM::VLDMSIA_UPD:
3045 case ARM::VLDMSDB_UPD:
3046 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003047 case ARM::VSTMDIA_UPD:
3048 case ARM::VSTMDDB_UPD:
3049 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003050 case ARM::VSTMSIA_UPD:
3051 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003052 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3053 return (NumRegs / 2) + (NumRegs % 2) + 1;
3054 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003055
3056 case ARM::LDMIA_RET:
3057 case ARM::LDMIA:
3058 case ARM::LDMDA:
3059 case ARM::LDMDB:
3060 case ARM::LDMIB:
3061 case ARM::LDMIA_UPD:
3062 case ARM::LDMDA_UPD:
3063 case ARM::LDMDB_UPD:
3064 case ARM::LDMIB_UPD:
3065 case ARM::STMIA:
3066 case ARM::STMDA:
3067 case ARM::STMDB:
3068 case ARM::STMIB:
3069 case ARM::STMIA_UPD:
3070 case ARM::STMDA_UPD:
3071 case ARM::STMDB_UPD:
3072 case ARM::STMIB_UPD:
3073 case ARM::tLDMIA:
3074 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003075 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003076 case ARM::tPOP_RET:
3077 case ARM::tPOP:
3078 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003079 case ARM::t2LDMIA_RET:
3080 case ARM::t2LDMIA:
3081 case ARM::t2LDMDB:
3082 case ARM::t2LDMIA_UPD:
3083 case ARM::t2LDMDB_UPD:
3084 case ARM::t2STMIA:
3085 case ARM::t2STMDB:
3086 case ARM::t2STMIA_UPD:
3087 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003088 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003089 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003090 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3091 switch (Opc) {
3092 default: break;
3093 case ARM::VLDMDIA_UPD:
3094 case ARM::VLDMDDB_UPD:
3095 case ARM::VLDMSIA_UPD:
3096 case ARM::VLDMSDB_UPD:
3097 case ARM::VSTMDIA_UPD:
3098 case ARM::VSTMDDB_UPD:
3099 case ARM::VSTMSIA_UPD:
3100 case ARM::VSTMSDB_UPD:
3101 case ARM::LDMIA_UPD:
3102 case ARM::LDMDA_UPD:
3103 case ARM::LDMDB_UPD:
3104 case ARM::LDMIB_UPD:
3105 case ARM::STMIA_UPD:
3106 case ARM::STMDA_UPD:
3107 case ARM::STMDB_UPD:
3108 case ARM::STMIB_UPD:
3109 case ARM::tLDMIA_UPD:
3110 case ARM::tSTMIA_UPD:
3111 case ARM::t2LDMIA_UPD:
3112 case ARM::t2LDMDB_UPD:
3113 case ARM::t2STMIA_UPD:
3114 case ARM::t2STMDB_UPD:
3115 ++UOps; // One for base register writeback.
3116 break;
3117 case ARM::LDMIA_RET:
3118 case ARM::tPOP_RET:
3119 case ARM::t2LDMIA_RET:
3120 UOps += 2; // One for base reg wb, one for write to pc.
3121 break;
3122 }
3123 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003124 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003125 if (NumRegs < 4)
3126 return 2;
3127 // 4 registers would be issued: 2, 2.
3128 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003129 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003130 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003131 ++A8UOps;
3132 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003133 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003134 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003135 // If there are odd number of registers or if it's not 64-bit aligned,
3136 // then it takes an extra AGU (Address Generation Unit) cycle.
3137 if ((NumRegs % 2) ||
3138 !MI->hasOneMemOperand() ||
3139 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003140 ++A9UOps;
3141 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003142 } else {
3143 // Assume the worst.
3144 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003145 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003146 }
3147 }
3148}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003149
3150int
Evan Cheng412e37b2010-10-07 23:12:15 +00003151ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003152 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003153 unsigned DefClass,
3154 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003155 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003156 if (RegNo <= 0)
3157 // Def is the address writeback.
3158 return ItinData->getOperandCycle(DefClass, DefIdx);
3159
3160 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003161 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003162 // (regno / 2) + (regno % 2) + 1
3163 DefCycle = RegNo / 2 + 1;
3164 if (RegNo % 2)
3165 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003166 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003167 DefCycle = RegNo;
3168 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003169
Evan Cheng6cc775f2011-06-28 19:10:37 +00003170 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003171 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003172 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003173 case ARM::VLDMSIA_UPD:
3174 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003175 isSLoad = true;
3176 break;
3177 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003178
Evan Cheng412e37b2010-10-07 23:12:15 +00003179 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3180 // then it takes an extra cycle.
3181 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3182 ++DefCycle;
3183 } else {
3184 // Assume the worst.
3185 DefCycle = RegNo + 2;
3186 }
3187
3188 return DefCycle;
3189}
3190
3191int
3192ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003193 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003194 unsigned DefClass,
3195 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003196 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003197 if (RegNo <= 0)
3198 // Def is the address writeback.
3199 return ItinData->getOperandCycle(DefClass, DefIdx);
3200
3201 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003202 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003203 // 4 registers would be issued: 1, 2, 1.
3204 // 5 registers would be issued: 1, 2, 2.
3205 DefCycle = RegNo / 2;
3206 if (DefCycle < 1)
3207 DefCycle = 1;
3208 // Result latency is issue cycle + 2: E2.
3209 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003210 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003211 DefCycle = (RegNo / 2);
3212 // If there are odd number of registers or if it's not 64-bit aligned,
3213 // then it takes an extra AGU (Address Generation Unit) cycle.
3214 if ((RegNo % 2) || DefAlign < 8)
3215 ++DefCycle;
3216 // Result latency is AGU cycles + 2.
3217 DefCycle += 2;
3218 } else {
3219 // Assume the worst.
3220 DefCycle = RegNo + 2;
3221 }
3222
3223 return DefCycle;
3224}
3225
3226int
3227ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003228 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003229 unsigned UseClass,
3230 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003231 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003232 if (RegNo <= 0)
3233 return ItinData->getOperandCycle(UseClass, UseIdx);
3234
3235 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003236 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003237 // (regno / 2) + (regno % 2) + 1
3238 UseCycle = RegNo / 2 + 1;
3239 if (RegNo % 2)
3240 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003241 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003242 UseCycle = RegNo;
3243 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003244
Evan Cheng6cc775f2011-06-28 19:10:37 +00003245 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003246 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003247 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003248 case ARM::VSTMSIA_UPD:
3249 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003250 isSStore = true;
3251 break;
3252 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003253
Evan Cheng412e37b2010-10-07 23:12:15 +00003254 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3255 // then it takes an extra cycle.
3256 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3257 ++UseCycle;
3258 } else {
3259 // Assume the worst.
3260 UseCycle = RegNo + 2;
3261 }
3262
3263 return UseCycle;
3264}
3265
3266int
3267ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003268 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003269 unsigned UseClass,
3270 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003271 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003272 if (RegNo <= 0)
3273 return ItinData->getOperandCycle(UseClass, UseIdx);
3274
3275 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003276 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003277 UseCycle = RegNo / 2;
3278 if (UseCycle < 2)
3279 UseCycle = 2;
3280 // Read in E3.
3281 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003282 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003283 UseCycle = (RegNo / 2);
3284 // If there are odd number of registers or if it's not 64-bit aligned,
3285 // then it takes an extra AGU (Address Generation Unit) cycle.
3286 if ((RegNo % 2) || UseAlign < 8)
3287 ++UseCycle;
3288 } else {
3289 // Assume the worst.
3290 UseCycle = 1;
3291 }
3292 return UseCycle;
3293}
3294
3295int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003296ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003297 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003298 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003299 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003300 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003301 unsigned DefClass = DefMCID.getSchedClass();
3302 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003303
Evan Cheng6cc775f2011-06-28 19:10:37 +00003304 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003305 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3306
3307 // This may be a def / use of a variable_ops instruction, the operand
3308 // latency might be determinable dynamically. Let the target try to
3309 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003310 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003311 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003312 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003313 default:
3314 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3315 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003316
3317 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003318 case ARM::VLDMDIA_UPD:
3319 case ARM::VLDMDDB_UPD:
3320 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003321 case ARM::VLDMSIA_UPD:
3322 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003323 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003324 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003325
3326 case ARM::LDMIA_RET:
3327 case ARM::LDMIA:
3328 case ARM::LDMDA:
3329 case ARM::LDMDB:
3330 case ARM::LDMIB:
3331 case ARM::LDMIA_UPD:
3332 case ARM::LDMDA_UPD:
3333 case ARM::LDMDB_UPD:
3334 case ARM::LDMIB_UPD:
3335 case ARM::tLDMIA:
3336 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003337 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003338 case ARM::t2LDMIA_RET:
3339 case ARM::t2LDMIA:
3340 case ARM::t2LDMDB:
3341 case ARM::t2LDMIA_UPD:
3342 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003343 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003344 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003345 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003346 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003347
3348 if (DefCycle == -1)
3349 // We can't seem to determine the result latency of the def, assume it's 2.
3350 DefCycle = 2;
3351
3352 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003353 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003354 default:
3355 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3356 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003357
3358 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003359 case ARM::VSTMDIA_UPD:
3360 case ARM::VSTMDDB_UPD:
3361 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003362 case ARM::VSTMSIA_UPD:
3363 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003364 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003365 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003366
3367 case ARM::STMIA:
3368 case ARM::STMDA:
3369 case ARM::STMDB:
3370 case ARM::STMIB:
3371 case ARM::STMIA_UPD:
3372 case ARM::STMDA_UPD:
3373 case ARM::STMDB_UPD:
3374 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003375 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003376 case ARM::tPOP_RET:
3377 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003378 case ARM::t2STMIA:
3379 case ARM::t2STMDB:
3380 case ARM::t2STMIA_UPD:
3381 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003382 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003383 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003384 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003385
3386 if (UseCycle == -1)
3387 // Assume it's read in the first stage.
3388 UseCycle = 1;
3389
3390 UseCycle = DefCycle - UseCycle + 1;
3391 if (UseCycle > 0) {
3392 if (LdmBypass) {
3393 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3394 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003395 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003396 UseClass, UseIdx))
3397 --UseCycle;
3398 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003399 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003400 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003401 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003402 }
3403
3404 return UseCycle;
3405}
3406
Evan Cheng7fae11b2011-12-14 02:11:42 +00003407static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003408 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003409 unsigned &DefIdx, unsigned &Dist) {
3410 Dist = 0;
3411
3412 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003413 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003414 assert(II->isInsideBundle() && "Empty bundle?");
3415
3416 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003417 while (II->isInsideBundle()) {
3418 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3419 if (Idx != -1)
3420 break;
3421 --II;
3422 ++Dist;
3423 }
3424
3425 assert(Idx != -1 && "Cannot find bundled definition!");
3426 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003427 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003428}
3429
3430static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003431 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003432 unsigned &UseIdx, unsigned &Dist) {
3433 Dist = 0;
3434
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00003435 MachineBasicBlock::const_instr_iterator II = ++MI->getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003436 assert(II->isInsideBundle() && "Empty bundle?");
3437 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3438
3439 // FIXME: This doesn't properly handle multiple uses.
3440 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003441 while (II != E && II->isInsideBundle()) {
3442 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3443 if (Idx != -1)
3444 break;
3445 if (II->getOpcode() != ARM::t2IT)
3446 ++Dist;
3447 ++II;
3448 }
3449
Evan Chengda103bf2011-12-14 20:00:08 +00003450 if (Idx == -1) {
3451 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003452 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003453 }
3454
Evan Cheng7fae11b2011-12-14 02:11:42 +00003455 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003456 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003457}
3458
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003459/// Return the number of cycles to add to (or subtract from) the static
3460/// itinerary based on the def opcode and alignment. The caller will ensure that
3461/// adjusted latency is at least one cycle.
3462static int adjustDefLatency(const ARMSubtarget &Subtarget,
3463 const MachineInstr *DefMI,
3464 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3465 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003466 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003467 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3468 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003469 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003470 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003471 case ARM::LDRrs:
3472 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003473 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3474 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3475 if (ShImm == 0 ||
3476 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003477 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003478 break;
3479 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003480 case ARM::t2LDRs:
3481 case ARM::t2LDRBs:
3482 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003483 case ARM::t2LDRSHs: {
3484 // Thumb2 mode: lsl only.
3485 unsigned ShAmt = DefMI->getOperand(3).getImm();
3486 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003487 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003488 break;
3489 }
3490 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003491 } else if (Subtarget.isSwift()) {
3492 // FIXME: Properly handle all of the latency adjustments for address
3493 // writeback.
3494 switch (DefMCID->getOpcode()) {
3495 default: break;
3496 case ARM::LDRrs:
3497 case ARM::LDRBrs: {
3498 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3499 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3500 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3501 if (!isSub &&
3502 (ShImm == 0 ||
3503 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3504 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3505 Adjust -= 2;
3506 else if (!isSub &&
3507 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3508 --Adjust;
3509 break;
3510 }
3511 case ARM::t2LDRs:
3512 case ARM::t2LDRBs:
3513 case ARM::t2LDRHs:
3514 case ARM::t2LDRSHs: {
3515 // Thumb2 mode: lsl only.
3516 unsigned ShAmt = DefMI->getOperand(3).getImm();
3517 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3518 Adjust -= 2;
3519 break;
3520 }
3521 }
Evan Chengff310732010-10-28 06:47:08 +00003522 }
3523
Silviu Barangab47bb942012-09-13 15:05:10 +00003524 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003525 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003526 default: break;
3527 case ARM::VLD1q8:
3528 case ARM::VLD1q16:
3529 case ARM::VLD1q32:
3530 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003531 case ARM::VLD1q8wb_fixed:
3532 case ARM::VLD1q16wb_fixed:
3533 case ARM::VLD1q32wb_fixed:
3534 case ARM::VLD1q64wb_fixed:
3535 case ARM::VLD1q8wb_register:
3536 case ARM::VLD1q16wb_register:
3537 case ARM::VLD1q32wb_register:
3538 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003539 case ARM::VLD2d8:
3540 case ARM::VLD2d16:
3541 case ARM::VLD2d32:
3542 case ARM::VLD2q8:
3543 case ARM::VLD2q16:
3544 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003545 case ARM::VLD2d8wb_fixed:
3546 case ARM::VLD2d16wb_fixed:
3547 case ARM::VLD2d32wb_fixed:
3548 case ARM::VLD2q8wb_fixed:
3549 case ARM::VLD2q16wb_fixed:
3550 case ARM::VLD2q32wb_fixed:
3551 case ARM::VLD2d8wb_register:
3552 case ARM::VLD2d16wb_register:
3553 case ARM::VLD2d32wb_register:
3554 case ARM::VLD2q8wb_register:
3555 case ARM::VLD2q16wb_register:
3556 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003557 case ARM::VLD3d8:
3558 case ARM::VLD3d16:
3559 case ARM::VLD3d32:
3560 case ARM::VLD1d64T:
3561 case ARM::VLD3d8_UPD:
3562 case ARM::VLD3d16_UPD:
3563 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003564 case ARM::VLD1d64Twb_fixed:
3565 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003566 case ARM::VLD3q8_UPD:
3567 case ARM::VLD3q16_UPD:
3568 case ARM::VLD3q32_UPD:
3569 case ARM::VLD4d8:
3570 case ARM::VLD4d16:
3571 case ARM::VLD4d32:
3572 case ARM::VLD1d64Q:
3573 case ARM::VLD4d8_UPD:
3574 case ARM::VLD4d16_UPD:
3575 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003576 case ARM::VLD1d64Qwb_fixed:
3577 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003578 case ARM::VLD4q8_UPD:
3579 case ARM::VLD4q16_UPD:
3580 case ARM::VLD4q32_UPD:
3581 case ARM::VLD1DUPq8:
3582 case ARM::VLD1DUPq16:
3583 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003584 case ARM::VLD1DUPq8wb_fixed:
3585 case ARM::VLD1DUPq16wb_fixed:
3586 case ARM::VLD1DUPq32wb_fixed:
3587 case ARM::VLD1DUPq8wb_register:
3588 case ARM::VLD1DUPq16wb_register:
3589 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003590 case ARM::VLD2DUPd8:
3591 case ARM::VLD2DUPd16:
3592 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003593 case ARM::VLD2DUPd8wb_fixed:
3594 case ARM::VLD2DUPd16wb_fixed:
3595 case ARM::VLD2DUPd32wb_fixed:
3596 case ARM::VLD2DUPd8wb_register:
3597 case ARM::VLD2DUPd16wb_register:
3598 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003599 case ARM::VLD4DUPd8:
3600 case ARM::VLD4DUPd16:
3601 case ARM::VLD4DUPd32:
3602 case ARM::VLD4DUPd8_UPD:
3603 case ARM::VLD4DUPd16_UPD:
3604 case ARM::VLD4DUPd32_UPD:
3605 case ARM::VLD1LNd8:
3606 case ARM::VLD1LNd16:
3607 case ARM::VLD1LNd32:
3608 case ARM::VLD1LNd8_UPD:
3609 case ARM::VLD1LNd16_UPD:
3610 case ARM::VLD1LNd32_UPD:
3611 case ARM::VLD2LNd8:
3612 case ARM::VLD2LNd16:
3613 case ARM::VLD2LNd32:
3614 case ARM::VLD2LNq16:
3615 case ARM::VLD2LNq32:
3616 case ARM::VLD2LNd8_UPD:
3617 case ARM::VLD2LNd16_UPD:
3618 case ARM::VLD2LNd32_UPD:
3619 case ARM::VLD2LNq16_UPD:
3620 case ARM::VLD2LNq32_UPD:
3621 case ARM::VLD4LNd8:
3622 case ARM::VLD4LNd16:
3623 case ARM::VLD4LNd32:
3624 case ARM::VLD4LNq16:
3625 case ARM::VLD4LNq32:
3626 case ARM::VLD4LNd8_UPD:
3627 case ARM::VLD4LNd16_UPD:
3628 case ARM::VLD4LNd32_UPD:
3629 case ARM::VLD4LNq16_UPD:
3630 case ARM::VLD4LNq32_UPD:
3631 // If the address is not 64-bit aligned, the latencies of these
3632 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003633 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003634 break;
3635 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003636 }
3637 return Adjust;
3638}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003639
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003640
3641
3642int
3643ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3644 const MachineInstr *DefMI, unsigned DefIdx,
3645 const MachineInstr *UseMI,
3646 unsigned UseIdx) const {
3647 // No operand latency. The caller may fall back to getInstrLatency.
3648 if (!ItinData || ItinData->isEmpty())
3649 return -1;
3650
3651 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3652 unsigned Reg = DefMO.getReg();
3653 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3654 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3655
3656 unsigned DefAdj = 0;
3657 if (DefMI->isBundle()) {
3658 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3659 DefMCID = &DefMI->getDesc();
3660 }
3661 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3662 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3663 return 1;
3664 }
3665
3666 unsigned UseAdj = 0;
3667 if (UseMI->isBundle()) {
3668 unsigned NewUseIdx;
3669 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3670 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003671 if (!NewUseMI)
3672 return -1;
3673
3674 UseMI = NewUseMI;
3675 UseIdx = NewUseIdx;
3676 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003677 }
3678
3679 if (Reg == ARM::CPSR) {
3680 if (DefMI->getOpcode() == ARM::FMSTAT) {
3681 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003682 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003683 }
3684
3685 // CPSR set and branch can be paired in the same cycle.
3686 if (UseMI->isBranch())
3687 return 0;
3688
3689 // Otherwise it takes the instruction latency (generally one).
3690 unsigned Latency = getInstrLatency(ItinData, DefMI);
3691
3692 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3693 // its uses. Instructions which are otherwise scheduled between them may
3694 // incur a code size penalty (not able to use the CPSR setting 16-bit
3695 // instructions).
3696 if (Latency > 0 && Subtarget.isThumb2()) {
3697 const MachineFunction *MF = DefMI->getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003698 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003699 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003700 --Latency;
3701 }
3702 return Latency;
3703 }
3704
Andrew Trick77d0b882012-06-22 02:50:33 +00003705 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3706 return -1;
3707
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003708 unsigned DefAlign = DefMI->hasOneMemOperand()
3709 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3710 unsigned UseAlign = UseMI->hasOneMemOperand()
3711 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3712
3713 // Get the itinerary's latency if possible, and handle variable_ops.
3714 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3715 *UseMCID, UseIdx, UseAlign);
3716 // Unable to find operand latency. The caller may resort to getInstrLatency.
3717 if (Latency < 0)
3718 return Latency;
3719
3720 // Adjust for IT block position.
3721 int Adj = DefAdj + UseAdj;
3722
3723 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3724 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3725 if (Adj >= 0 || (int)Latency > -Adj) {
3726 return Latency + Adj;
3727 }
3728 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003729 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003730}
3731
3732int
3733ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3734 SDNode *DefNode, unsigned DefIdx,
3735 SDNode *UseNode, unsigned UseIdx) const {
3736 if (!DefNode->isMachineOpcode())
3737 return 1;
3738
Evan Cheng6cc775f2011-06-28 19:10:37 +00003739 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003740
Evan Cheng6cc775f2011-06-28 19:10:37 +00003741 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003742 return 0;
3743
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003744 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003745 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003746
Evan Cheng6c1414f2010-10-29 18:09:28 +00003747 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003748 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003749 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003750 return Latency <= 2 ? 1 : Latency - 1;
3751 else
3752 return Latency <= 3 ? 1 : Latency - 2;
3753 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003754
Evan Cheng6cc775f2011-06-28 19:10:37 +00003755 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003756 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3757 unsigned DefAlign = !DefMN->memoperands_empty()
3758 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3759 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3760 unsigned UseAlign = !UseMN->memoperands_empty()
3761 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003762 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3763 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003764
3765 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003766 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3767 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003768 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3769 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003770 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003771 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003772 case ARM::LDRrs:
3773 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003774 unsigned ShOpVal =
3775 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3776 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3777 if (ShImm == 0 ||
3778 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3779 --Latency;
3780 break;
3781 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003782 case ARM::t2LDRs:
3783 case ARM::t2LDRBs:
3784 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003785 case ARM::t2LDRSHs: {
3786 // Thumb2 mode: lsl only.
3787 unsigned ShAmt =
3788 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3789 if (ShAmt == 0 || ShAmt == 2)
3790 --Latency;
3791 break;
3792 }
3793 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003794 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3795 // FIXME: Properly handle all of the latency adjustments for address
3796 // writeback.
3797 switch (DefMCID.getOpcode()) {
3798 default: break;
3799 case ARM::LDRrs:
3800 case ARM::LDRBrs: {
3801 unsigned ShOpVal =
3802 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3803 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3804 if (ShImm == 0 ||
3805 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3806 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3807 Latency -= 2;
3808 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3809 --Latency;
3810 break;
3811 }
3812 case ARM::t2LDRs:
3813 case ARM::t2LDRBs:
3814 case ARM::t2LDRHs:
3815 case ARM::t2LDRSHs: {
3816 // Thumb2 mode: lsl 0-3 only.
3817 Latency -= 2;
3818 break;
3819 }
3820 }
Evan Chengff310732010-10-28 06:47:08 +00003821 }
3822
Silviu Barangab47bb942012-09-13 15:05:10 +00003823 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003824 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003825 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003826 case ARM::VLD1q8:
3827 case ARM::VLD1q16:
3828 case ARM::VLD1q32:
3829 case ARM::VLD1q64:
3830 case ARM::VLD1q8wb_register:
3831 case ARM::VLD1q16wb_register:
3832 case ARM::VLD1q32wb_register:
3833 case ARM::VLD1q64wb_register:
3834 case ARM::VLD1q8wb_fixed:
3835 case ARM::VLD1q16wb_fixed:
3836 case ARM::VLD1q32wb_fixed:
3837 case ARM::VLD1q64wb_fixed:
3838 case ARM::VLD2d8:
3839 case ARM::VLD2d16:
3840 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003841 case ARM::VLD2q8Pseudo:
3842 case ARM::VLD2q16Pseudo:
3843 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003844 case ARM::VLD2d8wb_fixed:
3845 case ARM::VLD2d16wb_fixed:
3846 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003847 case ARM::VLD2q8PseudoWB_fixed:
3848 case ARM::VLD2q16PseudoWB_fixed:
3849 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003850 case ARM::VLD2d8wb_register:
3851 case ARM::VLD2d16wb_register:
3852 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003853 case ARM::VLD2q8PseudoWB_register:
3854 case ARM::VLD2q16PseudoWB_register:
3855 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003856 case ARM::VLD3d8Pseudo:
3857 case ARM::VLD3d16Pseudo:
3858 case ARM::VLD3d32Pseudo:
3859 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003860 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003861 case ARM::VLD3d8Pseudo_UPD:
3862 case ARM::VLD3d16Pseudo_UPD:
3863 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003864 case ARM::VLD3q8Pseudo_UPD:
3865 case ARM::VLD3q16Pseudo_UPD:
3866 case ARM::VLD3q32Pseudo_UPD:
3867 case ARM::VLD3q8oddPseudo:
3868 case ARM::VLD3q16oddPseudo:
3869 case ARM::VLD3q32oddPseudo:
3870 case ARM::VLD3q8oddPseudo_UPD:
3871 case ARM::VLD3q16oddPseudo_UPD:
3872 case ARM::VLD3q32oddPseudo_UPD:
3873 case ARM::VLD4d8Pseudo:
3874 case ARM::VLD4d16Pseudo:
3875 case ARM::VLD4d32Pseudo:
3876 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003877 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003878 case ARM::VLD4d8Pseudo_UPD:
3879 case ARM::VLD4d16Pseudo_UPD:
3880 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003881 case ARM::VLD4q8Pseudo_UPD:
3882 case ARM::VLD4q16Pseudo_UPD:
3883 case ARM::VLD4q32Pseudo_UPD:
3884 case ARM::VLD4q8oddPseudo:
3885 case ARM::VLD4q16oddPseudo:
3886 case ARM::VLD4q32oddPseudo:
3887 case ARM::VLD4q8oddPseudo_UPD:
3888 case ARM::VLD4q16oddPseudo_UPD:
3889 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003890 case ARM::VLD1DUPq8:
3891 case ARM::VLD1DUPq16:
3892 case ARM::VLD1DUPq32:
3893 case ARM::VLD1DUPq8wb_fixed:
3894 case ARM::VLD1DUPq16wb_fixed:
3895 case ARM::VLD1DUPq32wb_fixed:
3896 case ARM::VLD1DUPq8wb_register:
3897 case ARM::VLD1DUPq16wb_register:
3898 case ARM::VLD1DUPq32wb_register:
3899 case ARM::VLD2DUPd8:
3900 case ARM::VLD2DUPd16:
3901 case ARM::VLD2DUPd32:
3902 case ARM::VLD2DUPd8wb_fixed:
3903 case ARM::VLD2DUPd16wb_fixed:
3904 case ARM::VLD2DUPd32wb_fixed:
3905 case ARM::VLD2DUPd8wb_register:
3906 case ARM::VLD2DUPd16wb_register:
3907 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003908 case ARM::VLD4DUPd8Pseudo:
3909 case ARM::VLD4DUPd16Pseudo:
3910 case ARM::VLD4DUPd32Pseudo:
3911 case ARM::VLD4DUPd8Pseudo_UPD:
3912 case ARM::VLD4DUPd16Pseudo_UPD:
3913 case ARM::VLD4DUPd32Pseudo_UPD:
3914 case ARM::VLD1LNq8Pseudo:
3915 case ARM::VLD1LNq16Pseudo:
3916 case ARM::VLD1LNq32Pseudo:
3917 case ARM::VLD1LNq8Pseudo_UPD:
3918 case ARM::VLD1LNq16Pseudo_UPD:
3919 case ARM::VLD1LNq32Pseudo_UPD:
3920 case ARM::VLD2LNd8Pseudo:
3921 case ARM::VLD2LNd16Pseudo:
3922 case ARM::VLD2LNd32Pseudo:
3923 case ARM::VLD2LNq16Pseudo:
3924 case ARM::VLD2LNq32Pseudo:
3925 case ARM::VLD2LNd8Pseudo_UPD:
3926 case ARM::VLD2LNd16Pseudo_UPD:
3927 case ARM::VLD2LNd32Pseudo_UPD:
3928 case ARM::VLD2LNq16Pseudo_UPD:
3929 case ARM::VLD2LNq32Pseudo_UPD:
3930 case ARM::VLD4LNd8Pseudo:
3931 case ARM::VLD4LNd16Pseudo:
3932 case ARM::VLD4LNd32Pseudo:
3933 case ARM::VLD4LNq16Pseudo:
3934 case ARM::VLD4LNq32Pseudo:
3935 case ARM::VLD4LNd8Pseudo_UPD:
3936 case ARM::VLD4LNd16Pseudo_UPD:
3937 case ARM::VLD4LNd32Pseudo_UPD:
3938 case ARM::VLD4LNq16Pseudo_UPD:
3939 case ARM::VLD4LNq32Pseudo_UPD:
3940 // If the address is not 64-bit aligned, the latencies of these
3941 // instructions increases by one.
3942 ++Latency;
3943 break;
3944 }
3945
Evan Chengff310732010-10-28 06:47:08 +00003946 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003947}
Evan Cheng63c76082010-10-19 18:58:51 +00003948
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003949unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3950 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3951 MI->isRegSequence() || MI->isImplicitDef())
3952 return 0;
3953
3954 if (MI->isBundle())
3955 return 0;
3956
3957 const MCInstrDesc &MCID = MI->getDesc();
3958
3959 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3960 // When predicated, CPSR is an additional source operand for CPSR updating
3961 // instructions, this apparently increases their latencies.
3962 return 1;
3963 }
3964 return 0;
3965}
3966
Andrew Trick45446062012-06-05 21:11:27 +00003967unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3968 const MachineInstr *MI,
3969 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003970 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3971 MI->isRegSequence() || MI->isImplicitDef())
3972 return 1;
3973
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003974 // An instruction scheduler typically runs on unbundled instructions, however
3975 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003976 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003977 unsigned Latency = 0;
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00003978 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
3979 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003980 while (++I != E && I->isInsideBundle()) {
3981 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003982 Latency += getInstrLatency(ItinData, &*I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00003983 }
3984 return Latency;
3985 }
3986
Evan Cheng6cc775f2011-06-28 19:10:37 +00003987 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003988 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003989 // When predicated, CPSR is an additional source operand for CPSR updating
3990 // instructions, this apparently increases their latencies.
3991 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003992 }
3993 // Be sure to call getStageLatency for an empty itinerary in case it has a
3994 // valid MinLatency property.
3995 if (!ItinData)
3996 return MI->mayLoad() ? 3 : 1;
3997
3998 unsigned Class = MCID.getSchedClass();
3999
4000 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004001 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004002 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004003
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004004 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004005 unsigned Latency = ItinData->getStageLatency(Class);
4006
4007 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4008 unsigned DefAlign = MI->hasOneMemOperand()
4009 ? (*MI->memoperands_begin())->getAlignment() : 0;
4010 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
4011 if (Adj >= 0 || (int)Latency > -Adj) {
4012 return Latency + Adj;
4013 }
4014 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004015}
4016
4017int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4018 SDNode *Node) const {
4019 if (!Node->isMachineOpcode())
4020 return 1;
4021
4022 if (!ItinData || ItinData->isEmpty())
4023 return 1;
4024
4025 unsigned Opcode = Node->getMachineOpcode();
4026 switch (Opcode) {
4027 default:
4028 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004029 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004030 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004031 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004032 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004033}
4034
Evan Cheng63c76082010-10-19 18:58:51 +00004035bool ARMBaseInstrInfo::
Matthias Braun88e21312015-06-13 03:42:11 +00004036hasHighOperandLatency(const TargetSchedModel &SchedModel,
Evan Cheng63c76082010-10-19 18:58:51 +00004037 const MachineRegisterInfo *MRI,
4038 const MachineInstr *DefMI, unsigned DefIdx,
4039 const MachineInstr *UseMI, unsigned UseIdx) const {
4040 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4041 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4042 if (Subtarget.isCortexA8() &&
4043 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4044 // CortexA8 VFP instructions are not pipelined.
4045 return true;
4046
4047 // Hoist VFP / NEON instructions with 4 or higher latency.
Matthias Braun88e21312015-06-13 03:42:11 +00004048 unsigned Latency
4049 = SchedModel.computeOperandLatency(DefMI, DefIdx, UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004050 if (Latency <= 3)
4051 return false;
4052 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4053 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4054}
Evan Chenge96b8d72010-10-26 02:08:50 +00004055
4056bool ARMBaseInstrInfo::
Matthias Braun88e21312015-06-13 03:42:11 +00004057hasLowDefLatency(const TargetSchedModel &SchedModel,
Evan Chenge96b8d72010-10-26 02:08:50 +00004058 const MachineInstr *DefMI, unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004059 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004060 if (!ItinData || ItinData->isEmpty())
4061 return false;
4062
4063 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4064 if (DDomain == ARMII::DomainGeneral) {
4065 unsigned DefClass = DefMI->getDesc().getSchedClass();
4066 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4067 return (DefCycle != -1 && DefCycle <= 2);
4068 }
4069 return false;
4070}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004071
Andrew Trick924123a2011-09-21 02:20:46 +00004072bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4073 StringRef &ErrInfo) const {
4074 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4075 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4076 return false;
4077 }
4078 return true;
4079}
4080
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004081// LoadStackGuard has so far only been implemented for MachO. Different code
4082// sequence is needed for other targets.
4083void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4084 unsigned LoadImmOpc,
4085 unsigned LoadOpc,
4086 Reloc::Model RM) const {
4087 MachineBasicBlock &MBB = *MI->getParent();
4088 DebugLoc DL = MI->getDebugLoc();
4089 unsigned Reg = MI->getOperand(0).getReg();
4090 const GlobalValue *GV =
4091 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4092 MachineInstrBuilder MIB;
4093
4094 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4095 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4096
4097 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4098 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4099 MIB.addReg(Reg, RegState::Kill).addImm(0);
4100 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004101 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
4102 MachinePointerInfo::getGOT(*MBB.getParent()), Flag, 4, 4);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004103 MIB.addMemOperand(MMO);
4104 AddDefaultPred(MIB);
4105 }
4106
4107 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4108 MIB.addReg(Reg, RegState::Kill).addImm(0);
4109 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4110 AddDefaultPred(MIB);
4111}
4112
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004113bool
4114ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4115 unsigned &AddSubOpc,
4116 bool &NegAcc, bool &HasLane) const {
4117 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4118 if (I == MLxEntryMap.end())
4119 return false;
4120
4121 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4122 MulOpc = Entry.MulOpc;
4123 AddSubOpc = Entry.AddSubOpc;
4124 NegAcc = Entry.NegAcc;
4125 HasLane = Entry.HasLane;
4126 return true;
4127}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004128
4129//===----------------------------------------------------------------------===//
4130// Execution domains.
4131//===----------------------------------------------------------------------===//
4132//
4133// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4134// and some can go down both. The vmov instructions go down the VFP pipeline,
4135// but they can be changed to vorr equivalents that are executed by the NEON
4136// pipeline.
4137//
4138// We use the following execution domain numbering:
4139//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004140enum ARMExeDomain {
4141 ExeGeneric = 0,
4142 ExeVFP = 1,
4143 ExeNEON = 2
4144};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004145//
4146// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4147//
4148std::pair<uint16_t, uint16_t>
4149ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004150 // If we don't have access to NEON instructions then we won't be able
4151 // to swizzle anything to the NEON domain. Check to make sure.
4152 if (Subtarget.hasNEON()) {
4153 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4154 // if they are not predicated.
4155 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4156 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004157
Eric Christopher7e70aba2015-03-07 00:12:22 +00004158 // CortexA9 is particularly picky about mixing the two and wants these
4159 // converted.
4160 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4161 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4162 MI->getOpcode() == ARM::VMOVS))
4163 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4164 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004165 // No other instructions can be swizzled, so just determine their domain.
4166 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4167
4168 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004169 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004170
4171 // Certain instructions can go either way on Cortex-A8.
4172 // Treat them as NEON instructions.
4173 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004174 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004175
4176 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004177 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004178
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004179 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004180}
4181
Tim Northover771f1602012-08-29 16:36:07 +00004182static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4183 unsigned SReg, unsigned &Lane) {
4184 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4185 Lane = 0;
4186
4187 if (DReg != ARM::NoRegister)
4188 return DReg;
4189
4190 Lane = 1;
4191 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4192
4193 assert(DReg && "S-register with no D super-register?");
4194 return DReg;
4195}
4196
Andrew Trickd9296ec2012-10-10 05:43:01 +00004197/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004198/// set ImplicitSReg to a register number that must be marked as implicit-use or
4199/// zero if no register needs to be defined as implicit-use.
4200///
4201/// If the function cannot determine if an SPR should be marked implicit use or
4202/// not, it returns false.
4203///
4204/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004205/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004206/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4207/// lane of the DPR).
4208///
4209/// If the other SPR is defined, an implicit-use of it should be added. Else,
4210/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004211///
James Molloyea052562012-09-18 08:31:15 +00004212static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4213 MachineInstr *MI,
4214 unsigned DReg, unsigned Lane,
4215 unsigned &ImplicitSReg) {
4216 // If the DPR is defined or used already, the other SPR lane will be chained
4217 // correctly, so there is nothing to be done.
4218 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4219 ImplicitSReg = 0;
4220 return true;
4221 }
4222
4223 // Otherwise we need to go searching to see if the SPR is set explicitly.
4224 ImplicitSReg = TRI->getSubReg(DReg,
4225 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4226 MachineBasicBlock::LivenessQueryResult LQR =
4227 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4228
4229 if (LQR == MachineBasicBlock::LQR_Live)
4230 return true;
4231 else if (LQR == MachineBasicBlock::LQR_Unknown)
4232 return false;
4233
4234 // If the register is known not to be live, there is no need to add an
4235 // implicit-use.
4236 ImplicitSReg = 0;
4237 return true;
4238}
Tim Northover771f1602012-08-29 16:36:07 +00004239
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004240void
4241ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004242 unsigned DstReg, SrcReg, DReg;
4243 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004244 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004245 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004246 switch (MI->getOpcode()) {
4247 default:
4248 llvm_unreachable("cannot handle opcode!");
4249 break;
4250 case ARM::VMOVD:
4251 if (Domain != ExeNEON)
4252 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004253
Tim Northoverf6618152012-08-17 11:32:52 +00004254 // Zap the predicate operands.
4255 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004256
Eric Christopher7e70aba2015-03-07 00:12:22 +00004257 // Make sure we've got NEON instructions.
4258 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4259
Tim Northover771f1602012-08-29 16:36:07 +00004260 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4261 DstReg = MI->getOperand(0).getReg();
4262 SrcReg = MI->getOperand(1).getReg();
4263
4264 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4265 MI->RemoveOperand(i-1);
4266
4267 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004268 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004269 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4270 .addReg(SrcReg)
4271 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004272 break;
4273 case ARM::VMOVRS:
4274 if (Domain != ExeNEON)
4275 break;
4276 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4277
Tim Northover771f1602012-08-29 16:36:07 +00004278 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004279 DstReg = MI->getOperand(0).getReg();
4280 SrcReg = MI->getOperand(1).getReg();
4281
Tim Northover771f1602012-08-29 16:36:07 +00004282 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4283 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004284
Tim Northover771f1602012-08-29 16:36:07 +00004285 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004286
Tim Northover771f1602012-08-29 16:36:07 +00004287 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4288 // Note that DSrc has been widened and the other lane may be undef, which
4289 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004290 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004291 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4292 .addReg(DReg, RegState::Undef)
4293 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004294
Tim Northover771f1602012-08-29 16:36:07 +00004295 // The old source should be an implicit use, otherwise we might think it
4296 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004297 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004298 break;
James Molloyea052562012-09-18 08:31:15 +00004299 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004300 if (Domain != ExeNEON)
4301 break;
4302 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4303
Tim Northover771f1602012-08-29 16:36:07 +00004304 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004305 DstReg = MI->getOperand(0).getReg();
4306 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004307
Tim Northover771f1602012-08-29 16:36:07 +00004308 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4309
James Molloyea052562012-09-18 08:31:15 +00004310 unsigned ImplicitSReg;
4311 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4312 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004313
Tim Northoverc8d867d2012-09-05 18:37:53 +00004314 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4315 MI->RemoveOperand(i-1);
4316
Tim Northover771f1602012-08-29 16:36:07 +00004317 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4318 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004319 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004320 MIB.addReg(DReg, RegState::Define)
4321 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4322 .addReg(SrcReg)
4323 .addImm(Lane);
4324 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004325
Tim Northover726d32c2012-09-01 18:07:29 +00004326 // The narrower destination must be marked as set to keep previous chains
4327 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004328 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004329 if (ImplicitSReg != 0)
4330 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004331 break;
James Molloyea052562012-09-18 08:31:15 +00004332 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004333 case ARM::VMOVS: {
4334 if (Domain != ExeNEON)
4335 break;
4336
4337 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4338 DstReg = MI->getOperand(0).getReg();
4339 SrcReg = MI->getOperand(1).getReg();
4340
Tim Northoverca9f3842012-08-30 10:17:45 +00004341 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4342 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4343 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4344
James Molloyea052562012-09-18 08:31:15 +00004345 unsigned ImplicitSReg;
4346 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4347 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004348
Tim Northoverc8d867d2012-09-05 18:37:53 +00004349 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4350 MI->RemoveOperand(i-1);
4351
Tim Northoverca9f3842012-08-30 10:17:45 +00004352 if (DSrc == DDst) {
4353 // Destination can be:
4354 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4355 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004356 MIB.addReg(DDst, RegState::Define)
4357 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4358 .addImm(SrcLane);
4359 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004360
4361 // Neither the source or the destination are naturally represented any
4362 // more, so add them in manually.
4363 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4364 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004365 if (ImplicitSReg != 0)
4366 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004367 break;
4368 }
4369
4370 // In general there's no single instruction that can perform an S <-> S
4371 // move in NEON space, but a pair of VEXT instructions *can* do the
4372 // job. It turns out that the VEXTs needed will only use DSrc once, with
4373 // the position based purely on the combination of lane-0 and lane-1
4374 // involved. For example
4375 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4376 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4377 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4378 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4379 //
4380 // Pattern of the MachineInstrs is:
4381 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4382 MachineInstrBuilder NewMIB;
4383 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4384 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004385
4386 // On the first instruction, both DSrc and DDst may be <undef> if present.
4387 // Specifically when the original instruction didn't have them as an
4388 // <imp-use>.
4389 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4390 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4391 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4392
4393 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4394 CurUndef = !MI->readsRegister(CurReg, TRI);
4395 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4396
Tim Northoverca9f3842012-08-30 10:17:45 +00004397 NewMIB.addImm(1);
4398 AddDefaultPred(NewMIB);
4399
4400 if (SrcLane == DstLane)
4401 NewMIB.addReg(SrcReg, RegState::Implicit);
4402
4403 MI->setDesc(get(ARM::VEXTd32));
4404 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004405
4406 // On the second instruction, DDst has definitely been defined above, so
4407 // it is not <undef>. DSrc, if present, can be <undef> as above.
4408 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4409 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4410 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4411
4412 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4413 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4414 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4415
Tim Northoverca9f3842012-08-30 10:17:45 +00004416 MIB.addImm(1);
4417 AddDefaultPred(MIB);
4418
4419 if (SrcLane != DstLane)
4420 MIB.addReg(SrcReg, RegState::Implicit);
4421
4422 // As before, the original destination is no longer represented, add it
4423 // implicitly.
4424 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004425 if (ImplicitSReg != 0)
4426 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004427 break;
4428 }
Tim Northoverf6618152012-08-17 11:32:52 +00004429 }
4430
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004431}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004432
Bob Wilsone8a549c2012-09-29 21:43:49 +00004433//===----------------------------------------------------------------------===//
4434// Partial register updates
4435//===----------------------------------------------------------------------===//
4436//
4437// Swift renames NEON registers with 64-bit granularity. That means any
4438// instruction writing an S-reg implicitly reads the containing D-reg. The
4439// problem is mostly avoided by translating f32 operations to v2f32 operations
4440// on D-registers, but f32 loads are still a problem.
4441//
4442// These instructions can load an f32 into a NEON register:
4443//
4444// VLDRS - Only writes S, partial D update.
4445// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4446// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4447//
4448// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004449unsigned ARMBaseInstrInfo::
4450getPartialRegUpdateClearance(const MachineInstr *MI,
4451 unsigned OpNum,
4452 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004453 if (!SwiftPartialUpdateClearance ||
4454 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004455 return 0;
4456
4457 assert(TRI && "Need TRI instance");
4458
4459 const MachineOperand &MO = MI->getOperand(OpNum);
4460 if (MO.readsReg())
4461 return 0;
4462 unsigned Reg = MO.getReg();
4463 int UseOp = -1;
4464
4465 switch(MI->getOpcode()) {
4466 // Normal instructions writing only an S-register.
4467 case ARM::VLDRS:
4468 case ARM::FCONSTS:
4469 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004470 case ARM::VMOVv8i8:
4471 case ARM::VMOVv4i16:
4472 case ARM::VMOVv2i32:
4473 case ARM::VMOVv2f32:
4474 case ARM::VMOVv1i64:
4475 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4476 break;
4477
4478 // Explicitly reads the dependency.
4479 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004480 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004481 break;
4482 default:
4483 return 0;
4484 }
4485
4486 // If this instruction actually reads a value from Reg, there is no unwanted
4487 // dependency.
4488 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4489 return 0;
4490
4491 // We must be able to clobber the whole D-reg.
4492 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4493 // Virtual register must be a foo:ssub_0<def,undef> operand.
4494 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4495 return 0;
4496 } else if (ARM::SPRRegClass.contains(Reg)) {
4497 // Physical register: MI must define the full D-reg.
4498 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4499 &ARM::DPRRegClass);
4500 if (!DReg || !MI->definesRegister(DReg, TRI))
4501 return 0;
4502 }
4503
4504 // MI has an unwanted D-register dependency.
4505 // Avoid defs in the previous N instructrions.
4506 return SwiftPartialUpdateClearance;
4507}
4508
4509// Break a partial register dependency after getPartialRegUpdateClearance
4510// returned non-zero.
4511void ARMBaseInstrInfo::
4512breakPartialRegDependency(MachineBasicBlock::iterator MI,
4513 unsigned OpNum,
4514 const TargetRegisterInfo *TRI) const {
4515 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4516 assert(TRI && "Need TRI instance");
4517
4518 const MachineOperand &MO = MI->getOperand(OpNum);
4519 unsigned Reg = MO.getReg();
4520 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4521 "Can't break virtual register dependencies.");
4522 unsigned DReg = Reg;
4523
4524 // If MI defines an S-reg, find the corresponding D super-register.
4525 if (ARM::SPRRegClass.contains(Reg)) {
4526 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4527 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4528 }
4529
4530 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4531 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4532
4533 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4534 // the full D-register by loading the same value to both lanes. The
4535 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004536 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004537 // too big regressions.
4538
4539 // Insert the dependency-breaking FCONSTD before MI.
4540 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4541 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4542 get(ARM::FCONSTD), DReg).addImm(96));
4543 MI->addRegisterKilled(DReg, TRI, true);
4544}
4545
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004546bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004547 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004548}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004549
4550bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004551 if (MI->getNumOperands() < 4)
4552 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004553 unsigned ShOpVal = MI->getOperand(3).getImm();
4554 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4555 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4556 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4557 ((ShImm == 1 || ShImm == 2) &&
4558 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4559 return true;
4560
4561 return false;
4562}
Quentin Colombetd358e842014-08-22 18:05:22 +00004563
4564bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4565 const MachineInstr &MI, unsigned DefIdx,
4566 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4567 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4568 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4569
4570 switch (MI.getOpcode()) {
4571 case ARM::VMOVDRR:
4572 // dX = VMOVDRR rY, rZ
4573 // is the same as:
4574 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4575 // Populate the InputRegs accordingly.
4576 // rY
4577 const MachineOperand *MOReg = &MI.getOperand(1);
4578 InputRegs.push_back(
4579 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4580 // rZ
4581 MOReg = &MI.getOperand(2);
4582 InputRegs.push_back(
4583 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4584 return true;
4585 }
4586 llvm_unreachable("Target dependent opcode missing");
4587}
4588
4589bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4590 const MachineInstr &MI, unsigned DefIdx,
4591 RegSubRegPairAndIdx &InputReg) const {
4592 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4593 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4594
4595 switch (MI.getOpcode()) {
4596 case ARM::VMOVRRD:
4597 // rX, rY = VMOVRRD dZ
4598 // is the same as:
4599 // rX = EXTRACT_SUBREG dZ, ssub_0
4600 // rY = EXTRACT_SUBREG dZ, ssub_1
4601 const MachineOperand &MOReg = MI.getOperand(2);
4602 InputReg.Reg = MOReg.getReg();
4603 InputReg.SubReg = MOReg.getSubReg();
4604 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4605 return true;
4606 }
4607 llvm_unreachable("Target dependent opcode missing");
4608}
4609
4610bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4611 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4612 RegSubRegPairAndIdx &InsertedReg) const {
4613 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4614 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4615
4616 switch (MI.getOpcode()) {
4617 case ARM::VSETLNi32:
4618 // dX = VSETLNi32 dY, rZ, imm
4619 const MachineOperand &MOBaseReg = MI.getOperand(1);
4620 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4621 const MachineOperand &MOIndex = MI.getOperand(3);
4622 BaseReg.Reg = MOBaseReg.getReg();
4623 BaseReg.SubReg = MOBaseReg.getSubReg();
4624
4625 InsertedReg.Reg = MOInsertedReg.getReg();
4626 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4627 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4628 return true;
4629 }
4630 llvm_unreachable("Target dependent opcode missing");
4631}