blob: 6aa060ae0d9117405f0739de7b2e10b2e2e071e2 [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
Evan Cheng62c7b5b2010-12-05 22:04:16 +000054/// ARM_MLxEntry - Record information about MLA / MLS instructions.
55struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000056 uint16_t MLxOpc; // MLA / MLS opcode
57 uint16_t MulOpc; // Expanded multiplication opcode
58 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000059 bool NegAcc; // True if the acc is negated before the add / sub.
60 bool HasLane; // True if instruction has an extra "lane" operand.
61};
62
63static const ARM_MLxEntry ARM_MLxTable[] = {
64 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
65 // fp scalar ops
66 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
67 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
68 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
69 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000070 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
71 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
72 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
73 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
74
75 // fp SIMD ops
76 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
77 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
78 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
79 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
80 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
81 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
82 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
83 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
84};
85
Anton Korobeynikov14635da2009-11-02 00:10:38 +000086ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000087 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000088 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000089 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
90 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +000091 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +000092 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
93 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
94 }
95}
96
Andrew Trick10ffc2b2010-12-24 05:03:26 +000097// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
98// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +000099ScheduleHazardRecognizer *
100ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
101 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000102 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000103 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000104 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000105 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
106 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000107 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000108}
109
110ScheduleHazardRecognizer *ARMBaseInstrInfo::
111CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
112 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000113 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000114 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000115 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000116}
117
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000118MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
119 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000120 // FIXME: Thumb2 support.
121
David Goodwinaf7451b2009-07-08 16:09:28 +0000122 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000123 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000124
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000125 MachineFunction &MF = *MI.getParent()->getParent();
126 uint64_t TSFlags = MI.getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000127 bool isPre = false;
128 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000129 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000130 case ARMII::IndexModePre:
131 isPre = true;
132 break;
133 case ARMII::IndexModePost:
134 break;
135 }
136
137 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
138 // operation.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000139 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
David Goodwinaf7451b2009-07-08 16:09:28 +0000140 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000142
Craig Topper062a2ba2014-04-25 05:30:21 +0000143 MachineInstr *UpdateMI = nullptr;
144 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000145 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000146 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000147 unsigned NumOps = MCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 bool isLoad = !MI.mayStore();
149 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
150 const MachineOperand &Base = MI.getOperand(2);
151 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
David Goodwinaf7451b2009-07-08 16:09:28 +0000152 unsigned WBReg = WB.getReg();
153 unsigned BaseReg = Base.getReg();
154 unsigned OffReg = Offset.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000155 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
156 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
David Goodwinaf7451b2009-07-08 16:09:28 +0000157 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000158 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000159 case ARMII::AddrMode2: {
160 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
161 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
162 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000163 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000164 // Can't encode it in a so_imm operand. This transformation will
165 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000166 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000167 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000168 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 .addReg(BaseReg)
170 .addImm(Amt)
171 .addImm(Pred)
172 .addReg(0)
173 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000174 } else if (Amt != 0) {
175 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
176 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000177 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000178 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000179 .addReg(BaseReg)
180 .addReg(OffReg)
181 .addReg(0)
182 .addImm(SOOpc)
183 .addImm(Pred)
184 .addReg(0)
185 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000186 } else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000187 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000188 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000189 .addReg(BaseReg)
190 .addReg(OffReg)
191 .addImm(Pred)
192 .addReg(0)
193 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000194 break;
195 }
196 case ARMII::AddrMode3 : {
197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
198 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
199 if (OffReg == 0)
200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000201 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000203 .addReg(BaseReg)
204 .addImm(Amt)
205 .addImm(Pred)
206 .addReg(0)
207 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000209 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000210 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000211 .addReg(BaseReg)
212 .addReg(OffReg)
213 .addImm(Pred)
214 .addReg(0)
215 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000216 break;
217 }
218 }
219
220 std::vector<MachineInstr*> NewMIs;
221 if (isPre) {
222 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 MemMI =
224 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
225 .addReg(WBReg)
226 .addImm(0)
227 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000228 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000229 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
230 .addReg(MI.getOperand(1).getReg())
231 .addReg(WBReg)
232 .addReg(0)
233 .addImm(0)
234 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000235 NewMIs.push_back(MemMI);
236 NewMIs.push_back(UpdateMI);
237 } else {
238 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 MemMI =
240 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
241 .addReg(BaseReg)
242 .addImm(0)
243 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000244 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000245 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
246 .addReg(MI.getOperand(1).getReg())
247 .addReg(BaseReg)
248 .addReg(0)
249 .addImm(0)
250 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000251 if (WB.isDead())
252 UpdateMI->getOperand(0).setIsDead();
253 NewMIs.push_back(UpdateMI);
254 NewMIs.push_back(MemMI);
255 }
256
257 // Transfer LiveVariables states, kill / dead info.
258 if (LV) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000259 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
260 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000261 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000262 unsigned Reg = MO.getReg();
263
264 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
265 if (MO.isDef()) {
266 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
267 if (MO.isDead())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000268 LV->addVirtualRegisterDead(Reg, *NewMI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000269 }
270 if (MO.isUse() && MO.isKill()) {
271 for (unsigned j = 0; j < 2; ++j) {
272 // Look at the two new MI's in reverse order.
273 MachineInstr *NewMI = NewMIs[j];
274 if (!NewMI->readsRegister(Reg))
275 continue;
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000276 LV->addVirtualRegisterKilled(Reg, *NewMI);
277 if (VI.removeKill(MI))
David Goodwinaf7451b2009-07-08 16:09:28 +0000278 VI.Kills.push_back(NewMI);
279 break;
280 }
281 }
282 }
283 }
284 }
285
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000286 MachineBasicBlock::iterator MBBI = MI.getIterator();
David Goodwinaf7451b2009-07-08 16:09:28 +0000287 MFI->insert(MBBI, NewMIs[1]);
288 MFI->insert(MBBI, NewMIs[0]);
289 return NewMIs[0];
290}
291
292// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000293bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
294 MachineBasicBlock *&TBB,
295 MachineBasicBlock *&FBB,
296 SmallVectorImpl<MachineOperand> &Cond,
297 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000298 TBB = nullptr;
299 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000300
David Goodwinaf7451b2009-07-08 16:09:28 +0000301 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000302 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000303 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000304 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000305
306 // Walk backwards from the end of the basic block until the branch is
307 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000308 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000309
310 // Flag to be raised on unanalyzeable instructions. This is useful in cases
311 // where we want to clean up on the end of the basic block before we bail
312 // out.
313 bool CantAnalyze = false;
314
315 // Skip over DEBUG values and predicated nonterminators.
316 while (I->isDebugValue() || !I->isTerminator()) {
317 if (I == MBB.begin())
318 return false;
319 --I;
320 }
321
322 if (isIndirectBranchOpcode(I->getOpcode()) ||
323 isJumpTableBranchOpcode(I->getOpcode())) {
324 // Indirect branches and jump tables can't be analyzed, but we still want
325 // to clean up any instructions at the tail of the basic block.
326 CantAnalyze = true;
327 } else if (isUncondBranchOpcode(I->getOpcode())) {
328 TBB = I->getOperand(0).getMBB();
329 } else if (isCondBranchOpcode(I->getOpcode())) {
330 // Bail out if we encounter multiple conditional branches.
331 if (!Cond.empty())
332 return true;
333
334 assert(!FBB && "FBB should have been null.");
335 FBB = TBB;
336 TBB = I->getOperand(0).getMBB();
337 Cond.push_back(I->getOperand(1));
338 Cond.push_back(I->getOperand(2));
339 } else if (I->isReturn()) {
340 // Returns can't be analyzed, but we should run cleanup.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000341 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000342 } else {
343 // We encountered other unrecognized terminator. Bail out immediately.
344 return true;
345 }
346
347 // Cleanup code - to be run for unpredicated unconditional branches and
348 // returns.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000349 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000350 (isUncondBranchOpcode(I->getOpcode()) ||
351 isIndirectBranchOpcode(I->getOpcode()) ||
352 isJumpTableBranchOpcode(I->getOpcode()) ||
353 I->isReturn())) {
354 // Forget any previous condition branch information - it no longer applies.
355 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000356 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000357
358 // If we can modify the function, delete everything below this
359 // unconditional branch.
360 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000361 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000362 while (DI != MBB.end()) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000363 MachineInstr &InstToDelete = *DI;
Lang Hames24864fe2013-07-19 23:52:47 +0000364 ++DI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000365 InstToDelete.eraseFromParent();
Lang Hames24864fe2013-07-19 23:52:47 +0000366 }
367 }
368 }
369
370 if (CantAnalyze)
371 return true;
372
Dale Johannesen4244d122010-04-02 01:38:09 +0000373 if (I == MBB.begin())
374 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000375
Dale Johannesen4244d122010-04-02 01:38:09 +0000376 --I;
377 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000378
Lang Hames24864fe2013-07-19 23:52:47 +0000379 // We made it past the terminators without bailing out - we must have
380 // analyzed this branch successfully.
381 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000382}
383
384
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000385unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000386 int *BytesRemoved) const {
387 assert(!BytesRemoved && "code size not handled");
388
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000389 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
390 if (I == MBB.end())
391 return 0;
392
Evan Cheng056c6692009-07-27 18:20:05 +0000393 if (!isUncondBranchOpcode(I->getOpcode()) &&
394 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000395 return 0;
396
397 // Remove the branch.
398 I->eraseFromParent();
399
400 I = MBB.end();
401
402 if (I == MBB.begin()) return 1;
403 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000404 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000405 return 1;
406
407 // Remove the branch.
408 I->eraseFromParent();
409 return 2;
410}
411
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000412unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000413 MachineBasicBlock *TBB,
414 MachineBasicBlock *FBB,
415 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000416 const DebugLoc &DL,
417 int *BytesAdded) const {
418 assert(!BytesAdded && "code size not handled");
Evan Cheng780748d2009-07-28 05:48:47 +0000419 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
420 int BOpc = !AFI->isThumbFunction()
421 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
422 int BccOpc = !AFI->isThumbFunction()
423 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000424 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000425
David Goodwinaf7451b2009-07-08 16:09:28 +0000426 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000427 assert(TBB && "insertBranch must not be told to insert a fallthrough");
David Goodwinaf7451b2009-07-08 16:09:28 +0000428 assert((Cond.size() == 2 || Cond.size() == 0) &&
429 "ARM branch conditions have two components!");
430
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000431 // For conditional branches, we use addOperand to preserve CPSR flags.
432
Craig Topper062a2ba2014-04-25 05:30:21 +0000433 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000434 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000435 if (isThumb)
436 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
437 else
438 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000439 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000440 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000441 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000442 return 1;
443 }
444
445 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000446 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000447 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000448 if (isThumb)
449 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
450 else
451 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000452 return 2;
453}
454
455bool ARMBaseInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000456reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000457 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
458 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
459 return false;
460}
461
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000462bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
463 if (MI.isBundle()) {
464 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
465 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000466 while (++I != E && I->isInsideBundle()) {
467 int PIdx = I->findFirstPredOperandIdx();
468 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
469 return true;
470 }
471 return false;
472 }
473
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000474 int PIdx = MI.findFirstPredOperandIdx();
475 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000476}
477
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000478bool ARMBaseInstrInfo::PredicateInstruction(
479 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
480 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000481 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000482 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
483 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000484 .addImm(Pred[0].getImm())
485 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000486 return true;
487 }
488
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000489 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000490 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000491 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000492 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000493 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000494 return true;
495 }
496 return false;
497}
498
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000499bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
500 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000501 if (Pred1.size() > 2 || Pred2.size() > 2)
502 return false;
503
504 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
505 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
506 if (CC1 == CC2)
507 return true;
508
509 switch (CC1) {
510 default:
511 return false;
512 case ARMCC::AL:
513 return true;
514 case ARMCC::HS:
515 return CC2 == ARMCC::HI;
516 case ARMCC::LS:
517 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
518 case ARMCC::GE:
519 return CC2 == ARMCC::GT;
520 case ARMCC::LE:
521 return CC2 == ARMCC::LT;
522 }
523}
524
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000525bool ARMBaseInstrInfo::DefinesPredicate(
526 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000527 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000528 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
529 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000530 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
531 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000532 Pred.push_back(MO);
533 Found = true;
534 }
535 }
536
537 return Found;
538}
539
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000540static bool isCPSRDefined(const MachineInstr *MI) {
541 for (const auto &MO : MI->operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000542 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000543 return true;
544 return false;
545}
546
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000547static bool isEligibleForITBlock(const MachineInstr *MI) {
548 switch (MI->getOpcode()) {
549 default: return true;
550 case ARM::tADC: // ADC (register) T1
551 case ARM::tADDi3: // ADD (immediate) T1
552 case ARM::tADDi8: // ADD (immediate) T2
553 case ARM::tADDrr: // ADD (register) T1
554 case ARM::tAND: // AND (register) T1
555 case ARM::tASRri: // ASR (immediate) T1
556 case ARM::tASRrr: // ASR (register) T1
557 case ARM::tBIC: // BIC (register) T1
558 case ARM::tEOR: // EOR (register) T1
559 case ARM::tLSLri: // LSL (immediate) T1
560 case ARM::tLSLrr: // LSL (register) T1
561 case ARM::tLSRri: // LSR (immediate) T1
562 case ARM::tLSRrr: // LSR (register) T1
563 case ARM::tMUL: // MUL T1
564 case ARM::tMVN: // MVN (register) T1
565 case ARM::tORR: // ORR (register) T1
566 case ARM::tROR: // ROR (register) T1
567 case ARM::tRSB: // RSB (immediate) T1
568 case ARM::tSBC: // SBC (register) T1
569 case ARM::tSUBi3: // SUB (immediate) T1
570 case ARM::tSUBi8: // SUB (immediate) T2
571 case ARM::tSUBrr: // SUB (register) T1
572 return !isCPSRDefined(MI);
573 }
574}
575
Evan Chenga33fc862009-11-21 06:21:52 +0000576/// isPredicable - Return true if the specified instruction can be predicated.
577/// By default, this returns true for every instruction with a
578/// PredicateOperand.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000579bool ARMBaseInstrInfo::isPredicable(MachineInstr &MI) const {
580 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000581 return false;
582
Saleem Abdulrasoolbfa25bd2016-09-06 04:00:12 +0000583 if (MI.isBundle())
584 return false;
585
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000586 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000587 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000588
Joey Goulya5153cb2013-09-09 14:21:49 +0000589 ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000590 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000591
592 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000593 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000594 return isV8EligibleForIT(&MI);
Joey Goulya5153cb2013-09-09 14:21:49 +0000595 } else { // non-Thumb
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000596 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
Joey Goulya5153cb2013-09-09 14:21:49 +0000597 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000598 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000599
Evan Chenga33fc862009-11-21 06:21:52 +0000600 return true;
601}
David Goodwinaf7451b2009-07-08 16:09:28 +0000602
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000603namespace llvm {
604template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000605 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
606 const MachineOperand &MO = MI->getOperand(i);
607 if (!MO.isReg() || MO.isUndef() || MO.isUse())
608 continue;
609 if (MO.getReg() != ARM::CPSR)
610 continue;
611 if (!MO.isDead())
612 return false;
613 }
614 // all definitions of CPSR are dead
615 return true;
616}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000617}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000618
David Goodwinaf7451b2009-07-08 16:09:28 +0000619/// GetInstSize - Return the size of the specified MachineInstr.
620///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000621unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000622 const MachineBasicBlock &MBB = *MI.getParent();
David Goodwinaf7451b2009-07-08 16:09:28 +0000623 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000624 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000625
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000626 const MCInstrDesc &MCID = MI.getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000627 if (MCID.getSize())
628 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000629
David Blaikie46a9f012012-01-20 21:51:11 +0000630 // If this machine instr is an inline asm, measure it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000631 if (MI.getOpcode() == ARM::INLINEASM)
632 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
633 unsigned Opc = MI.getOpcode();
David Blaikie46a9f012012-01-20 21:51:11 +0000634 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000635 default:
636 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000637 return 0;
638 case TargetOpcode::BUNDLE:
639 return getInstBundleLength(MI);
640 case ARM::MOVi16_ga_pcrel:
641 case ARM::MOVTi16_ga_pcrel:
642 case ARM::t2MOVi16_ga_pcrel:
643 case ARM::t2MOVTi16_ga_pcrel:
644 return 4;
645 case ARM::MOVi32imm:
646 case ARM::t2MOVi32imm:
647 return 8;
648 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000649 case ARM::JUMPTABLE_INSTS:
650 case ARM::JUMPTABLE_ADDRS:
651 case ARM::JUMPTABLE_TBB:
652 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000653 // If this machine instr is a constant pool entry, its size is recorded as
654 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000655 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000656 case ARM::Int_eh_sjlj_longjmp:
657 return 16;
658 case ARM::tInt_eh_sjlj_longjmp:
659 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000660 case ARM::tInt_WIN_eh_sjlj_longjmp:
661 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000662 case ARM::Int_eh_sjlj_setjmp:
663 case ARM::Int_eh_sjlj_setjmp_nofp:
664 return 20;
665 case ARM::tInt_eh_sjlj_setjmp:
666 case ARM::t2Int_eh_sjlj_setjmp:
667 case ARM::t2Int_eh_sjlj_setjmp_nofp:
668 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000669 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000670 return MI.getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000671 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000672}
673
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000674unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000675 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000676 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
677 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000678 while (++I != E && I->isInsideBundle()) {
679 assert(!I->isBundle() && "No nested bundle!");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000680 Size += getInstSizeInBytes(*I);
Evan Cheng7fae11b2011-12-14 02:11:42 +0000681 }
682 return Size;
683}
684
Tim Northover5d72c5d2014-10-01 19:21:03 +0000685void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
686 MachineBasicBlock::iterator I,
687 unsigned DestReg, bool KillSrc,
688 const ARMSubtarget &Subtarget) const {
689 unsigned Opc = Subtarget.isThumb()
690 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
691 : ARM::MRS;
692
693 MachineInstrBuilder MIB =
694 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
695
696 // There is only 1 A/R class MRS instruction, and it always refers to
697 // APSR. However, there are lots of other possibilities on M-class cores.
698 if (Subtarget.isMClass())
699 MIB.addImm(0x800);
700
701 AddDefaultPred(MIB);
702
703 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
704}
705
706void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
707 MachineBasicBlock::iterator I,
708 unsigned SrcReg, bool KillSrc,
709 const ARMSubtarget &Subtarget) const {
710 unsigned Opc = Subtarget.isThumb()
711 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
712 : ARM::MSR;
713
714 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
715
716 if (Subtarget.isMClass())
717 MIB.addImm(0x800);
718 else
719 MIB.addImm(8);
720
721 MIB.addReg(SrcReg, getKillRegState(KillSrc));
722
723 AddDefaultPred(MIB);
724
725 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
726}
727
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000728void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000729 MachineBasicBlock::iterator I,
730 const DebugLoc &DL, unsigned DestReg,
731 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000732 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000733 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000734
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000735 if (GPRDest && GPRSrc) {
736 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000737 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000738 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000739 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000740
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000741 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000742 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000743
Chad Rosierbe762512011-08-20 00:17:25 +0000744 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000745 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000746 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000747 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000748 Opc = ARM::VMOVRS;
749 else if (SPRDest && GPRSrc)
750 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000751 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000752 Opc = ARM::VMOVD;
753 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000754 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000755
Chad Rosierbe762512011-08-20 00:17:25 +0000756 if (Opc) {
757 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000758 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000759 if (Opc == ARM::VORRq)
760 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000761 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000762 return;
763 }
764
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000765 // Handle register classes that require multiple instructions.
766 unsigned BeginIdx = 0;
767 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000768 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000769
770 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000771 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
772 Opc = ARM::VORRq;
773 BeginIdx = ARM::qsub_0;
774 SubRegs = 2;
775 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
776 Opc = ARM::VORRq;
777 BeginIdx = ARM::qsub_0;
778 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000779 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000780 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
781 Opc = ARM::VMOVD;
782 BeginIdx = ARM::dsub_0;
783 SubRegs = 2;
784 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
785 Opc = ARM::VMOVD;
786 BeginIdx = ARM::dsub_0;
787 SubRegs = 3;
788 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
789 Opc = ARM::VMOVD;
790 BeginIdx = ARM::dsub_0;
791 SubRegs = 4;
792 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000793 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000794 BeginIdx = ARM::gsub_0;
795 SubRegs = 2;
796 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
797 Opc = ARM::VMOVD;
798 BeginIdx = ARM::dsub_0;
799 SubRegs = 2;
800 Spacing = 2;
801 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
802 Opc = ARM::VMOVD;
803 BeginIdx = ARM::dsub_0;
804 SubRegs = 3;
805 Spacing = 2;
806 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
807 Opc = ARM::VMOVD;
808 BeginIdx = ARM::dsub_0;
809 SubRegs = 4;
810 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000811 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
812 Opc = ARM::VMOVS;
813 BeginIdx = ARM::ssub_0;
814 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000815 } else if (SrcReg == ARM::CPSR) {
816 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
817 return;
818 } else if (DestReg == ARM::CPSR) {
819 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
820 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000821 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000822
Andrew Trickb57e2252012-08-29 04:41:37 +0000823 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000824
Andrew Trick4cc69492012-08-29 01:58:52 +0000825 const TargetRegisterInfo *TRI = &getRegisterInfo();
826 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000827
828 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
829 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000830 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000831 Spacing = -Spacing;
832 }
833#ifndef NDEBUG
834 SmallSet<unsigned, 4> DstRegs;
835#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000836 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000837 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
838 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000839 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000840#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000841 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000842 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000843#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000844 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000845 // VORR takes two source operands.
846 if (Opc == ARM::VORRq)
847 Mov.addReg(Src);
848 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000849 // MOVr can set CC.
850 if (Opc == ARM::MOVr)
851 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000852 }
853 // Add implicit super-register defs and kills to the last instruction.
854 Mov->addRegisterDefined(DestReg, TRI);
855 if (KillSrc)
856 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000857}
858
Tim Northover798697d2013-04-21 11:57:07 +0000859const MachineInstrBuilder &
860ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
861 unsigned SubIdx, unsigned State,
862 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000863 if (!SubIdx)
864 return MIB.addReg(Reg, State);
865
866 if (TargetRegisterInfo::isPhysicalRegister(Reg))
867 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
868 return MIB.addReg(Reg, State, SubIdx);
869}
870
David Goodwinaf7451b2009-07-08 16:09:28 +0000871void ARMBaseInstrInfo::
872storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
873 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000874 const TargetRegisterClass *RC,
875 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000876 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000877 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000878 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000879 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000880 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000881
Alex Lorenze40c8a22015-08-11 23:09:45 +0000882 MachineMemOperand *MMO = MF.getMachineMemOperand(
883 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
884 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000885
Owen Anderson732f82c2011-08-10 17:21:20 +0000886 switch (RC->getSize()) {
887 case 4:
888 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
889 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000890 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000891 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000892 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
893 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000894 .addReg(SrcReg, getKillRegState(isKill))
895 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000896 } else
897 llvm_unreachable("Unknown reg class!");
898 break;
899 case 8:
900 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
901 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000902 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000903 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000904 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000905 if (Subtarget.hasV5TEOps()) {
906 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
907 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
908 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
909 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
910
911 AddDefaultPred(MIB);
912 } else {
913 // Fallback to STM instruction, which has existed since the dawn of
914 // time.
915 MachineInstrBuilder MIB =
916 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
917 .addFrameIndex(FI).addMemOperand(MMO));
918 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
919 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
920 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000921 } else
922 llvm_unreachable("Unknown reg class!");
923 break;
924 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000925 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000926 // Use aligned spills if the stack can be realigned.
927 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000928 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000929 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000930 .addReg(SrcReg, getKillRegState(isKill))
931 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000932 } else {
933 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000934 .addReg(SrcReg, getKillRegState(isKill))
935 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000936 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000937 }
938 } else
939 llvm_unreachable("Unknown reg class!");
940 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000941 case 24:
942 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
943 // Use aligned spills if the stack can be realigned.
944 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
945 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
946 .addFrameIndex(FI).addImm(16)
947 .addReg(SrcReg, getKillRegState(isKill))
948 .addMemOperand(MMO));
949 } else {
950 MachineInstrBuilder MIB =
951 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
952 .addFrameIndex(FI))
953 .addMemOperand(MMO);
954 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
955 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
956 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
957 }
958 } else
959 llvm_unreachable("Unknown reg class!");
960 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000961 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000962 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000963 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
964 // FIXME: It's possible to only store part of the QQ register if the
965 // spilled def has a sub-register index.
966 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000967 .addFrameIndex(FI).addImm(16)
968 .addReg(SrcReg, getKillRegState(isKill))
969 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000970 } else {
971 MachineInstrBuilder MIB =
972 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000973 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000974 .addMemOperand(MMO);
975 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
976 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
977 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
978 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
979 }
980 } else
981 llvm_unreachable("Unknown reg class!");
982 break;
983 case 64:
984 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
985 MachineInstrBuilder MIB =
986 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
987 .addFrameIndex(FI))
988 .addMemOperand(MMO);
989 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
990 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
991 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
992 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
993 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
994 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
995 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
996 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
997 } else
998 llvm_unreachable("Unknown reg class!");
999 break;
1000 default:
1001 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001002 }
1003}
1004
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001005unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1006 int &FrameIndex) const {
1007 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001008 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001009 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001010 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1012 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1013 MI.getOperand(3).getImm() == 0) {
1014 FrameIndex = MI.getOperand(1).getIndex();
1015 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001016 }
1017 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001018 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001019 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001020 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001021 case ARM::VSTRD:
1022 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001023 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1024 MI.getOperand(2).getImm() == 0) {
1025 FrameIndex = MI.getOperand(1).getIndex();
1026 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001027 }
1028 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001029 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001030 case ARM::VST1d64TPseudo:
1031 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1033 FrameIndex = MI.getOperand(0).getIndex();
1034 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001035 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001036 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001037 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001038 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1039 FrameIndex = MI.getOperand(1).getIndex();
1040 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001041 }
1042 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001043 }
1044
1045 return 0;
1046}
1047
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001049 int &FrameIndex) const {
1050 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001051 return MI.mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001052}
1053
David Goodwinaf7451b2009-07-08 16:09:28 +00001054void ARMBaseInstrInfo::
1055loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1056 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001057 const TargetRegisterClass *RC,
1058 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001059 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001060 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001061 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001062 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001063 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001064 MachineMemOperand *MMO = MF.getMachineMemOperand(
1065 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1066 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001067
Owen Anderson732f82c2011-08-10 17:21:20 +00001068 switch (RC->getSize()) {
1069 case 4:
1070 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1071 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1072 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001073
Owen Anderson732f82c2011-08-10 17:21:20 +00001074 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1075 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001076 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001077 } else
1078 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001079 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001080 case 8:
1081 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1082 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001083 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001084 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001085 MachineInstrBuilder MIB;
1086
1087 if (Subtarget.hasV5TEOps()) {
1088 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1089 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1090 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1091 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1092
1093 AddDefaultPred(MIB);
1094 } else {
1095 // Fallback to LDM instruction, which has existed since the dawn of
1096 // time.
1097 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1098 .addFrameIndex(FI).addMemOperand(MMO));
1099 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1100 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1101 }
1102
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001103 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1104 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001105 } else
1106 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001107 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001108 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001109 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001110 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001111 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001112 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001113 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001114 } else {
1115 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1116 .addFrameIndex(FI)
1117 .addMemOperand(MMO));
1118 }
1119 } else
1120 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001121 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001122 case 24:
1123 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1124 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1125 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1126 .addFrameIndex(FI).addImm(16)
1127 .addMemOperand(MMO));
1128 } else {
1129 MachineInstrBuilder MIB =
1130 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1131 .addFrameIndex(FI)
1132 .addMemOperand(MMO));
1133 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 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1137 MIB.addReg(DestReg, RegState::ImplicitDefine);
1138 }
1139 } else
1140 llvm_unreachable("Unknown reg class!");
1141 break;
1142 case 32:
1143 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001144 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1145 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001146 .addFrameIndex(FI).addImm(16)
1147 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001148 } else {
1149 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001150 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1151 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001152 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001153 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1154 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1155 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1156 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, 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 }
1160 } else
1161 llvm_unreachable("Unknown reg class!");
1162 break;
1163 case 64:
1164 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1165 MachineInstrBuilder MIB =
1166 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1167 .addFrameIndex(FI))
1168 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001169 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1170 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1171 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1172 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1173 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1174 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1175 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1176 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001177 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1178 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001179 } else
1180 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001181 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001182 default:
1183 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001184 }
1185}
1186
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001187unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1188 int &FrameIndex) const {
1189 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001190 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001191 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001192 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001193 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1194 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1195 MI.getOperand(3).getImm() == 0) {
1196 FrameIndex = MI.getOperand(1).getIndex();
1197 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001198 }
1199 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001200 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001201 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001202 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001203 case ARM::VLDRD:
1204 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001205 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1206 MI.getOperand(2).getImm() == 0) {
1207 FrameIndex = MI.getOperand(1).getIndex();
1208 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001209 }
1210 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001211 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001212 case ARM::VLD1d64TPseudo:
1213 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001214 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1215 FrameIndex = MI.getOperand(1).getIndex();
1216 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001217 }
1218 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001219 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001220 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1221 FrameIndex = MI.getOperand(1).getIndex();
1222 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001223 }
1224 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001225 }
1226
1227 return 0;
1228}
1229
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001230unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1231 int &FrameIndex) const {
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001232 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001233 return MI.mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001234}
1235
Scott Douglass953f9082015-10-05 14:49:54 +00001236/// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1237/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001238void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001239 bool isThumb1 = Subtarget.isThumb1Only();
1240 bool isThumb2 = Subtarget.isThumb2();
1241 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1242
Scott Douglass953f9082015-10-05 14:49:54 +00001243 DebugLoc dl = MI->getDebugLoc();
1244 MachineBasicBlock *BB = MI->getParent();
1245
1246 MachineInstrBuilder LDM, STM;
1247 if (isThumb1 || !MI->getOperand(1).isDead()) {
1248 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1249 : isThumb1 ? ARM::tLDMIA_UPD
1250 : ARM::LDMIA_UPD))
1251 .addOperand(MI->getOperand(1));
1252 } else {
1253 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1254 }
1255
1256 if (isThumb1 || !MI->getOperand(0).isDead()) {
1257 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1258 : isThumb1 ? ARM::tSTMIA_UPD
1259 : ARM::STMIA_UPD))
1260 .addOperand(MI->getOperand(0));
1261 } else {
1262 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1263 }
1264
1265 AddDefaultPred(LDM.addOperand(MI->getOperand(3)));
1266 AddDefaultPred(STM.addOperand(MI->getOperand(2)));
1267
1268 // Sort the scratch registers into ascending order.
1269 const TargetRegisterInfo &TRI = getRegisterInfo();
1270 llvm::SmallVector<unsigned, 6> ScratchRegs;
1271 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1272 ScratchRegs.push_back(MI->getOperand(I).getReg());
1273 std::sort(ScratchRegs.begin(), ScratchRegs.end(),
1274 [&TRI](const unsigned &Reg1,
1275 const unsigned &Reg2) -> bool {
1276 return TRI.getEncodingValue(Reg1) <
1277 TRI.getEncodingValue(Reg2);
1278 });
1279
1280 for (const auto &Reg : ScratchRegs) {
1281 LDM.addReg(Reg, RegState::Define);
1282 STM.addReg(Reg, RegState::Kill);
1283 }
1284
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001285 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001286}
1287
1288
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001289bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1290 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001291 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001292 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001293 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001294 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001295 return true;
1296 }
1297
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001298 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001299 expandMEMCPY(MI);
1300 return true;
1301 }
1302
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001303 // This hook gets to expand COPY instructions before they become
1304 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1305 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1306 // changed into a VORR that can go down the NEON pipeline.
Diana Picusb772e402016-07-06 11:22:11 +00001307 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001308 return false;
1309
1310 // Look for a copy between even S-registers. That is where we keep floats
1311 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001312 unsigned DstRegS = MI.getOperand(0).getReg();
1313 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001314 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1315 return false;
1316
1317 const TargetRegisterInfo *TRI = &getRegisterInfo();
1318 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1319 &ARM::DPRRegClass);
1320 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1321 &ARM::DPRRegClass);
1322 if (!DstRegD || !SrcRegD)
1323 return false;
1324
1325 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1326 // legal if the COPY already defines the full DstRegD, and it isn't a
1327 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001328 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001329 return false;
1330
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001331 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001332 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001333 return false;
1334
1335 // All clear, widen the COPY.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001336 DEBUG(dbgs() << "widening: " << MI);
1337 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001338
1339 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1340 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001341 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001342 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001343 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001344
1345 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001346 MI.setDesc(get(ARM::VMOVD));
1347 MI.getOperand(0).setReg(DstRegD);
1348 MI.getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001349 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001350
1351 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1352 // register scavenger and machine verifier, so we need to indicate that we
1353 // are reading an undefined value from SrcRegD, but a proper value from
1354 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001355 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001356 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001357
1358 // SrcRegD may actually contain an unrelated value in the ssub_1
1359 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001360 if (MI.getOperand(1).isKill()) {
1361 MI.getOperand(1).setIsKill(false);
1362 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001363 }
1364
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001365 DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001366 return true;
1367}
1368
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001369/// Create a copy of a const pool value. Update CPI to the new index and return
1370/// the label UID.
1371static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1372 MachineConstantPool *MCP = MF.getConstantPool();
1373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1374
1375 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1376 assert(MCPE.isMachineConstantPoolEntry() &&
1377 "Expecting a machine constantpool entry!");
1378 ARMConstantPoolValue *ACPV =
1379 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1380
Evan Chengdfce83c2011-01-17 08:03:18 +00001381 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001382 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001383
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001384 // FIXME: The below assumes PIC relocation model and that the function
1385 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1386 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1387 // instructions, so that's probably OK, but is PIC always correct when
1388 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001389 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001390 NewCPV = ARMConstantPoolConstant::Create(
1391 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1392 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001393 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001394 NewCPV = ARMConstantPoolSymbol::
1395 Create(MF.getFunction()->getContext(),
1396 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001397 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001398 NewCPV = ARMConstantPoolConstant::
1399 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1400 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001401 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001402 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1403 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001404 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001405 NewCPV = ARMConstantPoolMBB::
1406 Create(MF.getFunction()->getContext(),
1407 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001408 else
1409 llvm_unreachable("Unexpected ARM constantpool value type!!");
1410 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1411 return PCLabelId;
1412}
1413
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001414void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1415 MachineBasicBlock::iterator I,
1416 unsigned DestReg, unsigned SubIdx,
1417 const MachineInstr &Orig,
1418 const TargetRegisterInfo &TRI) const {
1419 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001420 switch (Opcode) {
1421 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001422 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1423 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001424 MBB.insert(I, MI);
1425 break;
1426 }
1427 case ARM::tLDRpci_pic:
1428 case ARM::t2LDRpci_pic: {
1429 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001430 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001431 unsigned PCLabelId = duplicateCPV(MF, CPI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001432 MachineInstrBuilder MIB =
1433 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1434 .addConstantPoolIndex(CPI)
1435 .addImm(PCLabelId);
1436 MIB->setMemRefs(Orig.memoperands_begin(), Orig.memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001437 break;
1438 }
1439 }
Evan Chengfe864422009-11-08 00:15:23 +00001440}
1441
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001442MachineInstr *ARMBaseInstrInfo::duplicate(MachineInstr &Orig,
1443 MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001444 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001445 switch (Orig.getOpcode()) {
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001446 case ARM::tLDRpci_pic:
1447 case ARM::t2LDRpci_pic: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001448 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001449 unsigned PCLabelId = duplicateCPV(MF, CPI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001450 Orig.getOperand(1).setIndex(CPI);
1451 Orig.getOperand(2).setImm(PCLabelId);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001452 break;
1453 }
1454 }
1455 return MI;
1456}
1457
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001458bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1459 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001460 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001461 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001462 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001463 Opcode == ARM::t2LDRpci_pic ||
1464 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001465 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001466 Opcode == ARM::LDRLIT_ga_pcrel ||
1467 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1468 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001469 Opcode == ARM::MOV_ga_pcrel ||
1470 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001471 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001472 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001473 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001474 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001475 return false;
1476
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001477 const MachineOperand &MO0 = MI0.getOperand(1);
1478 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001479 if (MO0.getOffset() != MO1.getOffset())
1480 return false;
1481
Tim Northover72360d22013-12-02 10:35:41 +00001482 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1483 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1484 Opcode == ARM::tLDRLIT_ga_pcrel ||
1485 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001486 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001487 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001488 // Ignore the PC labels.
1489 return MO0.getGlobal() == MO1.getGlobal();
1490
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001491 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001492 const MachineConstantPool *MCP = MF->getConstantPool();
1493 int CPI0 = MO0.getIndex();
1494 int CPI1 = MO1.getIndex();
1495 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1496 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001497 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1498 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1499 if (isARMCP0 && isARMCP1) {
1500 ARMConstantPoolValue *ACPV0 =
1501 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1502 ARMConstantPoolValue *ACPV1 =
1503 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1504 return ACPV0->hasSameValue(ACPV1);
1505 } else if (!isARMCP0 && !isARMCP1) {
1506 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1507 }
1508 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001509 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001510 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001511 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001512 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001513 return false;
1514
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001515 unsigned Addr0 = MI0.getOperand(1).getReg();
1516 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001517 if (Addr0 != Addr1) {
1518 if (!MRI ||
1519 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1520 !TargetRegisterInfo::isVirtualRegister(Addr1))
1521 return false;
1522
1523 // This assumes SSA form.
1524 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1525 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1526 // Check if the loaded value, e.g. a constantpool of a global address, are
1527 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001528 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001529 return false;
1530 }
1531
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001532 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Evan Chengb8b0ad82011-01-20 08:34:58 +00001533 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001534 const MachineOperand &MO0 = MI0.getOperand(i);
1535 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001536 if (!MO0.isIdenticalTo(MO1))
1537 return false;
1538 }
1539 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001540 }
1541
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001542 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001543}
1544
Bill Wendlingf4707472010-06-23 23:00:16 +00001545/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1546/// determine if two loads are loading from the same base address. It should
1547/// only return true if the base pointers are the same and the only differences
1548/// between the two addresses is the offset. It also returns the offsets by
1549/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001550///
1551/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1552/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001553bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1554 int64_t &Offset1,
1555 int64_t &Offset2) const {
1556 // Don't worry about Thumb: just ARM and Thumb2.
1557 if (Subtarget.isThumb1Only()) return false;
1558
1559 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1560 return false;
1561
1562 switch (Load1->getMachineOpcode()) {
1563 default:
1564 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001565 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001566 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001567 case ARM::LDRD:
1568 case ARM::LDRH:
1569 case ARM::LDRSB:
1570 case ARM::LDRSH:
1571 case ARM::VLDRD:
1572 case ARM::VLDRS:
1573 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001574 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001575 case ARM::t2LDRDi8:
1576 case ARM::t2LDRSHi8:
1577 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001578 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001579 case ARM::t2LDRSHi12:
1580 break;
1581 }
1582
1583 switch (Load2->getMachineOpcode()) {
1584 default:
1585 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001586 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001587 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001588 case ARM::LDRD:
1589 case ARM::LDRH:
1590 case ARM::LDRSB:
1591 case ARM::LDRSH:
1592 case ARM::VLDRD:
1593 case ARM::VLDRS:
1594 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001595 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001596 case ARM::t2LDRSHi8:
1597 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001598 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001599 case ARM::t2LDRSHi12:
1600 break;
1601 }
1602
1603 // Check if base addresses and chain operands match.
1604 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1605 Load1->getOperand(4) != Load2->getOperand(4))
1606 return false;
1607
1608 // Index should be Reg0.
1609 if (Load1->getOperand(3) != Load2->getOperand(3))
1610 return false;
1611
1612 // Determine the offsets.
1613 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1614 isa<ConstantSDNode>(Load2->getOperand(1))) {
1615 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1616 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1617 return true;
1618 }
1619
1620 return false;
1621}
1622
1623/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001624/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001625/// be scheduled togther. On some targets if two loads are loading from
1626/// addresses in the same cache line, it's better if they are scheduled
1627/// together. This function takes two integers that represent the load offsets
1628/// from the common base address. It returns true if it decides it's desirable
1629/// to schedule the two loads together. "NumLoads" is the number of loads that
1630/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001631///
1632/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1633/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001634bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1635 int64_t Offset1, int64_t Offset2,
1636 unsigned NumLoads) const {
1637 // Don't worry about Thumb: just ARM and Thumb2.
1638 if (Subtarget.isThumb1Only()) return false;
1639
1640 assert(Offset2 > Offset1);
1641
1642 if ((Offset2 - Offset1) / 8 > 64)
1643 return false;
1644
Renato Golinb184cd92013-08-14 16:35:29 +00001645 // Check if the machine opcodes are different. If they are different
1646 // then we consider them to not be of the same base address,
1647 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1648 // In this case, they are considered to be the same because they are different
1649 // encoding forms of the same basic instruction.
1650 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1651 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1652 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1653 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1654 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001655 return false; // FIXME: overly conservative?
1656
1657 // Four loads in a row should be sufficient.
1658 if (NumLoads >= 3)
1659 return false;
1660
1661 return true;
1662}
1663
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001664bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001665 const MachineBasicBlock *MBB,
1666 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001667 // Debug info is never a scheduling boundary. It's necessary to be explicit
1668 // due to the special treatment of IT instructions below, otherwise a
1669 // dbg_value followed by an IT will result in the IT instruction being
1670 // considered a scheduling hazard, which is wrong. It should be the actual
1671 // instruction preceding the dbg_value instruction(s), just like it is
1672 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001673 if (MI.isDebugValue())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001674 return false;
1675
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001676 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001677 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001678 return true;
1679
1680 // Treat the start of the IT block as a scheduling boundary, but schedule
1681 // t2IT along with all instructions following it.
1682 // FIXME: This is a big hammer. But the alternative is to add all potential
1683 // true and anti dependencies to IT block instructions as implicit operands
1684 // to the t2IT instruction. The added compile time and complexity does not
1685 // seem worth it.
1686 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001687 // Make sure to skip any dbg_value instructions
1688 while (++I != MBB->end() && I->isDebugValue())
1689 ;
1690 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001691 return true;
1692
1693 // Don't attempt to schedule around any instruction that defines
1694 // a stack-oriented pointer, as it's unlikely to be profitable. This
1695 // saves compile time, because it doesn't require every single
1696 // stack slot reference to depend on the instruction that does the
1697 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001698 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001699 // No ARM calling conventions change the stack pointer. (X86 calling
1700 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001701 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001702 return true;
1703
1704 return false;
1705}
1706
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001707bool ARMBaseInstrInfo::
1708isProfitableToIfCvt(MachineBasicBlock &MBB,
1709 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001710 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001711 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001712 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001713
Peter Collingbourne65295232015-04-23 20:31:30 +00001714 // If we are optimizing for size, see if the branch in the predecessor can be
1715 // lowered to cbn?z by the constant island lowering pass, and return false if
1716 // so. This results in a shorter instruction sequence.
Sanjay Patel924879a2015-08-04 15:49:57 +00001717 if (MBB.getParent()->getFunction()->optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001718 MachineBasicBlock *Pred = *MBB.pred_begin();
1719 if (!Pred->empty()) {
1720 MachineInstr *LastMI = &*Pred->rbegin();
1721 if (LastMI->getOpcode() == ARM::t2Bcc) {
1722 MachineBasicBlock::iterator CmpMI = LastMI;
1723 if (CmpMI != Pred->begin()) {
1724 --CmpMI;
1725 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1726 CmpMI->getOpcode() == ARM::t2CMPri) {
1727 unsigned Reg = CmpMI->getOperand(0).getReg();
1728 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001729 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
Peter Collingbourne65295232015-04-23 20:31:30 +00001730 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1731 isARMLowRegister(Reg))
1732 return false;
1733 }
1734 }
1735 }
1736 }
1737 }
1738
Owen Anderson88af7d02010-09-28 18:32:13 +00001739 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001740 // Here we scale up each component of UnpredCost to avoid precision issue when
1741 // scaling NumCycles by Probability.
1742 const unsigned ScalingUpFactor = 1024;
1743 unsigned UnpredCost = Probability.scale(NumCycles * ScalingUpFactor);
1744 UnpredCost += ScalingUpFactor; // The branch itself
1745 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001746
Cong Houf9f9ffb2015-09-18 18:19:40 +00001747 return (NumCycles + ExtraPredCycles) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001748}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001749
Evan Cheng02b184d2010-06-25 22:42:03 +00001750bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001751isProfitableToIfCvt(MachineBasicBlock &TMBB,
1752 unsigned TCycles, unsigned TExtra,
1753 MachineBasicBlock &FMBB,
1754 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001755 BranchProbability Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001756 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001757 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001758
Owen Anderson88af7d02010-09-28 18:32:13 +00001759 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001760 // Here we scale up each component of UnpredCost to avoid precision issue when
1761 // scaling TCycles/FCycles by Probability.
1762 const unsigned ScalingUpFactor = 1024;
1763 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1764 unsigned FUnpredCost =
1765 Probability.getCompl().scale(FCycles * ScalingUpFactor);
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001766 unsigned UnpredCost = TUnpredCost + FUnpredCost;
Cong Houf9f9ffb2015-09-18 18:19:40 +00001767 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1768 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001769
Cong Houf9f9ffb2015-09-18 18:19:40 +00001770 return (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001771}
1772
Bob Wilsone8a549c2012-09-29 21:43:49 +00001773bool
1774ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1775 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001776 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001777 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001778 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001779}
1780
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001781/// getInstrPredicate - If instruction is predicated, returns its predicate
1782/// condition, otherwise returns AL. It also returns the condition code
1783/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001784ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1785 unsigned &PredReg) {
1786 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001787 if (PIdx == -1) {
1788 PredReg = 0;
1789 return ARMCC::AL;
1790 }
1791
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001792 PredReg = MI.getOperand(PIdx+1).getReg();
1793 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001794}
1795
1796
Matthias Braunfa3872e2015-05-18 20:27:55 +00001797unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001798 if (Opc == ARM::B)
1799 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001800 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001801 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001802 if (Opc == ARM::t2B)
1803 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001804
1805 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001806}
1807
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001808MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001809 bool NewMI,
1810 unsigned OpIdx1,
1811 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001812 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001813 case ARM::MOVCCr:
1814 case ARM::t2MOVCCr: {
1815 // MOVCC can be commuted by inverting the condition.
1816 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001817 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001818 // MOVCC AL can't be inverted. Shouldn't happen.
1819 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001820 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001821 MachineInstr *CommutedMI =
1822 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1823 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001824 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001825 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001826 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
1827 .setImm(ARMCC::getOppositeCondition(CC));
1828 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001829 }
1830 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001831 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001832}
Evan Cheng780748d2009-07-28 05:48:47 +00001833
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001834/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001835/// return the defining instruction.
1836static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1837 const MachineRegisterInfo &MRI,
1838 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001839 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001840 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001841 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001842 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001843 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001844 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001845 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001846 // MI is folded into the MOVCC by predicating it.
1847 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001848 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001849 // Check if MI has any non-dead defs or physreg uses. This also detects
1850 // predicated instructions which will be reading CPSR.
1851 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1852 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001853 // Reject frame index operands, PEI can't handle the predicated pseudos.
1854 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001855 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001856 if (!MO.isReg())
1857 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001858 // MI can't have any tied operands, that would conflict with predication.
1859 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001860 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001861 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001862 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001863 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001864 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001865 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001866 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001867 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001868 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001869 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001870}
1871
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001872bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001873 SmallVectorImpl<MachineOperand> &Cond,
1874 unsigned &TrueOp, unsigned &FalseOp,
1875 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001876 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001877 "Unknown select instruction");
1878 // MOVCC operands:
1879 // 0: Def.
1880 // 1: True use.
1881 // 2: False use.
1882 // 3: Condition code.
1883 // 4: CPSR use.
1884 TrueOp = 1;
1885 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001886 Cond.push_back(MI.getOperand(3));
1887 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001888 // We can always fold a def.
1889 Optimizable = true;
1890 return false;
1891}
1892
Mehdi Amini22e59742015-01-13 07:07:13 +00001893MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001894ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00001895 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1896 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001897 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001898 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001899 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1900 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001901 bool Invert = !DefMI;
1902 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001903 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001904 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001905 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001906
Matthias Braun2f169f92013-10-04 16:52:56 +00001907 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001908 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
1909 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00001910 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1911 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001912 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001913
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001914 // Create a new predicated version of DefMI.
1915 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001916 MachineInstrBuilder NewMI =
1917 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001918
1919 // Copy all the DefMI operands, excluding its (null) predicate.
1920 const MCInstrDesc &DefDesc = DefMI->getDesc();
1921 for (unsigned i = 1, e = DefDesc.getNumOperands();
1922 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1923 NewMI.addOperand(DefMI->getOperand(i));
1924
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001925 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001926 if (Invert)
1927 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1928 else
1929 NewMI.addImm(CondCode);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001930 NewMI.addOperand(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001931
1932 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1933 if (NewMI->hasOptionalDef())
1934 AddDefaultCC(NewMI);
1935
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001936 // The output register value when the predicate is false is an implicit
1937 // register operand tied to the first def.
1938 // The tie makes the register allocator ensure the FalseReg is allocated the
1939 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001940 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001941 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001942 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1943
Mehdi Amini22e59742015-01-13 07:07:13 +00001944 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1945 SeenMIs.insert(NewMI);
1946 SeenMIs.erase(DefMI);
1947
Pete Cooper2127b002015-04-30 23:57:47 +00001948 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1949 // DefMI would be invalid when tranferred inside the loop. Checking for a
1950 // loop is expensive, but at least remove kill flags if they are in different
1951 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001952 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00001953 NewMI->clearKillInfo();
1954
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001955 // The caller will erase MI, but not DefMI.
1956 DefMI->eraseFromParent();
1957 return NewMI;
1958}
1959
Andrew Trick924123a2011-09-21 02:20:46 +00001960/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1961/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1962/// def operand.
1963///
1964/// This will go away once we can teach tblgen how to set the optional CPSR def
1965/// operand itself.
1966struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001967 uint16_t PseudoOpc;
1968 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001969};
1970
Craig Topper2fbd1302012-05-24 03:59:11 +00001971static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001972 {ARM::ADDSri, ARM::ADDri},
1973 {ARM::ADDSrr, ARM::ADDrr},
1974 {ARM::ADDSrsi, ARM::ADDrsi},
1975 {ARM::ADDSrsr, ARM::ADDrsr},
1976
1977 {ARM::SUBSri, ARM::SUBri},
1978 {ARM::SUBSrr, ARM::SUBrr},
1979 {ARM::SUBSrsi, ARM::SUBrsi},
1980 {ARM::SUBSrsr, ARM::SUBrsr},
1981
1982 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001983 {ARM::RSBSrsi, ARM::RSBrsi},
1984 {ARM::RSBSrsr, ARM::RSBrsr},
1985
1986 {ARM::t2ADDSri, ARM::t2ADDri},
1987 {ARM::t2ADDSrr, ARM::t2ADDrr},
1988 {ARM::t2ADDSrs, ARM::t2ADDrs},
1989
1990 {ARM::t2SUBSri, ARM::t2SUBri},
1991 {ARM::t2SUBSrr, ARM::t2SUBrr},
1992 {ARM::t2SUBSrs, ARM::t2SUBrs},
1993
1994 {ARM::t2RSBSri, ARM::t2RSBri},
1995 {ARM::t2RSBSrs, ARM::t2RSBrs},
1996};
1997
1998unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001999 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2000 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2001 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002002 return 0;
2003}
2004
Evan Cheng780748d2009-07-28 05:48:47 +00002005void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002006 MachineBasicBlock::iterator &MBBI,
2007 const DebugLoc &dl, unsigned DestReg,
2008 unsigned BaseReg, int NumBytes,
2009 ARMCC::CondCodes Pred, unsigned PredReg,
2010 const ARMBaseInstrInfo &TII,
2011 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002012 if (NumBytes == 0 && DestReg != BaseReg) {
2013 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
2014 .addReg(BaseReg, RegState::Kill)
2015 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2016 .setMIFlags(MIFlags);
2017 return;
2018 }
2019
Evan Cheng780748d2009-07-28 05:48:47 +00002020 bool isSub = NumBytes < 0;
2021 if (isSub) NumBytes = -NumBytes;
2022
2023 while (NumBytes) {
2024 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2025 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2026 assert(ThisVal && "Didn't extract field correctly");
2027
2028 // We will handle these bits from offset, clear them.
2029 NumBytes &= ~ThisVal;
2030
2031 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2032
2033 // Build the new ADD / SUB.
2034 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2035 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2036 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002037 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2038 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002039 BaseReg = DestReg;
2040 }
2041}
2042
Tim Northoverdee86042013-12-02 14:46:26 +00002043bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2044 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002045 unsigned NumBytes) {
2046 // This optimisation potentially adds lots of load and store
2047 // micro-operations, it's only really a great benefit to code-size.
Sanjay Patel924879a2015-08-04 15:49:57 +00002048 if (!MF.getFunction()->optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002049 return false;
2050
2051 // If only one register is pushed/popped, LLVM can use an LDR/STR
2052 // instead. We can't modify those so make sure we're dealing with an
2053 // instruction we understand.
2054 bool IsPop = isPopOpcode(MI->getOpcode());
2055 bool IsPush = isPushOpcode(MI->getOpcode());
2056 if (!IsPush && !IsPop)
2057 return false;
2058
2059 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2060 MI->getOpcode() == ARM::VLDMDIA_UPD;
2061 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2062 MI->getOpcode() == ARM::tPOP ||
2063 MI->getOpcode() == ARM::tPOP_RET;
2064
2065 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2066 MI->getOperand(1).getReg() == ARM::SP)) &&
2067 "trying to fold sp update into non-sp-updating push/pop");
2068
2069 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2070 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2071 // if this is violated.
2072 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2073 return false;
2074
2075 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2076 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2077 int RegListIdx = IsT1PushPop ? 2 : 4;
2078
2079 // Calculate the space we'll need in terms of registers.
2080 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2081 unsigned RD0Reg, RegsNeeded;
2082 if (IsVFPPushPop) {
2083 RD0Reg = ARM::D0;
2084 RegsNeeded = NumBytes / 8;
2085 } else {
2086 RD0Reg = ARM::R0;
2087 RegsNeeded = NumBytes / 4;
2088 }
2089
2090 // We're going to have to strip all list operands off before
2091 // re-adding them since the order matters, so save the existing ones
2092 // for later.
2093 SmallVector<MachineOperand, 4> RegList;
2094 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2095 RegList.push_back(MI->getOperand(i));
2096
Tim Northover93bcc662013-11-08 17:18:07 +00002097 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002098 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002099
2100 // Now try to find enough space in the reglist to allocate NumBytes.
2101 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002102 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002103 if (!IsPop) {
2104 // Pushing any register is completely harmless, mark the
2105 // register involved as undef since we don't care about it in
2106 // the slightest.
2107 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2108 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002109 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002110 continue;
2111 }
2112
Tim Northover45479dc2013-12-01 14:16:24 +00002113 // However, we can only pop an extra register if it's not live. For
2114 // registers live within the function we might clobber a return value
2115 // register; the other way a register can be live here is if it's
2116 // callee-saved.
2117 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002118 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2119 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002120 // VFP pops don't allow holes in the register list, so any skip is fatal
2121 // for our transformation. GPR pops do, so we should just keep looking.
2122 if (IsVFPPushPop)
2123 return false;
2124 else
2125 continue;
2126 }
Tim Northover93bcc662013-11-08 17:18:07 +00002127
2128 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002129 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2130 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002131 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002132 }
2133
2134 if (RegsNeeded > 0)
2135 return false;
2136
2137 // Finally we know we can profitably perform the optimisation so go
2138 // ahead: strip all existing registers off and add them back again
2139 // in the right order.
2140 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2141 MI->RemoveOperand(i);
2142
2143 // Add the complete list back in.
2144 MachineInstrBuilder MIB(MF, &*MI);
2145 for (int i = RegList.size() - 1; i >= 0; --i)
2146 MIB.addOperand(RegList[i]);
2147
2148 return true;
2149}
2150
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002151bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2152 unsigned FrameReg, int &Offset,
2153 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002154 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002155 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002156 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2157 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002158
Evan Cheng780748d2009-07-28 05:48:47 +00002159 // Memory operands in inline assembly always use AddrMode2.
2160 if (Opcode == ARM::INLINEASM)
2161 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002162
Evan Cheng780748d2009-07-28 05:48:47 +00002163 if (Opcode == ARM::ADDri) {
2164 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2165 if (Offset == 0) {
2166 // Turn it into a move.
2167 MI.setDesc(TII.get(ARM::MOVr));
2168 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2169 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002170 Offset = 0;
2171 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002172 } else if (Offset < 0) {
2173 Offset = -Offset;
2174 isSub = true;
2175 MI.setDesc(TII.get(ARM::SUBri));
2176 }
2177
2178 // Common case: small offset, fits into instruction.
2179 if (ARM_AM::getSOImmVal(Offset) != -1) {
2180 // Replace the FrameIndex with sp / fp
2181 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2182 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002183 Offset = 0;
2184 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002185 }
2186
2187 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2188 // as possible.
2189 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2190 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2191
2192 // We will handle these bits from offset, clear them.
2193 Offset &= ~ThisImmVal;
2194
2195 // Get the properly encoded SOImmVal field.
2196 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2197 "Bit extraction didn't work?");
2198 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2199 } else {
2200 unsigned ImmIdx = 0;
2201 int InstrOffs = 0;
2202 unsigned NumBits = 0;
2203 unsigned Scale = 1;
2204 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002205 case ARMII::AddrMode_i12: {
2206 ImmIdx = FrameRegIdx + 1;
2207 InstrOffs = MI.getOperand(ImmIdx).getImm();
2208 NumBits = 12;
2209 break;
2210 }
Evan Cheng780748d2009-07-28 05:48:47 +00002211 case ARMII::AddrMode2: {
2212 ImmIdx = FrameRegIdx+2;
2213 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2214 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2215 InstrOffs *= -1;
2216 NumBits = 12;
2217 break;
2218 }
2219 case ARMII::AddrMode3: {
2220 ImmIdx = FrameRegIdx+2;
2221 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2222 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2223 InstrOffs *= -1;
2224 NumBits = 8;
2225 break;
2226 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002227 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002228 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002229 // Can't fold any offset even if it's zero.
2230 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002231 case ARMII::AddrMode5: {
2232 ImmIdx = FrameRegIdx+1;
2233 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2234 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2235 InstrOffs *= -1;
2236 NumBits = 8;
2237 Scale = 4;
2238 break;
2239 }
2240 default:
2241 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002242 }
2243
2244 Offset += InstrOffs * Scale;
2245 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2246 if (Offset < 0) {
2247 Offset = -Offset;
2248 isSub = true;
2249 }
2250
2251 // Attempt to fold address comp. if opcode has offset bits
2252 if (NumBits > 0) {
2253 // Common case: small offset, fits into instruction.
2254 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2255 int ImmedOffset = Offset / Scale;
2256 unsigned Mask = (1 << NumBits) - 1;
2257 if ((unsigned)Offset <= Mask * Scale) {
2258 // Replace the FrameIndex with sp
2259 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002260 // FIXME: When addrmode2 goes away, this will simplify (like the
2261 // T2 version), as the LDR.i12 versions don't need the encoding
2262 // tricks for the offset value.
2263 if (isSub) {
2264 if (AddrMode == ARMII::AddrMode_i12)
2265 ImmedOffset = -ImmedOffset;
2266 else
2267 ImmedOffset |= 1 << NumBits;
2268 }
Evan Cheng780748d2009-07-28 05:48:47 +00002269 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002270 Offset = 0;
2271 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002272 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002273
Evan Cheng780748d2009-07-28 05:48:47 +00002274 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2275 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002276 if (isSub) {
2277 if (AddrMode == ARMII::AddrMode_i12)
2278 ImmedOffset = -ImmedOffset;
2279 else
2280 ImmedOffset |= 1 << NumBits;
2281 }
Evan Cheng780748d2009-07-28 05:48:47 +00002282 ImmOp.ChangeToImmediate(ImmedOffset);
2283 Offset &= ~(Mask*Scale);
2284 }
2285 }
2286
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002287 Offset = (isSub) ? -Offset : Offset;
2288 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002289}
Bill Wendling7de9d522010-08-06 01:32:48 +00002290
Manman Ren6fa76dc2012-06-29 21:33:59 +00002291/// analyzeCompare - For a comparison instruction, return the source registers
2292/// in SrcReg and SrcReg2 if having two register operands, and the value it
2293/// compares against in CmpValue. Return true if the comparison instruction
2294/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002295bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2296 unsigned &SrcReg2, int &CmpMask,
2297 int &CmpValue) const {
2298 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002299 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002300 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002301 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002302 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002303 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002304 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002305 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002306 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002307 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002308 case ARM::CMPrr:
2309 case ARM::t2CMPrr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002310 SrcReg = MI.getOperand(0).getReg();
2311 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002312 CmpMask = ~0;
2313 CmpValue = 0;
2314 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002315 case ARM::TSTri:
2316 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002317 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002318 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002319 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002320 CmpValue = 0;
2321 return true;
2322 }
2323
2324 return false;
2325}
2326
Gabor Greifd36e3e82010-09-29 10:12:08 +00002327/// isSuitableForMask - Identify a suitable 'and' instruction that
2328/// operates on the given source register and applies the same mask
2329/// as a 'tst' instruction. Provide a limited look-through for copies.
2330/// When successful, MI will hold the found instruction.
2331static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002332 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002333 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002334 case ARM::ANDri:
2335 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002336 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002337 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002338 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002339 return true;
2340 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002341 }
2342
2343 return false;
2344}
2345
Manman Renb1b3db62012-06-29 22:06:19 +00002346/// getSwappedCondition - assume the flags are set by MI(a,b), return
2347/// the condition code if we modify the instructions such that flags are
2348/// set by MI(b,a).
2349inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2350 switch (CC) {
2351 default: return ARMCC::AL;
2352 case ARMCC::EQ: return ARMCC::EQ;
2353 case ARMCC::NE: return ARMCC::NE;
2354 case ARMCC::HS: return ARMCC::LS;
2355 case ARMCC::LO: return ARMCC::HI;
2356 case ARMCC::HI: return ARMCC::LO;
2357 case ARMCC::LS: return ARMCC::HS;
2358 case ARMCC::GE: return ARMCC::LE;
2359 case ARMCC::LT: return ARMCC::GT;
2360 case ARMCC::GT: return ARMCC::LT;
2361 case ARMCC::LE: return ARMCC::GE;
2362 }
2363}
2364
2365/// isRedundantFlagInstr - check whether the first instruction, whose only
2366/// purpose is to update flags, can be made redundant.
2367/// CMPrr can be made redundant by SUBrr if the operands are the same.
2368/// CMPri can be made redundant by SUBri if the operands are the same.
2369/// This function can be extended later on.
2370inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2371 unsigned SrcReg2, int ImmValue,
2372 MachineInstr *OI) {
2373 if ((CmpI->getOpcode() == ARM::CMPrr ||
2374 CmpI->getOpcode() == ARM::t2CMPrr) &&
2375 (OI->getOpcode() == ARM::SUBrr ||
2376 OI->getOpcode() == ARM::t2SUBrr) &&
2377 ((OI->getOperand(1).getReg() == SrcReg &&
2378 OI->getOperand(2).getReg() == SrcReg2) ||
2379 (OI->getOperand(1).getReg() == SrcReg2 &&
2380 OI->getOperand(2).getReg() == SrcReg)))
2381 return true;
2382
2383 if ((CmpI->getOpcode() == ARM::CMPri ||
2384 CmpI->getOpcode() == ARM::t2CMPri) &&
2385 (OI->getOpcode() == ARM::SUBri ||
2386 OI->getOpcode() == ARM::t2SUBri) &&
2387 OI->getOperand(1).getReg() == SrcReg &&
2388 OI->getOperand(2).getImm() == ImmValue)
2389 return true;
2390 return false;
2391}
2392
Manman Ren6fa76dc2012-06-29 21:33:59 +00002393/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2394/// comparison into one that sets the zero bit in the flags register;
2395/// Remove a redundant Compare instruction if an earlier instruction can set the
2396/// flags in the same way as Compare.
2397/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2398/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2399/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002400bool ARMBaseInstrInfo::optimizeCompareInstr(
2401 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2402 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002403 // Get the unique definition of SrcReg.
2404 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2405 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002406
Gabor Greifadbbb932010-09-21 12:01:15 +00002407 // Masked compares sometimes use the same register as the corresponding 'and'.
2408 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002409 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002410 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002411 for (MachineRegisterInfo::use_instr_iterator
2412 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2413 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002414 if (UI->getParent() != CmpInstr.getParent())
2415 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002416 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002417 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002418 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002419 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002420 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002421 break;
2422 }
2423 if (!MI) return false;
2424 }
2425 }
2426
Manman Rendc8ad002012-05-11 01:30:47 +00002427 // Get ready to iterate backward from CmpInstr.
2428 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002429 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002430
2431 // Early exit if CmpInstr is at the beginning of the BB.
2432 if (I == B) return false;
2433
Manman Rendc8ad002012-05-11 01:30:47 +00002434 // There are two possible candidates which can be changed to set CPSR:
2435 // One is MI, the other is a SUB instruction.
2436 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2437 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002438 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002439 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002440 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002441 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002442 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002443 // Conservatively refuse to convert an instruction which isn't in the same
2444 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002445 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2446 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002447 if (CmpInstr.getOpcode() == ARM::CMPri ||
2448 CmpInstr.getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002449 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002450 else
2451 return false;
2452 }
2453
2454 // Check that CPSR isn't set between the comparison instruction and the one we
2455 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002456 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002457 --I;
2458 for (; I != E; --I) {
2459 const MachineInstr &Instr = *I;
2460
Manman Renb1b3db62012-06-29 22:06:19 +00002461 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2462 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002463 // This instruction modifies or uses CPSR after the one we want to
2464 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002465 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002466
Manman Renb1b3db62012-06-29 22:06:19 +00002467 // Check whether CmpInstr can be made redundant by the current instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002468 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002469 Sub = &*I;
2470 break;
2471 }
2472
Evan Chengd757c882010-09-21 23:49:07 +00002473 if (I == B)
2474 // The 'and' is below the comparison instruction.
2475 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002476 }
2477
Manman Rendc8ad002012-05-11 01:30:47 +00002478 // Return false if no candidates exist.
2479 if (!MI && !Sub)
2480 return false;
2481
2482 // The single candidate is called MI.
2483 if (!MI) MI = Sub;
2484
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002485 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002486 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002487 return false;
2488
James Molloy0f412272016-09-09 09:51:06 +00002489 bool IsThumb1 = false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002490 switch (MI->getOpcode()) {
2491 default: break;
James Molloy0f412272016-09-09 09:51:06 +00002492 case ARM::tLSLri:
2493 case ARM::tLSRri:
2494 case ARM::tLSLrr:
2495 case ARM::tLSRrr:
2496 case ARM::tSUBrr:
2497 case ARM::tADDrr:
2498 case ARM::tADDi3:
2499 case ARM::tADDi8:
2500 case ARM::tSUBi3:
2501 case ARM::tSUBi8:
2502 IsThumb1 = true;
2503 LLVM_FALLTHROUGH;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002504 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002505 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002506 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002507 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002508 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002509 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002510 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002511 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002512 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002513 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002514 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002515 case ARM::SBCri:
2516 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002517 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002518 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002519 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002520 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002521 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002522 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002523 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002524 case ARM::t2SBCri:
2525 case ARM::ANDrr:
2526 case ARM::ANDri:
2527 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002528 case ARM::t2ANDri:
2529 case ARM::ORRrr:
2530 case ARM::ORRri:
2531 case ARM::t2ORRrr:
2532 case ARM::t2ORRri:
2533 case ARM::EORrr:
2534 case ARM::EORri:
2535 case ARM::t2EORrr:
James Molloy9790d8f2016-09-14 09:45:28 +00002536 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002537 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002538 // When checking against MI: if it's a conditional code that requires
2539 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002540 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2541 // If we are done with the basic block, we need to check whether CPSR is
2542 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002543 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2544 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002545 bool isSafe = false;
2546 I = CmpInstr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002547 E = CmpInstr.getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002548 while (!isSafe && ++I != E) {
2549 const MachineInstr &Instr = *I;
2550 for (unsigned IO = 0, EO = Instr.getNumOperands();
2551 !isSafe && IO != EO; ++IO) {
2552 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002553 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2554 isSafe = true;
2555 break;
2556 }
Evan Cheng425489d2011-03-23 22:52:04 +00002557 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2558 continue;
2559 if (MO.isDef()) {
2560 isSafe = true;
2561 break;
2562 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002563 // Condition code is after the operand before CPSR except for VSELs.
2564 ARMCC::CondCodes CC;
2565 bool IsInstrVSel = true;
2566 switch (Instr.getOpcode()) {
2567 default:
2568 IsInstrVSel = false;
2569 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2570 break;
2571 case ARM::VSELEQD:
2572 case ARM::VSELEQS:
2573 CC = ARMCC::EQ;
2574 break;
2575 case ARM::VSELGTD:
2576 case ARM::VSELGTS:
2577 CC = ARMCC::GT;
2578 break;
2579 case ARM::VSELGED:
2580 case ARM::VSELGES:
2581 CC = ARMCC::GE;
2582 break;
2583 case ARM::VSELVSS:
2584 case ARM::VSELVSD:
2585 CC = ARMCC::VS;
2586 break;
2587 }
2588
Manman Renb1b3db62012-06-29 22:06:19 +00002589 if (Sub) {
2590 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2591 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002592 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002593 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2594 // on CMP needs to be updated to be based on SUB.
2595 // Push the condition code operands to OperandsToUpdate.
2596 // If it is safe to remove CmpInstr, the condition code of these
2597 // operands will be modified.
2598 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002599 Sub->getOperand(2).getReg() == SrcReg) {
2600 // VSel doesn't support condition code update.
2601 if (IsInstrVSel)
2602 return false;
2603 OperandsToUpdate.push_back(
2604 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2605 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002606 } else {
2607 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002608 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002609 case ARMCC::EQ: // Z
2610 case ARMCC::NE: // Z
2611 case ARMCC::MI: // N
2612 case ARMCC::PL: // N
2613 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002614 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002615 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002616 case ARMCC::HS: // C
2617 case ARMCC::LO: // C
2618 case ARMCC::VS: // V
2619 case ARMCC::VC: // V
2620 case ARMCC::HI: // C Z
2621 case ARMCC::LS: // C Z
2622 case ARMCC::GE: // N V
2623 case ARMCC::LT: // N V
2624 case ARMCC::GT: // Z N V
2625 case ARMCC::LE: // Z N V
2626 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002627 return false;
2628 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002629 }
Evan Cheng425489d2011-03-23 22:52:04 +00002630 }
2631 }
2632
Manman Ren34cb93e2012-07-11 22:51:44 +00002633 // If CPSR is not killed nor re-defined, we should check whether it is
2634 // live-out. If it is live-out, do not optimize.
2635 if (!isSafe) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002636 MachineBasicBlock *MBB = CmpInstr.getParent();
Manman Ren34cb93e2012-07-11 22:51:44 +00002637 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2638 SE = MBB->succ_end(); SI != SE; ++SI)
2639 if ((*SI)->isLiveIn(ARM::CPSR))
2640 return false;
2641 }
Evan Cheng425489d2011-03-23 22:52:04 +00002642
James Molloy0f412272016-09-09 09:51:06 +00002643 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2644 // set CPSR so this is represented as an explicit output)
2645 if (!IsThumb1) {
2646 MI->getOperand(5).setReg(ARM::CPSR);
2647 MI->getOperand(5).setIsDef(true);
2648 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002649 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002650 CmpInstr.eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002651
2652 // Modify the condition code of operands in OperandsToUpdate.
2653 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2654 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002655 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2656 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002657 return true;
2658 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002659 }
James Molloy0f412272016-09-09 09:51:06 +00002660
Bill Wendling7de9d522010-08-06 01:32:48 +00002661 return false;
2662}
Evan Cheng367a5df2010-09-09 18:18:55 +00002663
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002664bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2665 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002666 MachineRegisterInfo *MRI) const {
2667 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002668 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002669 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2670 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002671 if (!DefMI.getOperand(1).isImm())
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002672 // Could be t2MOVi32imm <ga:xx>
2673 return false;
2674
2675 if (!MRI->hasOneNonDBGUse(Reg))
2676 return false;
2677
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002678 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002679 if (DefMCID.hasOptionalDef()) {
2680 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002681 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00002682 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2683 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2684 // to delete DefMI.
2685 return false;
2686 }
2687
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002688 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002689 if (UseMCID.hasOptionalDef()) {
2690 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002691 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00002692 // If the instruction sets the flag, do not attempt this optimization
2693 // since it may change the semantics of the code.
2694 return false;
2695 }
2696
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002697 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002698 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002699 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002700 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002701 bool Commute = false;
2702 switch (UseOpc) {
2703 default: return false;
2704 case ARM::SUBrr:
2705 case ARM::ADDrr:
2706 case ARM::ORRrr:
2707 case ARM::EORrr:
2708 case ARM::t2SUBrr:
2709 case ARM::t2ADDrr:
2710 case ARM::t2ORRrr:
2711 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002712 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002713 switch (UseOpc) {
2714 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002715 case ARM::ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002716 case ARM::SUBrr: {
2717 if (UseOpc == ARM::SUBrr && Commute)
2718 return false;
2719
2720 // ADD/SUB are special because they're essentially the same operation, so
2721 // we can handle a larger range of immediates.
2722 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
2723 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
2724 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
2725 ImmVal = -ImmVal;
2726 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
2727 } else
2728 return false;
2729 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2730 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2731 break;
2732 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002733 case ARM::ORRrr:
2734 case ARM::EORrr: {
2735 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2736 return false;
2737 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2738 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2739 switch (UseOpc) {
2740 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002741 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2742 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2743 }
2744 break;
2745 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002746 case ARM::t2ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002747 case ARM::t2SUBrr: {
2748 if (UseOpc == ARM::t2SUBrr && Commute)
2749 return false;
2750
2751 // ADD/SUB are special because they're essentially the same operation, so
2752 // we can handle a larger range of immediates.
2753 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2754 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
2755 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
2756 ImmVal = -ImmVal;
2757 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
2758 } else
2759 return false;
2760 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2761 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2762 break;
2763 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002764 case ARM::t2ORRrr:
2765 case ARM::t2EORrr: {
2766 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2767 return false;
2768 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2769 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2770 switch (UseOpc) {
2771 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002772 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2773 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2774 }
2775 break;
2776 }
2777 }
2778 }
2779 }
2780
2781 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002782 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
2783 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002784 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002785 AddDefaultCC(
2786 AddDefaultPred(BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
2787 get(NewUseOpc), NewReg)
2788 .addReg(Reg1, getKillRegState(isKill))
2789 .addImm(SOImmValV1)));
2790 UseMI.setDesc(get(NewUseOpc));
2791 UseMI.getOperand(1).setReg(NewReg);
2792 UseMI.getOperand(1).setIsKill();
2793 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
2794 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002795 return true;
2796}
2797
Bob Wilsone8a549c2012-09-29 21:43:49 +00002798static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002799 const MachineInstr &MI) {
2800 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00002801 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002802 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002803 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2804 assert(UOps >= 0 && "bad # UOps");
2805 return UOps;
2806 }
2807
2808 case ARM::LDRrs:
2809 case ARM::LDRBrs:
2810 case ARM::STRrs:
2811 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002812 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002813 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2814 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2815 if (!isSub &&
2816 (ShImm == 0 ||
2817 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2818 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2819 return 1;
2820 return 2;
2821 }
2822
2823 case ARM::LDRH:
2824 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002825 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00002826 return 1;
2827
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002828 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002829 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2830 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2831 if (!isSub &&
2832 (ShImm == 0 ||
2833 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2834 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2835 return 1;
2836 return 2;
2837 }
2838
2839 case ARM::LDRSB:
2840 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002841 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002842
2843 case ARM::LDRSB_POST:
2844 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002845 unsigned Rt = MI.getOperand(0).getReg();
2846 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002847 return (Rt == Rm) ? 4 : 3;
2848 }
2849
2850 case ARM::LDR_PRE_REG:
2851 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002852 unsigned Rt = MI.getOperand(0).getReg();
2853 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002854 if (Rt == Rm)
2855 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002856 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002857 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2858 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2859 if (!isSub &&
2860 (ShImm == 0 ||
2861 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2862 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2863 return 2;
2864 return 3;
2865 }
2866
2867 case ARM::STR_PRE_REG:
2868 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002869 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002870 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2871 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2872 if (!isSub &&
2873 (ShImm == 0 ||
2874 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2875 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2876 return 2;
2877 return 3;
2878 }
2879
2880 case ARM::LDRH_PRE:
2881 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002882 unsigned Rt = MI.getOperand(0).getReg();
2883 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002884 if (!Rm)
2885 return 2;
2886 if (Rt == Rm)
2887 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002888 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002889 }
2890
2891 case ARM::LDR_POST_REG:
2892 case ARM::LDRB_POST_REG:
2893 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002894 unsigned Rt = MI.getOperand(0).getReg();
2895 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002896 return (Rt == Rm) ? 3 : 2;
2897 }
2898
2899 case ARM::LDR_PRE_IMM:
2900 case ARM::LDRB_PRE_IMM:
2901 case ARM::LDR_POST_IMM:
2902 case ARM::LDRB_POST_IMM:
2903 case ARM::STRB_POST_IMM:
2904 case ARM::STRB_POST_REG:
2905 case ARM::STRB_PRE_IMM:
2906 case ARM::STRH_POST:
2907 case ARM::STR_POST_IMM:
2908 case ARM::STR_POST_REG:
2909 case ARM::STR_PRE_IMM:
2910 return 2;
2911
2912 case ARM::LDRSB_PRE:
2913 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002914 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002915 if (Rm == 0)
2916 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002917 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002918 if (Rt == Rm)
2919 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002920 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002921 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2922 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2923 if (!isSub &&
2924 (ShImm == 0 ||
2925 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2926 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2927 return 3;
2928 return 4;
2929 }
2930
2931 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002932 unsigned Rt = MI.getOperand(0).getReg();
2933 unsigned Rn = MI.getOperand(2).getReg();
2934 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002935 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002936 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
2937 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002938 return (Rt == Rn) ? 3 : 2;
2939 }
2940
2941 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002942 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002943 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002944 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
2945 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002946 return 2;
2947 }
2948
2949 case ARM::LDRD_POST:
2950 case ARM::t2LDRD_POST:
2951 return 3;
2952
2953 case ARM::STRD_POST:
2954 case ARM::t2STRD_POST:
2955 return 4;
2956
2957 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002958 unsigned Rt = MI.getOperand(0).getReg();
2959 unsigned Rn = MI.getOperand(3).getReg();
2960 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002961 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002962 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
2963 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002964 return (Rt == Rn) ? 4 : 3;
2965 }
2966
2967 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002968 unsigned Rt = MI.getOperand(0).getReg();
2969 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002970 return (Rt == Rn) ? 4 : 3;
2971 }
2972
2973 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002974 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002975 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002976 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
2977 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002978 return 3;
2979 }
2980
2981 case ARM::t2STRD_PRE:
2982 return 3;
2983
2984 case ARM::t2LDR_POST:
2985 case ARM::t2LDRB_POST:
2986 case ARM::t2LDRB_PRE:
2987 case ARM::t2LDRSBi12:
2988 case ARM::t2LDRSBi8:
2989 case ARM::t2LDRSBpci:
2990 case ARM::t2LDRSBs:
2991 case ARM::t2LDRH_POST:
2992 case ARM::t2LDRH_PRE:
2993 case ARM::t2LDRSBT:
2994 case ARM::t2LDRSB_POST:
2995 case ARM::t2LDRSB_PRE:
2996 case ARM::t2LDRSH_POST:
2997 case ARM::t2LDRSH_PRE:
2998 case ARM::t2LDRSHi12:
2999 case ARM::t2LDRSHi8:
3000 case ARM::t2LDRSHpci:
3001 case ARM::t2LDRSHs:
3002 return 2;
3003
3004 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003005 unsigned Rt = MI.getOperand(0).getReg();
3006 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003007 return (Rt == Rn) ? 3 : 2;
3008 }
3009
3010 case ARM::t2STRB_POST:
3011 case ARM::t2STRB_PRE:
3012 case ARM::t2STRBs:
3013 case ARM::t2STRDi8:
3014 case ARM::t2STRH_POST:
3015 case ARM::t2STRH_PRE:
3016 case ARM::t2STRHs:
3017 case ARM::t2STR_POST:
3018 case ARM::t2STR_PRE:
3019 case ARM::t2STRs:
3020 return 2;
3021 }
3022}
3023
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003024// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3025// can't be easily determined return 0 (missing MachineMemOperand).
3026//
3027// FIXME: The current MachineInstr design does not support relying on machine
3028// mem operands to determine the width of a memory access. Instead, we expect
3029// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003030// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003031// two reasons:
3032//
3033// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3034// operands. This is much more dangerous than using the MachineMemOperand
3035// sizes because CodeGen passes can insert/remove optional machine operands. In
3036// fact, it's totally incorrect for preRA passes and appears to be wrong for
3037// postRA passes as well.
3038//
3039// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3040// machine model that calls this should handle the unknown (zero size) case.
3041//
3042// Long term, we should require a target hook that verifies MachineMemOperand
3043// sizes during MC lowering. That target hook should be local to MC lowering
3044// because we can't ensure that it is aware of other MI forms. Doing this will
3045// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003046unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003047 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003048 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3049 E = MI.memoperands_end();
3050 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003051 Size += (*I)->getSize();
3052 }
3053 return Size / 4;
3054}
3055
Diana Picus92423ce2016-06-27 09:08:23 +00003056static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3057 unsigned NumRegs) {
3058 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3059 switch (Opc) {
3060 default:
3061 break;
3062 case ARM::VLDMDIA_UPD:
3063 case ARM::VLDMDDB_UPD:
3064 case ARM::VLDMSIA_UPD:
3065 case ARM::VLDMSDB_UPD:
3066 case ARM::VSTMDIA_UPD:
3067 case ARM::VSTMDDB_UPD:
3068 case ARM::VSTMSIA_UPD:
3069 case ARM::VSTMSDB_UPD:
3070 case ARM::LDMIA_UPD:
3071 case ARM::LDMDA_UPD:
3072 case ARM::LDMDB_UPD:
3073 case ARM::LDMIB_UPD:
3074 case ARM::STMIA_UPD:
3075 case ARM::STMDA_UPD:
3076 case ARM::STMDB_UPD:
3077 case ARM::STMIB_UPD:
3078 case ARM::tLDMIA_UPD:
3079 case ARM::tSTMIA_UPD:
3080 case ARM::t2LDMIA_UPD:
3081 case ARM::t2LDMDB_UPD:
3082 case ARM::t2STMIA_UPD:
3083 case ARM::t2STMDB_UPD:
3084 ++UOps; // One for base register writeback.
3085 break;
3086 case ARM::LDMIA_RET:
3087 case ARM::tPOP_RET:
3088 case ARM::t2LDMIA_RET:
3089 UOps += 2; // One for base reg wb, one for write to pc.
3090 break;
3091 }
3092 return UOps;
3093}
3094
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003095unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3096 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003097 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003098 return 1;
3099
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003100 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003101 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003102 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003103 if (ItinUOps >= 0) {
3104 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3105 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3106
Andrew Trickf161e392012-07-02 18:10:42 +00003107 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003108 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003109
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003110 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003111 switch (Opc) {
3112 default:
3113 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003114 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003115 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003116 return 2;
3117
3118 // The number of uOps for load / store multiple are determined by the number
3119 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003120 //
Evan Chengbf407072010-09-10 01:29:16 +00003121 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3122 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003123 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003124 //
Evan Chengbf407072010-09-10 01:29:16 +00003125 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003126 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3127 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3128 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003129 case ARM::VLDMDIA_UPD:
3130 case ARM::VLDMDDB_UPD:
3131 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003132 case ARM::VLDMSIA_UPD:
3133 case ARM::VLDMSDB_UPD:
3134 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003135 case ARM::VSTMDIA_UPD:
3136 case ARM::VSTMDDB_UPD:
3137 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003138 case ARM::VSTMSIA_UPD:
3139 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003140 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003141 return (NumRegs / 2) + (NumRegs % 2) + 1;
3142 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003143
3144 case ARM::LDMIA_RET:
3145 case ARM::LDMIA:
3146 case ARM::LDMDA:
3147 case ARM::LDMDB:
3148 case ARM::LDMIB:
3149 case ARM::LDMIA_UPD:
3150 case ARM::LDMDA_UPD:
3151 case ARM::LDMDB_UPD:
3152 case ARM::LDMIB_UPD:
3153 case ARM::STMIA:
3154 case ARM::STMDA:
3155 case ARM::STMDB:
3156 case ARM::STMIB:
3157 case ARM::STMIA_UPD:
3158 case ARM::STMDA_UPD:
3159 case ARM::STMDB_UPD:
3160 case ARM::STMIB_UPD:
3161 case ARM::tLDMIA:
3162 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003163 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003164 case ARM::tPOP_RET:
3165 case ARM::tPOP:
3166 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003167 case ARM::t2LDMIA_RET:
3168 case ARM::t2LDMIA:
3169 case ARM::t2LDMDB:
3170 case ARM::t2LDMIA_UPD:
3171 case ARM::t2LDMDB_UPD:
3172 case ARM::t2STMIA:
3173 case ARM::t2STMDB:
3174 case ARM::t2STMIA_UPD:
3175 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003176 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003177 switch (Subtarget.getLdStMultipleTiming()) {
3178 case ARMSubtarget::SingleIssuePlusExtras:
3179 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3180 case ARMSubtarget::SingleIssue:
3181 // Assume the worst.
3182 return NumRegs;
3183 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003184 if (NumRegs < 4)
3185 return 2;
3186 // 4 registers would be issued: 2, 2.
3187 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003188 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003189 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003190 ++UOps;
3191 return UOps;
3192 }
3193 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3194 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003195 // If there are odd number of registers or if it's not 64-bit aligned,
3196 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003197 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3198 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003199 ++UOps;
3200 return UOps;
3201 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003202 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003203 }
3204 }
Diana Picus92423ce2016-06-27 09:08:23 +00003205 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003206}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003207
3208int
Evan Cheng412e37b2010-10-07 23:12:15 +00003209ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003210 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003211 unsigned DefClass,
3212 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003213 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003214 if (RegNo <= 0)
3215 // Def is the address writeback.
3216 return ItinData->getOperandCycle(DefClass, DefIdx);
3217
3218 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003219 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003220 // (regno / 2) + (regno % 2) + 1
3221 DefCycle = RegNo / 2 + 1;
3222 if (RegNo % 2)
3223 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003224 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003225 DefCycle = RegNo;
3226 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003227
Evan Cheng6cc775f2011-06-28 19:10:37 +00003228 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003229 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003230 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003231 case ARM::VLDMSIA_UPD:
3232 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003233 isSLoad = true;
3234 break;
3235 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003236
Evan Cheng412e37b2010-10-07 23:12:15 +00003237 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3238 // then it takes an extra cycle.
3239 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3240 ++DefCycle;
3241 } else {
3242 // Assume the worst.
3243 DefCycle = RegNo + 2;
3244 }
3245
3246 return DefCycle;
3247}
3248
3249int
3250ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003251 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003252 unsigned DefClass,
3253 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003254 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003255 if (RegNo <= 0)
3256 // Def is the address writeback.
3257 return ItinData->getOperandCycle(DefClass, DefIdx);
3258
3259 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003260 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003261 // 4 registers would be issued: 1, 2, 1.
3262 // 5 registers would be issued: 1, 2, 2.
3263 DefCycle = RegNo / 2;
3264 if (DefCycle < 1)
3265 DefCycle = 1;
3266 // Result latency is issue cycle + 2: E2.
3267 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003268 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003269 DefCycle = (RegNo / 2);
3270 // If there are odd number of registers or if it's not 64-bit aligned,
3271 // then it takes an extra AGU (Address Generation Unit) cycle.
3272 if ((RegNo % 2) || DefAlign < 8)
3273 ++DefCycle;
3274 // Result latency is AGU cycles + 2.
3275 DefCycle += 2;
3276 } else {
3277 // Assume the worst.
3278 DefCycle = RegNo + 2;
3279 }
3280
3281 return DefCycle;
3282}
3283
3284int
3285ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003286 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003287 unsigned UseClass,
3288 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003289 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003290 if (RegNo <= 0)
3291 return ItinData->getOperandCycle(UseClass, UseIdx);
3292
3293 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003294 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003295 // (regno / 2) + (regno % 2) + 1
3296 UseCycle = RegNo / 2 + 1;
3297 if (RegNo % 2)
3298 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003299 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003300 UseCycle = RegNo;
3301 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003302
Evan Cheng6cc775f2011-06-28 19:10:37 +00003303 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003304 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003305 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003306 case ARM::VSTMSIA_UPD:
3307 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003308 isSStore = true;
3309 break;
3310 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003311
Evan Cheng412e37b2010-10-07 23:12:15 +00003312 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3313 // then it takes an extra cycle.
3314 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3315 ++UseCycle;
3316 } else {
3317 // Assume the worst.
3318 UseCycle = RegNo + 2;
3319 }
3320
3321 return UseCycle;
3322}
3323
3324int
3325ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003326 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003327 unsigned UseClass,
3328 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003329 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003330 if (RegNo <= 0)
3331 return ItinData->getOperandCycle(UseClass, UseIdx);
3332
3333 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003334 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003335 UseCycle = RegNo / 2;
3336 if (UseCycle < 2)
3337 UseCycle = 2;
3338 // Read in E3.
3339 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003340 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003341 UseCycle = (RegNo / 2);
3342 // If there are odd number of registers or if it's not 64-bit aligned,
3343 // then it takes an extra AGU (Address Generation Unit) cycle.
3344 if ((RegNo % 2) || UseAlign < 8)
3345 ++UseCycle;
3346 } else {
3347 // Assume the worst.
3348 UseCycle = 1;
3349 }
3350 return UseCycle;
3351}
3352
3353int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003354ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003355 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003356 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003357 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003358 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003359 unsigned DefClass = DefMCID.getSchedClass();
3360 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003361
Evan Cheng6cc775f2011-06-28 19:10:37 +00003362 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003363 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3364
3365 // This may be a def / use of a variable_ops instruction, the operand
3366 // latency might be determinable dynamically. Let the target try to
3367 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003368 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003369 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003370 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003371 default:
3372 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3373 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003374
3375 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003376 case ARM::VLDMDIA_UPD:
3377 case ARM::VLDMDDB_UPD:
3378 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003379 case ARM::VLDMSIA_UPD:
3380 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003381 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003382 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003383
3384 case ARM::LDMIA_RET:
3385 case ARM::LDMIA:
3386 case ARM::LDMDA:
3387 case ARM::LDMDB:
3388 case ARM::LDMIB:
3389 case ARM::LDMIA_UPD:
3390 case ARM::LDMDA_UPD:
3391 case ARM::LDMDB_UPD:
3392 case ARM::LDMIB_UPD:
3393 case ARM::tLDMIA:
3394 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003395 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003396 case ARM::t2LDMIA_RET:
3397 case ARM::t2LDMIA:
3398 case ARM::t2LDMDB:
3399 case ARM::t2LDMIA_UPD:
3400 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003401 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003402 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003403 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003404 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003405
3406 if (DefCycle == -1)
3407 // We can't seem to determine the result latency of the def, assume it's 2.
3408 DefCycle = 2;
3409
3410 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003411 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003412 default:
3413 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3414 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003415
3416 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003417 case ARM::VSTMDIA_UPD:
3418 case ARM::VSTMDDB_UPD:
3419 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003420 case ARM::VSTMSIA_UPD:
3421 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003422 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003423 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003424
3425 case ARM::STMIA:
3426 case ARM::STMDA:
3427 case ARM::STMDB:
3428 case ARM::STMIB:
3429 case ARM::STMIA_UPD:
3430 case ARM::STMDA_UPD:
3431 case ARM::STMDB_UPD:
3432 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003433 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003434 case ARM::tPOP_RET:
3435 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003436 case ARM::t2STMIA:
3437 case ARM::t2STMDB:
3438 case ARM::t2STMIA_UPD:
3439 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003440 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003441 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003442 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003443
3444 if (UseCycle == -1)
3445 // Assume it's read in the first stage.
3446 UseCycle = 1;
3447
3448 UseCycle = DefCycle - UseCycle + 1;
3449 if (UseCycle > 0) {
3450 if (LdmBypass) {
3451 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3452 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003453 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003454 UseClass, UseIdx))
3455 --UseCycle;
3456 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003457 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003458 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003459 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003460 }
3461
3462 return UseCycle;
3463}
3464
Evan Cheng7fae11b2011-12-14 02:11:42 +00003465static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003466 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003467 unsigned &DefIdx, unsigned &Dist) {
3468 Dist = 0;
3469
3470 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003471 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003472 assert(II->isInsideBundle() && "Empty bundle?");
3473
3474 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003475 while (II->isInsideBundle()) {
3476 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3477 if (Idx != -1)
3478 break;
3479 --II;
3480 ++Dist;
3481 }
3482
3483 assert(Idx != -1 && "Cannot find bundled definition!");
3484 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003485 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003486}
3487
3488static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003489 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003490 unsigned &UseIdx, unsigned &Dist) {
3491 Dist = 0;
3492
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003493 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003494 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003495 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003496
3497 // FIXME: This doesn't properly handle multiple uses.
3498 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003499 while (II != E && II->isInsideBundle()) {
3500 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3501 if (Idx != -1)
3502 break;
3503 if (II->getOpcode() != ARM::t2IT)
3504 ++Dist;
3505 ++II;
3506 }
3507
Evan Chengda103bf2011-12-14 20:00:08 +00003508 if (Idx == -1) {
3509 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003510 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003511 }
3512
Evan Cheng7fae11b2011-12-14 02:11:42 +00003513 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003514 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003515}
3516
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003517/// Return the number of cycles to add to (or subtract from) the static
3518/// itinerary based on the def opcode and alignment. The caller will ensure that
3519/// adjusted latency is at least one cycle.
3520static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003521 const MachineInstr &DefMI,
3522 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003523 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003524 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003525 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3526 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003527 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003528 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003529 case ARM::LDRrs:
3530 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003531 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003532 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3533 if (ShImm == 0 ||
3534 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003535 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003536 break;
3537 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003538 case ARM::t2LDRs:
3539 case ARM::t2LDRBs:
3540 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003541 case ARM::t2LDRSHs: {
3542 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003543 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003544 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003545 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003546 break;
3547 }
3548 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003549 } else if (Subtarget.isSwift()) {
3550 // FIXME: Properly handle all of the latency adjustments for address
3551 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003552 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003553 default: break;
3554 case ARM::LDRrs:
3555 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003556 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003557 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3558 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3559 if (!isSub &&
3560 (ShImm == 0 ||
3561 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3562 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3563 Adjust -= 2;
3564 else if (!isSub &&
3565 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3566 --Adjust;
3567 break;
3568 }
3569 case ARM::t2LDRs:
3570 case ARM::t2LDRBs:
3571 case ARM::t2LDRHs:
3572 case ARM::t2LDRSHs: {
3573 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003574 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003575 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3576 Adjust -= 2;
3577 break;
3578 }
3579 }
Evan Chengff310732010-10-28 06:47:08 +00003580 }
3581
Diana Picus92423ce2016-06-27 09:08:23 +00003582 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003583 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003584 default: break;
3585 case ARM::VLD1q8:
3586 case ARM::VLD1q16:
3587 case ARM::VLD1q32:
3588 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003589 case ARM::VLD1q8wb_fixed:
3590 case ARM::VLD1q16wb_fixed:
3591 case ARM::VLD1q32wb_fixed:
3592 case ARM::VLD1q64wb_fixed:
3593 case ARM::VLD1q8wb_register:
3594 case ARM::VLD1q16wb_register:
3595 case ARM::VLD1q32wb_register:
3596 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003597 case ARM::VLD2d8:
3598 case ARM::VLD2d16:
3599 case ARM::VLD2d32:
3600 case ARM::VLD2q8:
3601 case ARM::VLD2q16:
3602 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003603 case ARM::VLD2d8wb_fixed:
3604 case ARM::VLD2d16wb_fixed:
3605 case ARM::VLD2d32wb_fixed:
3606 case ARM::VLD2q8wb_fixed:
3607 case ARM::VLD2q16wb_fixed:
3608 case ARM::VLD2q32wb_fixed:
3609 case ARM::VLD2d8wb_register:
3610 case ARM::VLD2d16wb_register:
3611 case ARM::VLD2d32wb_register:
3612 case ARM::VLD2q8wb_register:
3613 case ARM::VLD2q16wb_register:
3614 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003615 case ARM::VLD3d8:
3616 case ARM::VLD3d16:
3617 case ARM::VLD3d32:
3618 case ARM::VLD1d64T:
3619 case ARM::VLD3d8_UPD:
3620 case ARM::VLD3d16_UPD:
3621 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003622 case ARM::VLD1d64Twb_fixed:
3623 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003624 case ARM::VLD3q8_UPD:
3625 case ARM::VLD3q16_UPD:
3626 case ARM::VLD3q32_UPD:
3627 case ARM::VLD4d8:
3628 case ARM::VLD4d16:
3629 case ARM::VLD4d32:
3630 case ARM::VLD1d64Q:
3631 case ARM::VLD4d8_UPD:
3632 case ARM::VLD4d16_UPD:
3633 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003634 case ARM::VLD1d64Qwb_fixed:
3635 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003636 case ARM::VLD4q8_UPD:
3637 case ARM::VLD4q16_UPD:
3638 case ARM::VLD4q32_UPD:
3639 case ARM::VLD1DUPq8:
3640 case ARM::VLD1DUPq16:
3641 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003642 case ARM::VLD1DUPq8wb_fixed:
3643 case ARM::VLD1DUPq16wb_fixed:
3644 case ARM::VLD1DUPq32wb_fixed:
3645 case ARM::VLD1DUPq8wb_register:
3646 case ARM::VLD1DUPq16wb_register:
3647 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003648 case ARM::VLD2DUPd8:
3649 case ARM::VLD2DUPd16:
3650 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003651 case ARM::VLD2DUPd8wb_fixed:
3652 case ARM::VLD2DUPd16wb_fixed:
3653 case ARM::VLD2DUPd32wb_fixed:
3654 case ARM::VLD2DUPd8wb_register:
3655 case ARM::VLD2DUPd16wb_register:
3656 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003657 case ARM::VLD4DUPd8:
3658 case ARM::VLD4DUPd16:
3659 case ARM::VLD4DUPd32:
3660 case ARM::VLD4DUPd8_UPD:
3661 case ARM::VLD4DUPd16_UPD:
3662 case ARM::VLD4DUPd32_UPD:
3663 case ARM::VLD1LNd8:
3664 case ARM::VLD1LNd16:
3665 case ARM::VLD1LNd32:
3666 case ARM::VLD1LNd8_UPD:
3667 case ARM::VLD1LNd16_UPD:
3668 case ARM::VLD1LNd32_UPD:
3669 case ARM::VLD2LNd8:
3670 case ARM::VLD2LNd16:
3671 case ARM::VLD2LNd32:
3672 case ARM::VLD2LNq16:
3673 case ARM::VLD2LNq32:
3674 case ARM::VLD2LNd8_UPD:
3675 case ARM::VLD2LNd16_UPD:
3676 case ARM::VLD2LNd32_UPD:
3677 case ARM::VLD2LNq16_UPD:
3678 case ARM::VLD2LNq32_UPD:
3679 case ARM::VLD4LNd8:
3680 case ARM::VLD4LNd16:
3681 case ARM::VLD4LNd32:
3682 case ARM::VLD4LNq16:
3683 case ARM::VLD4LNq32:
3684 case ARM::VLD4LNd8_UPD:
3685 case ARM::VLD4LNd16_UPD:
3686 case ARM::VLD4LNd32_UPD:
3687 case ARM::VLD4LNq16_UPD:
3688 case ARM::VLD4LNq32_UPD:
3689 // If the address is not 64-bit aligned, the latencies of these
3690 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003691 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003692 break;
3693 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003694 }
3695 return Adjust;
3696}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003697
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003698int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3699 const MachineInstr &DefMI,
3700 unsigned DefIdx,
3701 const MachineInstr &UseMI,
3702 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003703 // No operand latency. The caller may fall back to getInstrLatency.
3704 if (!ItinData || ItinData->isEmpty())
3705 return -1;
3706
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003707 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003708 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003709
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003710 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003711 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003712 if (DefMI.isBundle())
3713 ResolvedDefMI =
3714 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
3715 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
3716 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003717 return 1;
3718 }
3719
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003720 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003721 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003722 if (UseMI.isBundle()) {
3723 ResolvedUseMI =
3724 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
3725 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00003726 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003727 }
3728
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003729 return getOperandLatencyImpl(
3730 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
3731 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
3732}
3733
3734int ARMBaseInstrInfo::getOperandLatencyImpl(
3735 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
3736 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
3737 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
3738 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003739 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003740 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003741 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003742 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003743 }
3744
3745 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003746 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003747 return 0;
3748
3749 // Otherwise it takes the instruction latency (generally one).
3750 unsigned Latency = getInstrLatency(ItinData, DefMI);
3751
3752 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3753 // its uses. Instructions which are otherwise scheduled between them may
3754 // incur a code size penalty (not able to use the CPSR setting 16-bit
3755 // instructions).
3756 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003757 const MachineFunction *MF = DefMI.getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003758 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003759 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003760 --Latency;
3761 }
3762 return Latency;
3763 }
3764
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003765 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00003766 return -1;
3767
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003768 unsigned DefAlign = DefMI.hasOneMemOperand()
3769 ? (*DefMI.memoperands_begin())->getAlignment()
3770 : 0;
3771 unsigned UseAlign = UseMI.hasOneMemOperand()
3772 ? (*UseMI.memoperands_begin())->getAlignment()
3773 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003774
3775 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003776 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
3777 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003778 // Unable to find operand latency. The caller may resort to getInstrLatency.
3779 if (Latency < 0)
3780 return Latency;
3781
3782 // Adjust for IT block position.
3783 int Adj = DefAdj + UseAdj;
3784
3785 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3786 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3787 if (Adj >= 0 || (int)Latency > -Adj) {
3788 return Latency + Adj;
3789 }
3790 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003791 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003792}
3793
3794int
3795ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3796 SDNode *DefNode, unsigned DefIdx,
3797 SDNode *UseNode, unsigned UseIdx) const {
3798 if (!DefNode->isMachineOpcode())
3799 return 1;
3800
Evan Cheng6cc775f2011-06-28 19:10:37 +00003801 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003802
Evan Cheng6cc775f2011-06-28 19:10:37 +00003803 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003804 return 0;
3805
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003806 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003807 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003808
Evan Cheng6c1414f2010-10-29 18:09:28 +00003809 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003810 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00003811 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
3812 int Threshold = 1 + Adj;
3813 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00003814 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003815
Evan Cheng6cc775f2011-06-28 19:10:37 +00003816 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003817 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3818 unsigned DefAlign = !DefMN->memoperands_empty()
3819 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3820 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3821 unsigned UseAlign = !UseMN->memoperands_empty()
3822 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003823 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3824 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003825
3826 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003827 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3828 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003829 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3830 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003831 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003832 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003833 case ARM::LDRrs:
3834 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003835 unsigned ShOpVal =
3836 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3837 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3838 if (ShImm == 0 ||
3839 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3840 --Latency;
3841 break;
3842 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003843 case ARM::t2LDRs:
3844 case ARM::t2LDRBs:
3845 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003846 case ARM::t2LDRSHs: {
3847 // Thumb2 mode: lsl only.
3848 unsigned ShAmt =
3849 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3850 if (ShAmt == 0 || ShAmt == 2)
3851 --Latency;
3852 break;
3853 }
3854 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003855 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3856 // FIXME: Properly handle all of the latency adjustments for address
3857 // writeback.
3858 switch (DefMCID.getOpcode()) {
3859 default: break;
3860 case ARM::LDRrs:
3861 case ARM::LDRBrs: {
3862 unsigned ShOpVal =
3863 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3864 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3865 if (ShImm == 0 ||
3866 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3867 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3868 Latency -= 2;
3869 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3870 --Latency;
3871 break;
3872 }
3873 case ARM::t2LDRs:
3874 case ARM::t2LDRBs:
3875 case ARM::t2LDRHs:
3876 case ARM::t2LDRSHs: {
3877 // Thumb2 mode: lsl 0-3 only.
3878 Latency -= 2;
3879 break;
3880 }
3881 }
Evan Chengff310732010-10-28 06:47:08 +00003882 }
3883
Diana Picus92423ce2016-06-27 09:08:23 +00003884 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003885 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003886 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003887 case ARM::VLD1q8:
3888 case ARM::VLD1q16:
3889 case ARM::VLD1q32:
3890 case ARM::VLD1q64:
3891 case ARM::VLD1q8wb_register:
3892 case ARM::VLD1q16wb_register:
3893 case ARM::VLD1q32wb_register:
3894 case ARM::VLD1q64wb_register:
3895 case ARM::VLD1q8wb_fixed:
3896 case ARM::VLD1q16wb_fixed:
3897 case ARM::VLD1q32wb_fixed:
3898 case ARM::VLD1q64wb_fixed:
3899 case ARM::VLD2d8:
3900 case ARM::VLD2d16:
3901 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003902 case ARM::VLD2q8Pseudo:
3903 case ARM::VLD2q16Pseudo:
3904 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003905 case ARM::VLD2d8wb_fixed:
3906 case ARM::VLD2d16wb_fixed:
3907 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003908 case ARM::VLD2q8PseudoWB_fixed:
3909 case ARM::VLD2q16PseudoWB_fixed:
3910 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003911 case ARM::VLD2d8wb_register:
3912 case ARM::VLD2d16wb_register:
3913 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003914 case ARM::VLD2q8PseudoWB_register:
3915 case ARM::VLD2q16PseudoWB_register:
3916 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003917 case ARM::VLD3d8Pseudo:
3918 case ARM::VLD3d16Pseudo:
3919 case ARM::VLD3d32Pseudo:
3920 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003921 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003922 case ARM::VLD3d8Pseudo_UPD:
3923 case ARM::VLD3d16Pseudo_UPD:
3924 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003925 case ARM::VLD3q8Pseudo_UPD:
3926 case ARM::VLD3q16Pseudo_UPD:
3927 case ARM::VLD3q32Pseudo_UPD:
3928 case ARM::VLD3q8oddPseudo:
3929 case ARM::VLD3q16oddPseudo:
3930 case ARM::VLD3q32oddPseudo:
3931 case ARM::VLD3q8oddPseudo_UPD:
3932 case ARM::VLD3q16oddPseudo_UPD:
3933 case ARM::VLD3q32oddPseudo_UPD:
3934 case ARM::VLD4d8Pseudo:
3935 case ARM::VLD4d16Pseudo:
3936 case ARM::VLD4d32Pseudo:
3937 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003938 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003939 case ARM::VLD4d8Pseudo_UPD:
3940 case ARM::VLD4d16Pseudo_UPD:
3941 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003942 case ARM::VLD4q8Pseudo_UPD:
3943 case ARM::VLD4q16Pseudo_UPD:
3944 case ARM::VLD4q32Pseudo_UPD:
3945 case ARM::VLD4q8oddPseudo:
3946 case ARM::VLD4q16oddPseudo:
3947 case ARM::VLD4q32oddPseudo:
3948 case ARM::VLD4q8oddPseudo_UPD:
3949 case ARM::VLD4q16oddPseudo_UPD:
3950 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003951 case ARM::VLD1DUPq8:
3952 case ARM::VLD1DUPq16:
3953 case ARM::VLD1DUPq32:
3954 case ARM::VLD1DUPq8wb_fixed:
3955 case ARM::VLD1DUPq16wb_fixed:
3956 case ARM::VLD1DUPq32wb_fixed:
3957 case ARM::VLD1DUPq8wb_register:
3958 case ARM::VLD1DUPq16wb_register:
3959 case ARM::VLD1DUPq32wb_register:
3960 case ARM::VLD2DUPd8:
3961 case ARM::VLD2DUPd16:
3962 case ARM::VLD2DUPd32:
3963 case ARM::VLD2DUPd8wb_fixed:
3964 case ARM::VLD2DUPd16wb_fixed:
3965 case ARM::VLD2DUPd32wb_fixed:
3966 case ARM::VLD2DUPd8wb_register:
3967 case ARM::VLD2DUPd16wb_register:
3968 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003969 case ARM::VLD4DUPd8Pseudo:
3970 case ARM::VLD4DUPd16Pseudo:
3971 case ARM::VLD4DUPd32Pseudo:
3972 case ARM::VLD4DUPd8Pseudo_UPD:
3973 case ARM::VLD4DUPd16Pseudo_UPD:
3974 case ARM::VLD4DUPd32Pseudo_UPD:
3975 case ARM::VLD1LNq8Pseudo:
3976 case ARM::VLD1LNq16Pseudo:
3977 case ARM::VLD1LNq32Pseudo:
3978 case ARM::VLD1LNq8Pseudo_UPD:
3979 case ARM::VLD1LNq16Pseudo_UPD:
3980 case ARM::VLD1LNq32Pseudo_UPD:
3981 case ARM::VLD2LNd8Pseudo:
3982 case ARM::VLD2LNd16Pseudo:
3983 case ARM::VLD2LNd32Pseudo:
3984 case ARM::VLD2LNq16Pseudo:
3985 case ARM::VLD2LNq32Pseudo:
3986 case ARM::VLD2LNd8Pseudo_UPD:
3987 case ARM::VLD2LNd16Pseudo_UPD:
3988 case ARM::VLD2LNd32Pseudo_UPD:
3989 case ARM::VLD2LNq16Pseudo_UPD:
3990 case ARM::VLD2LNq32Pseudo_UPD:
3991 case ARM::VLD4LNd8Pseudo:
3992 case ARM::VLD4LNd16Pseudo:
3993 case ARM::VLD4LNd32Pseudo:
3994 case ARM::VLD4LNq16Pseudo:
3995 case ARM::VLD4LNq32Pseudo:
3996 case ARM::VLD4LNd8Pseudo_UPD:
3997 case ARM::VLD4LNd16Pseudo_UPD:
3998 case ARM::VLD4LNd32Pseudo_UPD:
3999 case ARM::VLD4LNq16Pseudo_UPD:
4000 case ARM::VLD4LNq32Pseudo_UPD:
4001 // If the address is not 64-bit aligned, the latencies of these
4002 // instructions increases by one.
4003 ++Latency;
4004 break;
4005 }
4006
Evan Chengff310732010-10-28 06:47:08 +00004007 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004008}
Evan Cheng63c76082010-10-19 18:58:51 +00004009
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004010unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4011 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4012 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004013 return 0;
4014
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004015 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004016 return 0;
4017
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004018 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004019
4020 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
4021 // When predicated, CPSR is an additional source operand for CPSR updating
4022 // instructions, this apparently increases their latencies.
4023 return 1;
4024 }
4025 return 0;
4026}
4027
Andrew Trick45446062012-06-05 21:11:27 +00004028unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004029 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004030 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004031 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4032 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004033 return 1;
4034
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004035 // An instruction scheduler typically runs on unbundled instructions, however
4036 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004037 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004038 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004039 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4040 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004041 while (++I != E && I->isInsideBundle()) {
4042 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004043 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004044 }
4045 return Latency;
4046 }
4047
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004048 const MCInstrDesc &MCID = MI.getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004049 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004050 // When predicated, CPSR is an additional source operand for CPSR updating
4051 // instructions, this apparently increases their latencies.
4052 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004053 }
4054 // Be sure to call getStageLatency for an empty itinerary in case it has a
4055 // valid MinLatency property.
4056 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004057 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004058
4059 unsigned Class = MCID.getSchedClass();
4060
4061 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004062 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004063 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004064
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004065 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004066 unsigned Latency = ItinData->getStageLatency(Class);
4067
4068 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004069 unsigned DefAlign =
4070 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4071 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004072 if (Adj >= 0 || (int)Latency > -Adj) {
4073 return Latency + Adj;
4074 }
4075 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004076}
4077
4078int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4079 SDNode *Node) const {
4080 if (!Node->isMachineOpcode())
4081 return 1;
4082
4083 if (!ItinData || ItinData->isEmpty())
4084 return 1;
4085
4086 unsigned Opcode = Node->getMachineOpcode();
4087 switch (Opcode) {
4088 default:
4089 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004090 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004091 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004092 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004093 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004094}
4095
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004096bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4097 const MachineRegisterInfo *MRI,
4098 const MachineInstr &DefMI,
4099 unsigned DefIdx,
4100 const MachineInstr &UseMI,
4101 unsigned UseIdx) const {
4102 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4103 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004104 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004105 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004106 return true;
4107
4108 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004109 unsigned Latency =
4110 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004111 if (Latency <= 3)
4112 return false;
4113 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4114 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4115}
Evan Chenge96b8d72010-10-26 02:08:50 +00004116
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004117bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4118 const MachineInstr &DefMI,
4119 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004120 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004121 if (!ItinData || ItinData->isEmpty())
4122 return false;
4123
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004124 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004125 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004126 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004127 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4128 return (DefCycle != -1 && DefCycle <= 2);
4129 }
4130 return false;
4131}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004132
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004133bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004134 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004135 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004136 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4137 return false;
4138 }
4139 return true;
4140}
4141
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004142// LoadStackGuard has so far only been implemented for MachO. Different code
4143// sequence is needed for other targets.
4144void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4145 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004146 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004147 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4148 "ROPI/RWPI not currently supported with stack guard");
4149
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004150 MachineBasicBlock &MBB = *MI->getParent();
4151 DebugLoc DL = MI->getDebugLoc();
4152 unsigned Reg = MI->getOperand(0).getReg();
4153 const GlobalValue *GV =
4154 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4155 MachineInstrBuilder MIB;
4156
4157 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4158 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4159
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004160 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004161 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4162 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004163 auto Flags = MachineMemOperand::MOLoad |
4164 MachineMemOperand::MODereferenceable |
4165 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004166 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004167 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004168 MIB.addMemOperand(MMO);
4169 AddDefaultPred(MIB);
4170 }
4171
4172 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4173 MIB.addReg(Reg, RegState::Kill).addImm(0);
4174 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4175 AddDefaultPred(MIB);
4176}
4177
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004178bool
4179ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4180 unsigned &AddSubOpc,
4181 bool &NegAcc, bool &HasLane) const {
4182 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4183 if (I == MLxEntryMap.end())
4184 return false;
4185
4186 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4187 MulOpc = Entry.MulOpc;
4188 AddSubOpc = Entry.AddSubOpc;
4189 NegAcc = Entry.NegAcc;
4190 HasLane = Entry.HasLane;
4191 return true;
4192}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004193
4194//===----------------------------------------------------------------------===//
4195// Execution domains.
4196//===----------------------------------------------------------------------===//
4197//
4198// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4199// and some can go down both. The vmov instructions go down the VFP pipeline,
4200// but they can be changed to vorr equivalents that are executed by the NEON
4201// pipeline.
4202//
4203// We use the following execution domain numbering:
4204//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004205enum ARMExeDomain {
4206 ExeGeneric = 0,
4207 ExeVFP = 1,
4208 ExeNEON = 2
4209};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004210//
4211// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4212//
4213std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004214ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004215 // If we don't have access to NEON instructions then we won't be able
4216 // to swizzle anything to the NEON domain. Check to make sure.
4217 if (Subtarget.hasNEON()) {
4218 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4219 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004220 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004221 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004222
Eric Christopher7e70aba2015-03-07 00:12:22 +00004223 // CortexA9 is particularly picky about mixing the two and wants these
4224 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004225 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4226 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4227 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004228 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4229 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004230 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004231 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004232
4233 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004234 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004235
4236 // Certain instructions can go either way on Cortex-A8.
4237 // Treat them as NEON instructions.
4238 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004239 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004240
4241 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004242 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004243
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004244 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004245}
4246
Tim Northover771f1602012-08-29 16:36:07 +00004247static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4248 unsigned SReg, unsigned &Lane) {
4249 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4250 Lane = 0;
4251
4252 if (DReg != ARM::NoRegister)
4253 return DReg;
4254
4255 Lane = 1;
4256 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4257
4258 assert(DReg && "S-register with no D super-register?");
4259 return DReg;
4260}
4261
Andrew Trickd9296ec2012-10-10 05:43:01 +00004262/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004263/// set ImplicitSReg to a register number that must be marked as implicit-use or
4264/// zero if no register needs to be defined as implicit-use.
4265///
4266/// If the function cannot determine if an SPR should be marked implicit use or
4267/// not, it returns false.
4268///
4269/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004270/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004271/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4272/// lane of the DPR).
4273///
4274/// If the other SPR is defined, an implicit-use of it should be added. Else,
4275/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004276///
James Molloyea052562012-09-18 08:31:15 +00004277static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004278 MachineInstr &MI, unsigned DReg,
4279 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004280 // If the DPR is defined or used already, the other SPR lane will be chained
4281 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004282 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004283 ImplicitSReg = 0;
4284 return true;
4285 }
4286
4287 // Otherwise we need to go searching to see if the SPR is set explicitly.
4288 ImplicitSReg = TRI->getSubReg(DReg,
4289 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4290 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004291 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004292
4293 if (LQR == MachineBasicBlock::LQR_Live)
4294 return true;
4295 else if (LQR == MachineBasicBlock::LQR_Unknown)
4296 return false;
4297
4298 // If the register is known not to be live, there is no need to add an
4299 // implicit-use.
4300 ImplicitSReg = 0;
4301 return true;
4302}
Tim Northover771f1602012-08-29 16:36:07 +00004303
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004304void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4305 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004306 unsigned DstReg, SrcReg, DReg;
4307 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004308 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004309 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004310 switch (MI.getOpcode()) {
4311 default:
4312 llvm_unreachable("cannot handle opcode!");
4313 break;
4314 case ARM::VMOVD:
4315 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004316 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004317
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004318 // Zap the predicate operands.
4319 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004320
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004321 // Make sure we've got NEON instructions.
4322 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004323
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004324 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4325 DstReg = MI.getOperand(0).getReg();
4326 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004327
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004328 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4329 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004330
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004331 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4332 MI.setDesc(get(ARM::VORRd));
4333 AddDefaultPred(
4334 MIB.addReg(DstReg, RegState::Define).addReg(SrcReg).addReg(SrcReg));
4335 break;
4336 case ARM::VMOVRS:
4337 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004338 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004339 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004340
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004341 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4342 DstReg = MI.getOperand(0).getReg();
4343 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004344
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004345 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4346 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004347
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004348 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004349
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004350 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4351 // Note that DSrc has been widened and the other lane may be undef, which
4352 // contaminates the entire register.
4353 MI.setDesc(get(ARM::VGETLNi32));
4354 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4355 .addReg(DReg, RegState::Undef)
4356 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004357
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004358 // The old source should be an implicit use, otherwise we might think it
4359 // was dead before here.
4360 MIB.addReg(SrcReg, RegState::Implicit);
4361 break;
4362 case ARM::VMOVSR: {
4363 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004364 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004365 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004366
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004367 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4368 DstReg = MI.getOperand(0).getReg();
4369 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004370
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004371 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004372
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004373 unsigned ImplicitSReg;
4374 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004375 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004376
4377 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4378 MI.RemoveOperand(i - 1);
4379
4380 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4381 // Again DDst may be undefined at the beginning of this instruction.
4382 MI.setDesc(get(ARM::VSETLNi32));
4383 MIB.addReg(DReg, RegState::Define)
4384 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4385 .addReg(SrcReg)
4386 .addImm(Lane);
4387 AddDefaultPred(MIB);
4388
4389 // The narrower destination must be marked as set to keep previous chains
4390 // in place.
4391 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4392 if (ImplicitSReg != 0)
4393 MIB.addReg(ImplicitSReg, RegState::Implicit);
4394 break;
James Molloyea052562012-09-18 08:31:15 +00004395 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004396 case ARM::VMOVS: {
4397 if (Domain != ExeNEON)
4398 break;
4399
4400 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004401 DstReg = MI.getOperand(0).getReg();
4402 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004403
Tim Northoverca9f3842012-08-30 10:17:45 +00004404 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4405 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4406 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4407
James Molloyea052562012-09-18 08:31:15 +00004408 unsigned ImplicitSReg;
4409 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4410 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004411
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004412 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4413 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004414
Tim Northoverca9f3842012-08-30 10:17:45 +00004415 if (DSrc == DDst) {
4416 // Destination can be:
4417 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004418 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004419 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004420 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
4421 .addImm(SrcLane);
Tim Northover726d32c2012-09-01 18:07:29 +00004422 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004423
4424 // Neither the source or the destination are naturally represented any
4425 // more, so add them in manually.
4426 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4427 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004428 if (ImplicitSReg != 0)
4429 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004430 break;
4431 }
4432
4433 // In general there's no single instruction that can perform an S <-> S
4434 // move in NEON space, but a pair of VEXT instructions *can* do the
4435 // job. It turns out that the VEXTs needed will only use DSrc once, with
4436 // the position based purely on the combination of lane-0 and lane-1
4437 // involved. For example
4438 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4439 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4440 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4441 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4442 //
4443 // Pattern of the MachineInstrs is:
4444 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4445 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004446 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4447 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004448
4449 // On the first instruction, both DSrc and DDst may be <undef> if present.
4450 // Specifically when the original instruction didn't have them as an
4451 // <imp-use>.
4452 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004453 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004454 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4455
4456 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004457 CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004458 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4459
Tim Northoverca9f3842012-08-30 10:17:45 +00004460 NewMIB.addImm(1);
4461 AddDefaultPred(NewMIB);
4462
4463 if (SrcLane == DstLane)
4464 NewMIB.addReg(SrcReg, RegState::Implicit);
4465
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004466 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004467 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004468
4469 // On the second instruction, DDst has definitely been defined above, so
4470 // it is not <undef>. DSrc, if present, can be <undef> as above.
4471 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004472 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004473 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4474
4475 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004476 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004477 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4478
Tim Northoverca9f3842012-08-30 10:17:45 +00004479 MIB.addImm(1);
4480 AddDefaultPred(MIB);
4481
4482 if (SrcLane != DstLane)
4483 MIB.addReg(SrcReg, RegState::Implicit);
4484
4485 // As before, the original destination is no longer represented, add it
4486 // implicitly.
4487 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004488 if (ImplicitSReg != 0)
4489 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004490 break;
4491 }
Tim Northoverf6618152012-08-17 11:32:52 +00004492 }
4493
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004494}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004495
Bob Wilsone8a549c2012-09-29 21:43:49 +00004496//===----------------------------------------------------------------------===//
4497// Partial register updates
4498//===----------------------------------------------------------------------===//
4499//
4500// Swift renames NEON registers with 64-bit granularity. That means any
4501// instruction writing an S-reg implicitly reads the containing D-reg. The
4502// problem is mostly avoided by translating f32 operations to v2f32 operations
4503// on D-registers, but f32 loads are still a problem.
4504//
4505// These instructions can load an f32 into a NEON register:
4506//
4507// VLDRS - Only writes S, partial D update.
4508// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4509// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4510//
4511// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004512unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4513 const MachineInstr &MI, unsigned OpNum,
4514 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004515 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4516 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004517 return 0;
4518
4519 assert(TRI && "Need TRI instance");
4520
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004521 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004522 if (MO.readsReg())
4523 return 0;
4524 unsigned Reg = MO.getReg();
4525 int UseOp = -1;
4526
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004527 switch (MI.getOpcode()) {
4528 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004529 case ARM::VLDRS:
4530 case ARM::FCONSTS:
4531 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004532 case ARM::VMOVv8i8:
4533 case ARM::VMOVv4i16:
4534 case ARM::VMOVv2i32:
4535 case ARM::VMOVv2f32:
4536 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004537 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004538 break;
4539
4540 // Explicitly reads the dependency.
4541 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004542 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004543 break;
4544 default:
4545 return 0;
4546 }
4547
4548 // If this instruction actually reads a value from Reg, there is no unwanted
4549 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004550 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00004551 return 0;
4552
4553 // We must be able to clobber the whole D-reg.
4554 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4555 // Virtual register must be a foo:ssub_0<def,undef> operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004556 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004557 return 0;
4558 } else if (ARM::SPRRegClass.contains(Reg)) {
4559 // Physical register: MI must define the full D-reg.
4560 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4561 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004562 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004563 return 0;
4564 }
4565
4566 // MI has an unwanted D-register dependency.
4567 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00004568 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004569}
4570
4571// Break a partial register dependency after getPartialRegUpdateClearance
4572// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004573void ARMBaseInstrInfo::breakPartialRegDependency(
4574 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4575 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004576 assert(TRI && "Need TRI instance");
4577
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004578 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004579 unsigned Reg = MO.getReg();
4580 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4581 "Can't break virtual register dependencies.");
4582 unsigned DReg = Reg;
4583
4584 // If MI defines an S-reg, find the corresponding D super-register.
4585 if (ARM::SPRRegClass.contains(Reg)) {
4586 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4587 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4588 }
4589
4590 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004591 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004592
4593 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4594 // the full D-register by loading the same value to both lanes. The
4595 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004596 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004597 // too big regressions.
4598
4599 // Insert the dependency-breaking FCONSTD before MI.
4600 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004601 AddDefaultPred(
4602 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4603 .addImm(96));
4604 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004605}
4606
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004607bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004608 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004609}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004610
4611bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004612 if (MI->getNumOperands() < 4)
4613 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004614 unsigned ShOpVal = MI->getOperand(3).getImm();
4615 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4616 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4617 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4618 ((ShImm == 1 || ShImm == 2) &&
4619 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4620 return true;
4621
4622 return false;
4623}
Quentin Colombetd358e842014-08-22 18:05:22 +00004624
4625bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4626 const MachineInstr &MI, unsigned DefIdx,
4627 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4628 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4629 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4630
4631 switch (MI.getOpcode()) {
4632 case ARM::VMOVDRR:
4633 // dX = VMOVDRR rY, rZ
4634 // is the same as:
4635 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4636 // Populate the InputRegs accordingly.
4637 // rY
4638 const MachineOperand *MOReg = &MI.getOperand(1);
4639 InputRegs.push_back(
4640 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4641 // rZ
4642 MOReg = &MI.getOperand(2);
4643 InputRegs.push_back(
4644 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4645 return true;
4646 }
4647 llvm_unreachable("Target dependent opcode missing");
4648}
4649
4650bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4651 const MachineInstr &MI, unsigned DefIdx,
4652 RegSubRegPairAndIdx &InputReg) const {
4653 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4654 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4655
4656 switch (MI.getOpcode()) {
4657 case ARM::VMOVRRD:
4658 // rX, rY = VMOVRRD dZ
4659 // is the same as:
4660 // rX = EXTRACT_SUBREG dZ, ssub_0
4661 // rY = EXTRACT_SUBREG dZ, ssub_1
4662 const MachineOperand &MOReg = MI.getOperand(2);
4663 InputReg.Reg = MOReg.getReg();
4664 InputReg.SubReg = MOReg.getSubReg();
4665 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4666 return true;
4667 }
4668 llvm_unreachable("Target dependent opcode missing");
4669}
4670
4671bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4672 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4673 RegSubRegPairAndIdx &InsertedReg) const {
4674 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4675 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4676
4677 switch (MI.getOpcode()) {
4678 case ARM::VSETLNi32:
4679 // dX = VSETLNi32 dY, rZ, imm
4680 const MachineOperand &MOBaseReg = MI.getOperand(1);
4681 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4682 const MachineOperand &MOIndex = MI.getOperand(3);
4683 BaseReg.Reg = MOBaseReg.getReg();
4684 BaseReg.SubReg = MOBaseReg.getSubReg();
4685
4686 InsertedReg.Reg = MOInsertedReg.getReg();
4687 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4688 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4689 return true;
4690 }
4691 llvm_unreachable("Target dependent opcode missing");
4692}