blob: 9c8229c275d25e47880eecee76e04876e06f9be1 [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.
Tim Northovera9cc3852016-10-26 20:01:00 +00002080 unsigned RegsNeeded;
2081 const TargetRegisterClass *RegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002082 if (IsVFPPushPop) {
Tim Northover93bcc662013-11-08 17:18:07 +00002083 RegsNeeded = NumBytes / 8;
Tim Northovera9cc3852016-10-26 20:01:00 +00002084 RegClass = &ARM::DPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002085 } else {
Tim Northover93bcc662013-11-08 17:18:07 +00002086 RegsNeeded = NumBytes / 4;
Tim Northovera9cc3852016-10-26 20:01:00 +00002087 RegClass = &ARM::GPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002088 }
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;
Tim Northovera9cc3852016-10-26 20:01:00 +00002094
2095 // We're also going to need the first register transferred by this
2096 // instruction, which won't necessarily be the first register in the list.
2097 unsigned FirstRegEnc = -1;
Tim Northover93bcc662013-11-08 17:18:07 +00002098
Tim Northover93bcc662013-11-08 17:18:07 +00002099 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northovera9cc3852016-10-26 20:01:00 +00002100 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2101 MachineOperand &MO = MI->getOperand(i);
2102 RegList.push_back(MO);
2103
2104 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2105 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2106 }
2107
Tim Northover45479dc2013-12-01 14:16:24 +00002108 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002109
2110 // Now try to find enough space in the reglist to allocate NumBytes.
Tim Northovera9cc3852016-10-26 20:01:00 +00002111 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2112 --CurRegEnc) {
2113 unsigned CurReg = RegClass->getRegister(CurRegEnc);
Tim Northover93bcc662013-11-08 17:18:07 +00002114 if (!IsPop) {
2115 // Pushing any register is completely harmless, mark the
2116 // register involved as undef since we don't care about it in
2117 // the slightest.
2118 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2119 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002120 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002121 continue;
2122 }
2123
Tim Northover45479dc2013-12-01 14:16:24 +00002124 // However, we can only pop an extra register if it's not live. For
2125 // registers live within the function we might clobber a return value
2126 // register; the other way a register can be live here is if it's
2127 // callee-saved.
2128 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002129 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2130 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002131 // VFP pops don't allow holes in the register list, so any skip is fatal
2132 // for our transformation. GPR pops do, so we should just keep looking.
2133 if (IsVFPPushPop)
2134 return false;
2135 else
2136 continue;
2137 }
Tim Northover93bcc662013-11-08 17:18:07 +00002138
2139 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002140 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2141 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002142 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002143 }
2144
2145 if (RegsNeeded > 0)
2146 return false;
2147
2148 // Finally we know we can profitably perform the optimisation so go
2149 // ahead: strip all existing registers off and add them back again
2150 // in the right order.
2151 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2152 MI->RemoveOperand(i);
2153
2154 // Add the complete list back in.
2155 MachineInstrBuilder MIB(MF, &*MI);
2156 for (int i = RegList.size() - 1; i >= 0; --i)
2157 MIB.addOperand(RegList[i]);
2158
2159 return true;
2160}
2161
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002162bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2163 unsigned FrameReg, int &Offset,
2164 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002165 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002166 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002167 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2168 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002169
Evan Cheng780748d2009-07-28 05:48:47 +00002170 // Memory operands in inline assembly always use AddrMode2.
2171 if (Opcode == ARM::INLINEASM)
2172 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002173
Evan Cheng780748d2009-07-28 05:48:47 +00002174 if (Opcode == ARM::ADDri) {
2175 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2176 if (Offset == 0) {
2177 // Turn it into a move.
2178 MI.setDesc(TII.get(ARM::MOVr));
2179 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2180 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002181 Offset = 0;
2182 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002183 } else if (Offset < 0) {
2184 Offset = -Offset;
2185 isSub = true;
2186 MI.setDesc(TII.get(ARM::SUBri));
2187 }
2188
2189 // Common case: small offset, fits into instruction.
2190 if (ARM_AM::getSOImmVal(Offset) != -1) {
2191 // Replace the FrameIndex with sp / fp
2192 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2193 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002194 Offset = 0;
2195 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002196 }
2197
2198 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2199 // as possible.
2200 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2201 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2202
2203 // We will handle these bits from offset, clear them.
2204 Offset &= ~ThisImmVal;
2205
2206 // Get the properly encoded SOImmVal field.
2207 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2208 "Bit extraction didn't work?");
2209 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2210 } else {
2211 unsigned ImmIdx = 0;
2212 int InstrOffs = 0;
2213 unsigned NumBits = 0;
2214 unsigned Scale = 1;
2215 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002216 case ARMII::AddrMode_i12: {
2217 ImmIdx = FrameRegIdx + 1;
2218 InstrOffs = MI.getOperand(ImmIdx).getImm();
2219 NumBits = 12;
2220 break;
2221 }
Evan Cheng780748d2009-07-28 05:48:47 +00002222 case ARMII::AddrMode2: {
2223 ImmIdx = FrameRegIdx+2;
2224 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2225 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2226 InstrOffs *= -1;
2227 NumBits = 12;
2228 break;
2229 }
2230 case ARMII::AddrMode3: {
2231 ImmIdx = FrameRegIdx+2;
2232 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2233 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2234 InstrOffs *= -1;
2235 NumBits = 8;
2236 break;
2237 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002238 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002239 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002240 // Can't fold any offset even if it's zero.
2241 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002242 case ARMII::AddrMode5: {
2243 ImmIdx = FrameRegIdx+1;
2244 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2245 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2246 InstrOffs *= -1;
2247 NumBits = 8;
2248 Scale = 4;
2249 break;
2250 }
2251 default:
2252 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002253 }
2254
2255 Offset += InstrOffs * Scale;
2256 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2257 if (Offset < 0) {
2258 Offset = -Offset;
2259 isSub = true;
2260 }
2261
2262 // Attempt to fold address comp. if opcode has offset bits
2263 if (NumBits > 0) {
2264 // Common case: small offset, fits into instruction.
2265 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2266 int ImmedOffset = Offset / Scale;
2267 unsigned Mask = (1 << NumBits) - 1;
2268 if ((unsigned)Offset <= Mask * Scale) {
2269 // Replace the FrameIndex with sp
2270 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002271 // FIXME: When addrmode2 goes away, this will simplify (like the
2272 // T2 version), as the LDR.i12 versions don't need the encoding
2273 // tricks for the offset value.
2274 if (isSub) {
2275 if (AddrMode == ARMII::AddrMode_i12)
2276 ImmedOffset = -ImmedOffset;
2277 else
2278 ImmedOffset |= 1 << NumBits;
2279 }
Evan Cheng780748d2009-07-28 05:48:47 +00002280 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002281 Offset = 0;
2282 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002283 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002284
Evan Cheng780748d2009-07-28 05:48:47 +00002285 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2286 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002287 if (isSub) {
2288 if (AddrMode == ARMII::AddrMode_i12)
2289 ImmedOffset = -ImmedOffset;
2290 else
2291 ImmedOffset |= 1 << NumBits;
2292 }
Evan Cheng780748d2009-07-28 05:48:47 +00002293 ImmOp.ChangeToImmediate(ImmedOffset);
2294 Offset &= ~(Mask*Scale);
2295 }
2296 }
2297
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002298 Offset = (isSub) ? -Offset : Offset;
2299 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002300}
Bill Wendling7de9d522010-08-06 01:32:48 +00002301
Manman Ren6fa76dc2012-06-29 21:33:59 +00002302/// analyzeCompare - For a comparison instruction, return the source registers
2303/// in SrcReg and SrcReg2 if having two register operands, and the value it
2304/// compares against in CmpValue. Return true if the comparison instruction
2305/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002306bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2307 unsigned &SrcReg2, int &CmpMask,
2308 int &CmpValue) const {
2309 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002310 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002311 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002312 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002313 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002314 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002315 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002316 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002317 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002318 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002319 case ARM::CMPrr:
2320 case ARM::t2CMPrr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002321 SrcReg = MI.getOperand(0).getReg();
2322 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002323 CmpMask = ~0;
2324 CmpValue = 0;
2325 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002326 case ARM::TSTri:
2327 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002328 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002329 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002330 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002331 CmpValue = 0;
2332 return true;
2333 }
2334
2335 return false;
2336}
2337
Gabor Greifd36e3e82010-09-29 10:12:08 +00002338/// isSuitableForMask - Identify a suitable 'and' instruction that
2339/// operates on the given source register and applies the same mask
2340/// as a 'tst' instruction. Provide a limited look-through for copies.
2341/// When successful, MI will hold the found instruction.
2342static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002343 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002344 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002345 case ARM::ANDri:
2346 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002347 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002348 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002349 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002350 return true;
2351 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002352 }
2353
2354 return false;
2355}
2356
Manman Renb1b3db62012-06-29 22:06:19 +00002357/// getSwappedCondition - assume the flags are set by MI(a,b), return
2358/// the condition code if we modify the instructions such that flags are
2359/// set by MI(b,a).
2360inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2361 switch (CC) {
2362 default: return ARMCC::AL;
2363 case ARMCC::EQ: return ARMCC::EQ;
2364 case ARMCC::NE: return ARMCC::NE;
2365 case ARMCC::HS: return ARMCC::LS;
2366 case ARMCC::LO: return ARMCC::HI;
2367 case ARMCC::HI: return ARMCC::LO;
2368 case ARMCC::LS: return ARMCC::HS;
2369 case ARMCC::GE: return ARMCC::LE;
2370 case ARMCC::LT: return ARMCC::GT;
2371 case ARMCC::GT: return ARMCC::LT;
2372 case ARMCC::LE: return ARMCC::GE;
2373 }
2374}
2375
2376/// isRedundantFlagInstr - check whether the first instruction, whose only
2377/// purpose is to update flags, can be made redundant.
2378/// CMPrr can be made redundant by SUBrr if the operands are the same.
2379/// CMPri can be made redundant by SUBri if the operands are the same.
2380/// This function can be extended later on.
2381inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2382 unsigned SrcReg2, int ImmValue,
2383 MachineInstr *OI) {
2384 if ((CmpI->getOpcode() == ARM::CMPrr ||
2385 CmpI->getOpcode() == ARM::t2CMPrr) &&
2386 (OI->getOpcode() == ARM::SUBrr ||
2387 OI->getOpcode() == ARM::t2SUBrr) &&
2388 ((OI->getOperand(1).getReg() == SrcReg &&
2389 OI->getOperand(2).getReg() == SrcReg2) ||
2390 (OI->getOperand(1).getReg() == SrcReg2 &&
2391 OI->getOperand(2).getReg() == SrcReg)))
2392 return true;
2393
2394 if ((CmpI->getOpcode() == ARM::CMPri ||
2395 CmpI->getOpcode() == ARM::t2CMPri) &&
2396 (OI->getOpcode() == ARM::SUBri ||
2397 OI->getOpcode() == ARM::t2SUBri) &&
2398 OI->getOperand(1).getReg() == SrcReg &&
2399 OI->getOperand(2).getImm() == ImmValue)
2400 return true;
2401 return false;
2402}
2403
Manman Ren6fa76dc2012-06-29 21:33:59 +00002404/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2405/// comparison into one that sets the zero bit in the flags register;
2406/// Remove a redundant Compare instruction if an earlier instruction can set the
2407/// flags in the same way as Compare.
2408/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2409/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2410/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002411bool ARMBaseInstrInfo::optimizeCompareInstr(
2412 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2413 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002414 // Get the unique definition of SrcReg.
2415 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2416 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002417
Gabor Greifadbbb932010-09-21 12:01:15 +00002418 // Masked compares sometimes use the same register as the corresponding 'and'.
2419 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002420 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002421 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002422 for (MachineRegisterInfo::use_instr_iterator
2423 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2424 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002425 if (UI->getParent() != CmpInstr.getParent())
2426 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002427 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002428 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002429 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002430 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002431 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002432 break;
2433 }
2434 if (!MI) return false;
2435 }
2436 }
2437
Manman Rendc8ad002012-05-11 01:30:47 +00002438 // Get ready to iterate backward from CmpInstr.
2439 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002440 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002441
2442 // Early exit if CmpInstr is at the beginning of the BB.
2443 if (I == B) return false;
2444
Manman Rendc8ad002012-05-11 01:30:47 +00002445 // There are two possible candidates which can be changed to set CPSR:
2446 // One is MI, the other is a SUB instruction.
2447 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2448 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002449 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002450 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002451 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002452 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002453 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002454 // Conservatively refuse to convert an instruction which isn't in the same
2455 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002456 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2457 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002458 if (CmpInstr.getOpcode() == ARM::CMPri ||
2459 CmpInstr.getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002460 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002461 else
2462 return false;
2463 }
2464
2465 // Check that CPSR isn't set between the comparison instruction and the one we
2466 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002467 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002468 --I;
2469 for (; I != E; --I) {
2470 const MachineInstr &Instr = *I;
2471
Manman Renb1b3db62012-06-29 22:06:19 +00002472 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2473 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002474 // This instruction modifies or uses CPSR after the one we want to
2475 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002476 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002477
Manman Renb1b3db62012-06-29 22:06:19 +00002478 // Check whether CmpInstr can be made redundant by the current instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002479 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002480 Sub = &*I;
2481 break;
2482 }
2483
Evan Chengd757c882010-09-21 23:49:07 +00002484 if (I == B)
2485 // The 'and' is below the comparison instruction.
2486 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002487 }
2488
Manman Rendc8ad002012-05-11 01:30:47 +00002489 // Return false if no candidates exist.
2490 if (!MI && !Sub)
2491 return false;
2492
2493 // The single candidate is called MI.
2494 if (!MI) MI = Sub;
2495
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002496 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002497 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002498 return false;
2499
James Molloy0f412272016-09-09 09:51:06 +00002500 bool IsThumb1 = false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002501 switch (MI->getOpcode()) {
2502 default: break;
James Molloy0f412272016-09-09 09:51:06 +00002503 case ARM::tLSLri:
2504 case ARM::tLSRri:
2505 case ARM::tLSLrr:
2506 case ARM::tLSRrr:
2507 case ARM::tSUBrr:
2508 case ARM::tADDrr:
2509 case ARM::tADDi3:
2510 case ARM::tADDi8:
2511 case ARM::tSUBi3:
2512 case ARM::tSUBi8:
2513 IsThumb1 = true;
2514 LLVM_FALLTHROUGH;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002515 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002516 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002517 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002518 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002519 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002520 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002521 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002522 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002523 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002524 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002525 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002526 case ARM::SBCri:
2527 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002528 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002529 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002530 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002531 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002532 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002533 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002534 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002535 case ARM::t2SBCri:
2536 case ARM::ANDrr:
2537 case ARM::ANDri:
2538 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002539 case ARM::t2ANDri:
2540 case ARM::ORRrr:
2541 case ARM::ORRri:
2542 case ARM::t2ORRrr:
2543 case ARM::t2ORRri:
2544 case ARM::EORrr:
2545 case ARM::EORri:
2546 case ARM::t2EORrr:
James Molloye7d97362016-11-03 14:08:01 +00002547 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002548 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002549 // When checking against MI: if it's a conditional code that requires
2550 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002551 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2552 // If we are done with the basic block, we need to check whether CPSR is
2553 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002554 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2555 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002556 bool isSafe = false;
2557 I = CmpInstr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002558 E = CmpInstr.getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002559 while (!isSafe && ++I != E) {
2560 const MachineInstr &Instr = *I;
2561 for (unsigned IO = 0, EO = Instr.getNumOperands();
2562 !isSafe && IO != EO; ++IO) {
2563 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002564 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2565 isSafe = true;
2566 break;
2567 }
Evan Cheng425489d2011-03-23 22:52:04 +00002568 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2569 continue;
2570 if (MO.isDef()) {
2571 isSafe = true;
2572 break;
2573 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002574 // Condition code is after the operand before CPSR except for VSELs.
2575 ARMCC::CondCodes CC;
2576 bool IsInstrVSel = true;
2577 switch (Instr.getOpcode()) {
2578 default:
2579 IsInstrVSel = false;
2580 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2581 break;
2582 case ARM::VSELEQD:
2583 case ARM::VSELEQS:
2584 CC = ARMCC::EQ;
2585 break;
2586 case ARM::VSELGTD:
2587 case ARM::VSELGTS:
2588 CC = ARMCC::GT;
2589 break;
2590 case ARM::VSELGED:
2591 case ARM::VSELGES:
2592 CC = ARMCC::GE;
2593 break;
2594 case ARM::VSELVSS:
2595 case ARM::VSELVSD:
2596 CC = ARMCC::VS;
2597 break;
2598 }
2599
Manman Renb1b3db62012-06-29 22:06:19 +00002600 if (Sub) {
2601 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2602 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002603 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002604 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2605 // on CMP needs to be updated to be based on SUB.
2606 // Push the condition code operands to OperandsToUpdate.
2607 // If it is safe to remove CmpInstr, the condition code of these
2608 // operands will be modified.
2609 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002610 Sub->getOperand(2).getReg() == SrcReg) {
2611 // VSel doesn't support condition code update.
2612 if (IsInstrVSel)
2613 return false;
2614 OperandsToUpdate.push_back(
2615 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2616 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002617 } else {
2618 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002619 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002620 case ARMCC::EQ: // Z
2621 case ARMCC::NE: // Z
2622 case ARMCC::MI: // N
2623 case ARMCC::PL: // N
2624 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002625 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002626 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002627 case ARMCC::HS: // C
2628 case ARMCC::LO: // C
2629 case ARMCC::VS: // V
2630 case ARMCC::VC: // V
2631 case ARMCC::HI: // C Z
2632 case ARMCC::LS: // C Z
2633 case ARMCC::GE: // N V
2634 case ARMCC::LT: // N V
2635 case ARMCC::GT: // Z N V
2636 case ARMCC::LE: // Z N V
2637 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002638 return false;
2639 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002640 }
Evan Cheng425489d2011-03-23 22:52:04 +00002641 }
2642 }
2643
Manman Ren34cb93e2012-07-11 22:51:44 +00002644 // If CPSR is not killed nor re-defined, we should check whether it is
2645 // live-out. If it is live-out, do not optimize.
2646 if (!isSafe) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002647 MachineBasicBlock *MBB = CmpInstr.getParent();
Manman Ren34cb93e2012-07-11 22:51:44 +00002648 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2649 SE = MBB->succ_end(); SI != SE; ++SI)
2650 if ((*SI)->isLiveIn(ARM::CPSR))
2651 return false;
2652 }
Evan Cheng425489d2011-03-23 22:52:04 +00002653
James Molloy0f412272016-09-09 09:51:06 +00002654 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2655 // set CPSR so this is represented as an explicit output)
2656 if (!IsThumb1) {
2657 MI->getOperand(5).setReg(ARM::CPSR);
2658 MI->getOperand(5).setIsDef(true);
2659 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002660 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002661 CmpInstr.eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002662
2663 // Modify the condition code of operands in OperandsToUpdate.
2664 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2665 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002666 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2667 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002668 return true;
2669 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002670 }
James Molloy0f412272016-09-09 09:51:06 +00002671
Bill Wendling7de9d522010-08-06 01:32:48 +00002672 return false;
2673}
Evan Cheng367a5df2010-09-09 18:18:55 +00002674
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002675bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2676 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002677 MachineRegisterInfo *MRI) const {
2678 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002679 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002680 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2681 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002682 if (!DefMI.getOperand(1).isImm())
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002683 // Could be t2MOVi32imm <ga:xx>
2684 return false;
2685
2686 if (!MRI->hasOneNonDBGUse(Reg))
2687 return false;
2688
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002689 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002690 if (DefMCID.hasOptionalDef()) {
2691 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002692 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00002693 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2694 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2695 // to delete DefMI.
2696 return false;
2697 }
2698
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002699 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002700 if (UseMCID.hasOptionalDef()) {
2701 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002702 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00002703 // If the instruction sets the flag, do not attempt this optimization
2704 // since it may change the semantics of the code.
2705 return false;
2706 }
2707
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002708 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002709 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002710 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002711 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002712 bool Commute = false;
2713 switch (UseOpc) {
2714 default: return false;
2715 case ARM::SUBrr:
2716 case ARM::ADDrr:
2717 case ARM::ORRrr:
2718 case ARM::EORrr:
2719 case ARM::t2SUBrr:
2720 case ARM::t2ADDrr:
2721 case ARM::t2ORRrr:
2722 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002723 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002724 switch (UseOpc) {
2725 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002726 case ARM::ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002727 case ARM::SUBrr: {
2728 if (UseOpc == ARM::SUBrr && Commute)
2729 return false;
2730
2731 // ADD/SUB are special because they're essentially the same operation, so
2732 // we can handle a larger range of immediates.
2733 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
2734 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
2735 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
2736 ImmVal = -ImmVal;
2737 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
2738 } else
2739 return false;
2740 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2741 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2742 break;
2743 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002744 case ARM::ORRrr:
2745 case ARM::EORrr: {
2746 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2747 return false;
2748 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2749 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2750 switch (UseOpc) {
2751 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002752 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2753 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2754 }
2755 break;
2756 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002757 case ARM::t2ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002758 case ARM::t2SUBrr: {
2759 if (UseOpc == ARM::t2SUBrr && Commute)
2760 return false;
2761
2762 // ADD/SUB are special because they're essentially the same operation, so
2763 // we can handle a larger range of immediates.
2764 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2765 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
2766 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
2767 ImmVal = -ImmVal;
2768 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
2769 } else
2770 return false;
2771 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2772 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2773 break;
2774 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002775 case ARM::t2ORRrr:
2776 case ARM::t2EORrr: {
2777 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2778 return false;
2779 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2780 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2781 switch (UseOpc) {
2782 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002783 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2784 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2785 }
2786 break;
2787 }
2788 }
2789 }
2790 }
2791
2792 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002793 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
2794 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002795 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002796 AddDefaultCC(
2797 AddDefaultPred(BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
2798 get(NewUseOpc), NewReg)
2799 .addReg(Reg1, getKillRegState(isKill))
2800 .addImm(SOImmValV1)));
2801 UseMI.setDesc(get(NewUseOpc));
2802 UseMI.getOperand(1).setReg(NewReg);
2803 UseMI.getOperand(1).setIsKill();
2804 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
2805 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002806 return true;
2807}
2808
Bob Wilsone8a549c2012-09-29 21:43:49 +00002809static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002810 const MachineInstr &MI) {
2811 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00002812 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002813 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002814 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2815 assert(UOps >= 0 && "bad # UOps");
2816 return UOps;
2817 }
2818
2819 case ARM::LDRrs:
2820 case ARM::LDRBrs:
2821 case ARM::STRrs:
2822 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002823 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002824 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2825 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2826 if (!isSub &&
2827 (ShImm == 0 ||
2828 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2829 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2830 return 1;
2831 return 2;
2832 }
2833
2834 case ARM::LDRH:
2835 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002836 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00002837 return 1;
2838
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002839 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002840 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2841 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2842 if (!isSub &&
2843 (ShImm == 0 ||
2844 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2845 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2846 return 1;
2847 return 2;
2848 }
2849
2850 case ARM::LDRSB:
2851 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002852 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002853
2854 case ARM::LDRSB_POST:
2855 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002856 unsigned Rt = MI.getOperand(0).getReg();
2857 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002858 return (Rt == Rm) ? 4 : 3;
2859 }
2860
2861 case ARM::LDR_PRE_REG:
2862 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002863 unsigned Rt = MI.getOperand(0).getReg();
2864 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002865 if (Rt == Rm)
2866 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002867 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002868 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2869 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2870 if (!isSub &&
2871 (ShImm == 0 ||
2872 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2873 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2874 return 2;
2875 return 3;
2876 }
2877
2878 case ARM::STR_PRE_REG:
2879 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002880 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002881 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2882 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2883 if (!isSub &&
2884 (ShImm == 0 ||
2885 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2886 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2887 return 2;
2888 return 3;
2889 }
2890
2891 case ARM::LDRH_PRE:
2892 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002893 unsigned Rt = MI.getOperand(0).getReg();
2894 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002895 if (!Rm)
2896 return 2;
2897 if (Rt == Rm)
2898 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002899 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002900 }
2901
2902 case ARM::LDR_POST_REG:
2903 case ARM::LDRB_POST_REG:
2904 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002905 unsigned Rt = MI.getOperand(0).getReg();
2906 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002907 return (Rt == Rm) ? 3 : 2;
2908 }
2909
2910 case ARM::LDR_PRE_IMM:
2911 case ARM::LDRB_PRE_IMM:
2912 case ARM::LDR_POST_IMM:
2913 case ARM::LDRB_POST_IMM:
2914 case ARM::STRB_POST_IMM:
2915 case ARM::STRB_POST_REG:
2916 case ARM::STRB_PRE_IMM:
2917 case ARM::STRH_POST:
2918 case ARM::STR_POST_IMM:
2919 case ARM::STR_POST_REG:
2920 case ARM::STR_PRE_IMM:
2921 return 2;
2922
2923 case ARM::LDRSB_PRE:
2924 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002925 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002926 if (Rm == 0)
2927 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002928 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002929 if (Rt == Rm)
2930 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002931 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002932 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2933 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2934 if (!isSub &&
2935 (ShImm == 0 ||
2936 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2937 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2938 return 3;
2939 return 4;
2940 }
2941
2942 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002943 unsigned Rt = MI.getOperand(0).getReg();
2944 unsigned Rn = MI.getOperand(2).getReg();
2945 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002946 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002947 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
2948 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002949 return (Rt == Rn) ? 3 : 2;
2950 }
2951
2952 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002953 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002954 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002955 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
2956 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002957 return 2;
2958 }
2959
2960 case ARM::LDRD_POST:
2961 case ARM::t2LDRD_POST:
2962 return 3;
2963
2964 case ARM::STRD_POST:
2965 case ARM::t2STRD_POST:
2966 return 4;
2967
2968 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002969 unsigned Rt = MI.getOperand(0).getReg();
2970 unsigned Rn = MI.getOperand(3).getReg();
2971 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002972 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002973 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
2974 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002975 return (Rt == Rn) ? 4 : 3;
2976 }
2977
2978 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002979 unsigned Rt = MI.getOperand(0).getReg();
2980 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002981 return (Rt == Rn) ? 4 : 3;
2982 }
2983
2984 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002985 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002986 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002987 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
2988 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002989 return 3;
2990 }
2991
2992 case ARM::t2STRD_PRE:
2993 return 3;
2994
2995 case ARM::t2LDR_POST:
2996 case ARM::t2LDRB_POST:
2997 case ARM::t2LDRB_PRE:
2998 case ARM::t2LDRSBi12:
2999 case ARM::t2LDRSBi8:
3000 case ARM::t2LDRSBpci:
3001 case ARM::t2LDRSBs:
3002 case ARM::t2LDRH_POST:
3003 case ARM::t2LDRH_PRE:
3004 case ARM::t2LDRSBT:
3005 case ARM::t2LDRSB_POST:
3006 case ARM::t2LDRSB_PRE:
3007 case ARM::t2LDRSH_POST:
3008 case ARM::t2LDRSH_PRE:
3009 case ARM::t2LDRSHi12:
3010 case ARM::t2LDRSHi8:
3011 case ARM::t2LDRSHpci:
3012 case ARM::t2LDRSHs:
3013 return 2;
3014
3015 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003016 unsigned Rt = MI.getOperand(0).getReg();
3017 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003018 return (Rt == Rn) ? 3 : 2;
3019 }
3020
3021 case ARM::t2STRB_POST:
3022 case ARM::t2STRB_PRE:
3023 case ARM::t2STRBs:
3024 case ARM::t2STRDi8:
3025 case ARM::t2STRH_POST:
3026 case ARM::t2STRH_PRE:
3027 case ARM::t2STRHs:
3028 case ARM::t2STR_POST:
3029 case ARM::t2STR_PRE:
3030 case ARM::t2STRs:
3031 return 2;
3032 }
3033}
3034
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003035// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3036// can't be easily determined return 0 (missing MachineMemOperand).
3037//
3038// FIXME: The current MachineInstr design does not support relying on machine
3039// mem operands to determine the width of a memory access. Instead, we expect
3040// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003041// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003042// two reasons:
3043//
3044// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3045// operands. This is much more dangerous than using the MachineMemOperand
3046// sizes because CodeGen passes can insert/remove optional machine operands. In
3047// fact, it's totally incorrect for preRA passes and appears to be wrong for
3048// postRA passes as well.
3049//
3050// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3051// machine model that calls this should handle the unknown (zero size) case.
3052//
3053// Long term, we should require a target hook that verifies MachineMemOperand
3054// sizes during MC lowering. That target hook should be local to MC lowering
3055// because we can't ensure that it is aware of other MI forms. Doing this will
3056// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003057unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003058 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003059 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3060 E = MI.memoperands_end();
3061 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003062 Size += (*I)->getSize();
3063 }
3064 return Size / 4;
3065}
3066
Diana Picus92423ce2016-06-27 09:08:23 +00003067static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3068 unsigned NumRegs) {
3069 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3070 switch (Opc) {
3071 default:
3072 break;
3073 case ARM::VLDMDIA_UPD:
3074 case ARM::VLDMDDB_UPD:
3075 case ARM::VLDMSIA_UPD:
3076 case ARM::VLDMSDB_UPD:
3077 case ARM::VSTMDIA_UPD:
3078 case ARM::VSTMDDB_UPD:
3079 case ARM::VSTMSIA_UPD:
3080 case ARM::VSTMSDB_UPD:
3081 case ARM::LDMIA_UPD:
3082 case ARM::LDMDA_UPD:
3083 case ARM::LDMDB_UPD:
3084 case ARM::LDMIB_UPD:
3085 case ARM::STMIA_UPD:
3086 case ARM::STMDA_UPD:
3087 case ARM::STMDB_UPD:
3088 case ARM::STMIB_UPD:
3089 case ARM::tLDMIA_UPD:
3090 case ARM::tSTMIA_UPD:
3091 case ARM::t2LDMIA_UPD:
3092 case ARM::t2LDMDB_UPD:
3093 case ARM::t2STMIA_UPD:
3094 case ARM::t2STMDB_UPD:
3095 ++UOps; // One for base register writeback.
3096 break;
3097 case ARM::LDMIA_RET:
3098 case ARM::tPOP_RET:
3099 case ARM::t2LDMIA_RET:
3100 UOps += 2; // One for base reg wb, one for write to pc.
3101 break;
3102 }
3103 return UOps;
3104}
3105
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003106unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3107 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003108 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003109 return 1;
3110
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003111 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003112 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003113 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003114 if (ItinUOps >= 0) {
3115 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3116 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3117
Andrew Trickf161e392012-07-02 18:10:42 +00003118 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003119 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003120
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003121 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003122 switch (Opc) {
3123 default:
3124 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003125 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003126 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003127 return 2;
3128
3129 // The number of uOps for load / store multiple are determined by the number
3130 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003131 //
Evan Chengbf407072010-09-10 01:29:16 +00003132 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3133 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003134 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003135 //
Evan Chengbf407072010-09-10 01:29:16 +00003136 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003137 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3138 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3139 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003140 case ARM::VLDMDIA_UPD:
3141 case ARM::VLDMDDB_UPD:
3142 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003143 case ARM::VLDMSIA_UPD:
3144 case ARM::VLDMSDB_UPD:
3145 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003146 case ARM::VSTMDIA_UPD:
3147 case ARM::VSTMDDB_UPD:
3148 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003149 case ARM::VSTMSIA_UPD:
3150 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003151 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003152 return (NumRegs / 2) + (NumRegs % 2) + 1;
3153 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003154
3155 case ARM::LDMIA_RET:
3156 case ARM::LDMIA:
3157 case ARM::LDMDA:
3158 case ARM::LDMDB:
3159 case ARM::LDMIB:
3160 case ARM::LDMIA_UPD:
3161 case ARM::LDMDA_UPD:
3162 case ARM::LDMDB_UPD:
3163 case ARM::LDMIB_UPD:
3164 case ARM::STMIA:
3165 case ARM::STMDA:
3166 case ARM::STMDB:
3167 case ARM::STMIB:
3168 case ARM::STMIA_UPD:
3169 case ARM::STMDA_UPD:
3170 case ARM::STMDB_UPD:
3171 case ARM::STMIB_UPD:
3172 case ARM::tLDMIA:
3173 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003174 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003175 case ARM::tPOP_RET:
3176 case ARM::tPOP:
3177 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003178 case ARM::t2LDMIA_RET:
3179 case ARM::t2LDMIA:
3180 case ARM::t2LDMDB:
3181 case ARM::t2LDMIA_UPD:
3182 case ARM::t2LDMDB_UPD:
3183 case ARM::t2STMIA:
3184 case ARM::t2STMDB:
3185 case ARM::t2STMIA_UPD:
3186 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003187 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003188 switch (Subtarget.getLdStMultipleTiming()) {
3189 case ARMSubtarget::SingleIssuePlusExtras:
3190 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3191 case ARMSubtarget::SingleIssue:
3192 // Assume the worst.
3193 return NumRegs;
3194 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003195 if (NumRegs < 4)
3196 return 2;
3197 // 4 registers would be issued: 2, 2.
3198 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003199 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003200 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003201 ++UOps;
3202 return UOps;
3203 }
3204 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3205 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003206 // If there are odd number of registers or if it's not 64-bit aligned,
3207 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003208 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3209 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003210 ++UOps;
3211 return UOps;
3212 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003213 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003214 }
3215 }
Diana Picus92423ce2016-06-27 09:08:23 +00003216 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003217}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003218
3219int
Evan Cheng412e37b2010-10-07 23:12:15 +00003220ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003221 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003222 unsigned DefClass,
3223 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003224 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003225 if (RegNo <= 0)
3226 // Def is the address writeback.
3227 return ItinData->getOperandCycle(DefClass, DefIdx);
3228
3229 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003230 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003231 // (regno / 2) + (regno % 2) + 1
3232 DefCycle = RegNo / 2 + 1;
3233 if (RegNo % 2)
3234 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003235 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003236 DefCycle = RegNo;
3237 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003238
Evan Cheng6cc775f2011-06-28 19:10:37 +00003239 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003240 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003241 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003242 case ARM::VLDMSIA_UPD:
3243 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003244 isSLoad = true;
3245 break;
3246 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003247
Evan Cheng412e37b2010-10-07 23:12:15 +00003248 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3249 // then it takes an extra cycle.
3250 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3251 ++DefCycle;
3252 } else {
3253 // Assume the worst.
3254 DefCycle = RegNo + 2;
3255 }
3256
3257 return DefCycle;
3258}
3259
3260int
3261ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003262 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003263 unsigned DefClass,
3264 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003265 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003266 if (RegNo <= 0)
3267 // Def is the address writeback.
3268 return ItinData->getOperandCycle(DefClass, DefIdx);
3269
3270 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003271 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003272 // 4 registers would be issued: 1, 2, 1.
3273 // 5 registers would be issued: 1, 2, 2.
3274 DefCycle = RegNo / 2;
3275 if (DefCycle < 1)
3276 DefCycle = 1;
3277 // Result latency is issue cycle + 2: E2.
3278 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003279 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003280 DefCycle = (RegNo / 2);
3281 // If there are odd number of registers or if it's not 64-bit aligned,
3282 // then it takes an extra AGU (Address Generation Unit) cycle.
3283 if ((RegNo % 2) || DefAlign < 8)
3284 ++DefCycle;
3285 // Result latency is AGU cycles + 2.
3286 DefCycle += 2;
3287 } else {
3288 // Assume the worst.
3289 DefCycle = RegNo + 2;
3290 }
3291
3292 return DefCycle;
3293}
3294
3295int
3296ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003297 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003298 unsigned UseClass,
3299 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003300 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003301 if (RegNo <= 0)
3302 return ItinData->getOperandCycle(UseClass, UseIdx);
3303
3304 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003305 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003306 // (regno / 2) + (regno % 2) + 1
3307 UseCycle = RegNo / 2 + 1;
3308 if (RegNo % 2)
3309 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003310 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003311 UseCycle = RegNo;
3312 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003313
Evan Cheng6cc775f2011-06-28 19:10:37 +00003314 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003315 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003316 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003317 case ARM::VSTMSIA_UPD:
3318 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003319 isSStore = true;
3320 break;
3321 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003322
Evan Cheng412e37b2010-10-07 23:12:15 +00003323 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3324 // then it takes an extra cycle.
3325 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3326 ++UseCycle;
3327 } else {
3328 // Assume the worst.
3329 UseCycle = RegNo + 2;
3330 }
3331
3332 return UseCycle;
3333}
3334
3335int
3336ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003337 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003338 unsigned UseClass,
3339 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003340 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003341 if (RegNo <= 0)
3342 return ItinData->getOperandCycle(UseClass, UseIdx);
3343
3344 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003345 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003346 UseCycle = RegNo / 2;
3347 if (UseCycle < 2)
3348 UseCycle = 2;
3349 // Read in E3.
3350 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003351 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003352 UseCycle = (RegNo / 2);
3353 // If there are odd number of registers or if it's not 64-bit aligned,
3354 // then it takes an extra AGU (Address Generation Unit) cycle.
3355 if ((RegNo % 2) || UseAlign < 8)
3356 ++UseCycle;
3357 } else {
3358 // Assume the worst.
3359 UseCycle = 1;
3360 }
3361 return UseCycle;
3362}
3363
3364int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003365ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003366 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003367 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003368 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003369 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003370 unsigned DefClass = DefMCID.getSchedClass();
3371 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003372
Evan Cheng6cc775f2011-06-28 19:10:37 +00003373 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003374 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3375
3376 // This may be a def / use of a variable_ops instruction, the operand
3377 // latency might be determinable dynamically. Let the target try to
3378 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003379 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003380 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003381 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003382 default:
3383 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3384 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003385
3386 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003387 case ARM::VLDMDIA_UPD:
3388 case ARM::VLDMDDB_UPD:
3389 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003390 case ARM::VLDMSIA_UPD:
3391 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003392 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003393 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003394
3395 case ARM::LDMIA_RET:
3396 case ARM::LDMIA:
3397 case ARM::LDMDA:
3398 case ARM::LDMDB:
3399 case ARM::LDMIB:
3400 case ARM::LDMIA_UPD:
3401 case ARM::LDMDA_UPD:
3402 case ARM::LDMDB_UPD:
3403 case ARM::LDMIB_UPD:
3404 case ARM::tLDMIA:
3405 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003406 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003407 case ARM::t2LDMIA_RET:
3408 case ARM::t2LDMIA:
3409 case ARM::t2LDMDB:
3410 case ARM::t2LDMIA_UPD:
3411 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003412 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003413 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003414 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003415 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003416
3417 if (DefCycle == -1)
3418 // We can't seem to determine the result latency of the def, assume it's 2.
3419 DefCycle = 2;
3420
3421 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003422 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003423 default:
3424 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3425 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003426
3427 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003428 case ARM::VSTMDIA_UPD:
3429 case ARM::VSTMDDB_UPD:
3430 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003431 case ARM::VSTMSIA_UPD:
3432 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003433 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003434 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003435
3436 case ARM::STMIA:
3437 case ARM::STMDA:
3438 case ARM::STMDB:
3439 case ARM::STMIB:
3440 case ARM::STMIA_UPD:
3441 case ARM::STMDA_UPD:
3442 case ARM::STMDB_UPD:
3443 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003444 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003445 case ARM::tPOP_RET:
3446 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003447 case ARM::t2STMIA:
3448 case ARM::t2STMDB:
3449 case ARM::t2STMIA_UPD:
3450 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003451 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003452 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003453 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003454
3455 if (UseCycle == -1)
3456 // Assume it's read in the first stage.
3457 UseCycle = 1;
3458
3459 UseCycle = DefCycle - UseCycle + 1;
3460 if (UseCycle > 0) {
3461 if (LdmBypass) {
3462 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3463 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003464 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003465 UseClass, UseIdx))
3466 --UseCycle;
3467 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003468 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003469 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003470 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003471 }
3472
3473 return UseCycle;
3474}
3475
Evan Cheng7fae11b2011-12-14 02:11:42 +00003476static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003477 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003478 unsigned &DefIdx, unsigned &Dist) {
3479 Dist = 0;
3480
3481 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003482 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003483 assert(II->isInsideBundle() && "Empty bundle?");
3484
3485 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003486 while (II->isInsideBundle()) {
3487 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3488 if (Idx != -1)
3489 break;
3490 --II;
3491 ++Dist;
3492 }
3493
3494 assert(Idx != -1 && "Cannot find bundled definition!");
3495 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003496 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003497}
3498
3499static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003500 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003501 unsigned &UseIdx, unsigned &Dist) {
3502 Dist = 0;
3503
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003504 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003505 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003506 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003507
3508 // FIXME: This doesn't properly handle multiple uses.
3509 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003510 while (II != E && II->isInsideBundle()) {
3511 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3512 if (Idx != -1)
3513 break;
3514 if (II->getOpcode() != ARM::t2IT)
3515 ++Dist;
3516 ++II;
3517 }
3518
Evan Chengda103bf2011-12-14 20:00:08 +00003519 if (Idx == -1) {
3520 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003521 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003522 }
3523
Evan Cheng7fae11b2011-12-14 02:11:42 +00003524 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003525 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003526}
3527
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003528/// Return the number of cycles to add to (or subtract from) the static
3529/// itinerary based on the def opcode and alignment. The caller will ensure that
3530/// adjusted latency is at least one cycle.
3531static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003532 const MachineInstr &DefMI,
3533 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003534 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003535 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003536 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3537 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003538 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003539 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003540 case ARM::LDRrs:
3541 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003542 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003543 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3544 if (ShImm == 0 ||
3545 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003546 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003547 break;
3548 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003549 case ARM::t2LDRs:
3550 case ARM::t2LDRBs:
3551 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003552 case ARM::t2LDRSHs: {
3553 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003554 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003555 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003556 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003557 break;
3558 }
3559 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003560 } else if (Subtarget.isSwift()) {
3561 // FIXME: Properly handle all of the latency adjustments for address
3562 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003563 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003564 default: break;
3565 case ARM::LDRrs:
3566 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003567 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003568 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3569 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3570 if (!isSub &&
3571 (ShImm == 0 ||
3572 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3573 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3574 Adjust -= 2;
3575 else if (!isSub &&
3576 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3577 --Adjust;
3578 break;
3579 }
3580 case ARM::t2LDRs:
3581 case ARM::t2LDRBs:
3582 case ARM::t2LDRHs:
3583 case ARM::t2LDRSHs: {
3584 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003585 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003586 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3587 Adjust -= 2;
3588 break;
3589 }
3590 }
Evan Chengff310732010-10-28 06:47:08 +00003591 }
3592
Diana Picus92423ce2016-06-27 09:08:23 +00003593 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003594 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003595 default: break;
3596 case ARM::VLD1q8:
3597 case ARM::VLD1q16:
3598 case ARM::VLD1q32:
3599 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003600 case ARM::VLD1q8wb_fixed:
3601 case ARM::VLD1q16wb_fixed:
3602 case ARM::VLD1q32wb_fixed:
3603 case ARM::VLD1q64wb_fixed:
3604 case ARM::VLD1q8wb_register:
3605 case ARM::VLD1q16wb_register:
3606 case ARM::VLD1q32wb_register:
3607 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003608 case ARM::VLD2d8:
3609 case ARM::VLD2d16:
3610 case ARM::VLD2d32:
3611 case ARM::VLD2q8:
3612 case ARM::VLD2q16:
3613 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003614 case ARM::VLD2d8wb_fixed:
3615 case ARM::VLD2d16wb_fixed:
3616 case ARM::VLD2d32wb_fixed:
3617 case ARM::VLD2q8wb_fixed:
3618 case ARM::VLD2q16wb_fixed:
3619 case ARM::VLD2q32wb_fixed:
3620 case ARM::VLD2d8wb_register:
3621 case ARM::VLD2d16wb_register:
3622 case ARM::VLD2d32wb_register:
3623 case ARM::VLD2q8wb_register:
3624 case ARM::VLD2q16wb_register:
3625 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003626 case ARM::VLD3d8:
3627 case ARM::VLD3d16:
3628 case ARM::VLD3d32:
3629 case ARM::VLD1d64T:
3630 case ARM::VLD3d8_UPD:
3631 case ARM::VLD3d16_UPD:
3632 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003633 case ARM::VLD1d64Twb_fixed:
3634 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003635 case ARM::VLD3q8_UPD:
3636 case ARM::VLD3q16_UPD:
3637 case ARM::VLD3q32_UPD:
3638 case ARM::VLD4d8:
3639 case ARM::VLD4d16:
3640 case ARM::VLD4d32:
3641 case ARM::VLD1d64Q:
3642 case ARM::VLD4d8_UPD:
3643 case ARM::VLD4d16_UPD:
3644 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003645 case ARM::VLD1d64Qwb_fixed:
3646 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003647 case ARM::VLD4q8_UPD:
3648 case ARM::VLD4q16_UPD:
3649 case ARM::VLD4q32_UPD:
3650 case ARM::VLD1DUPq8:
3651 case ARM::VLD1DUPq16:
3652 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003653 case ARM::VLD1DUPq8wb_fixed:
3654 case ARM::VLD1DUPq16wb_fixed:
3655 case ARM::VLD1DUPq32wb_fixed:
3656 case ARM::VLD1DUPq8wb_register:
3657 case ARM::VLD1DUPq16wb_register:
3658 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003659 case ARM::VLD2DUPd8:
3660 case ARM::VLD2DUPd16:
3661 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003662 case ARM::VLD2DUPd8wb_fixed:
3663 case ARM::VLD2DUPd16wb_fixed:
3664 case ARM::VLD2DUPd32wb_fixed:
3665 case ARM::VLD2DUPd8wb_register:
3666 case ARM::VLD2DUPd16wb_register:
3667 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003668 case ARM::VLD4DUPd8:
3669 case ARM::VLD4DUPd16:
3670 case ARM::VLD4DUPd32:
3671 case ARM::VLD4DUPd8_UPD:
3672 case ARM::VLD4DUPd16_UPD:
3673 case ARM::VLD4DUPd32_UPD:
3674 case ARM::VLD1LNd8:
3675 case ARM::VLD1LNd16:
3676 case ARM::VLD1LNd32:
3677 case ARM::VLD1LNd8_UPD:
3678 case ARM::VLD1LNd16_UPD:
3679 case ARM::VLD1LNd32_UPD:
3680 case ARM::VLD2LNd8:
3681 case ARM::VLD2LNd16:
3682 case ARM::VLD2LNd32:
3683 case ARM::VLD2LNq16:
3684 case ARM::VLD2LNq32:
3685 case ARM::VLD2LNd8_UPD:
3686 case ARM::VLD2LNd16_UPD:
3687 case ARM::VLD2LNd32_UPD:
3688 case ARM::VLD2LNq16_UPD:
3689 case ARM::VLD2LNq32_UPD:
3690 case ARM::VLD4LNd8:
3691 case ARM::VLD4LNd16:
3692 case ARM::VLD4LNd32:
3693 case ARM::VLD4LNq16:
3694 case ARM::VLD4LNq32:
3695 case ARM::VLD4LNd8_UPD:
3696 case ARM::VLD4LNd16_UPD:
3697 case ARM::VLD4LNd32_UPD:
3698 case ARM::VLD4LNq16_UPD:
3699 case ARM::VLD4LNq32_UPD:
3700 // If the address is not 64-bit aligned, the latencies of these
3701 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003702 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003703 break;
3704 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003705 }
3706 return Adjust;
3707}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003708
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003709int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3710 const MachineInstr &DefMI,
3711 unsigned DefIdx,
3712 const MachineInstr &UseMI,
3713 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003714 // No operand latency. The caller may fall back to getInstrLatency.
3715 if (!ItinData || ItinData->isEmpty())
3716 return -1;
3717
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003718 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003719 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003720
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003721 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003722 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003723 if (DefMI.isBundle())
3724 ResolvedDefMI =
3725 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
3726 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
3727 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003728 return 1;
3729 }
3730
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003731 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003732 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003733 if (UseMI.isBundle()) {
3734 ResolvedUseMI =
3735 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
3736 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00003737 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003738 }
3739
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003740 return getOperandLatencyImpl(
3741 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
3742 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
3743}
3744
3745int ARMBaseInstrInfo::getOperandLatencyImpl(
3746 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
3747 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
3748 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
3749 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003750 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003751 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003752 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003753 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003754 }
3755
3756 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003757 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003758 return 0;
3759
3760 // Otherwise it takes the instruction latency (generally one).
3761 unsigned Latency = getInstrLatency(ItinData, DefMI);
3762
3763 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3764 // its uses. Instructions which are otherwise scheduled between them may
3765 // incur a code size penalty (not able to use the CPSR setting 16-bit
3766 // instructions).
3767 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003768 const MachineFunction *MF = DefMI.getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003769 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003770 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003771 --Latency;
3772 }
3773 return Latency;
3774 }
3775
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003776 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00003777 return -1;
3778
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003779 unsigned DefAlign = DefMI.hasOneMemOperand()
3780 ? (*DefMI.memoperands_begin())->getAlignment()
3781 : 0;
3782 unsigned UseAlign = UseMI.hasOneMemOperand()
3783 ? (*UseMI.memoperands_begin())->getAlignment()
3784 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003785
3786 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003787 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
3788 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003789 // Unable to find operand latency. The caller may resort to getInstrLatency.
3790 if (Latency < 0)
3791 return Latency;
3792
3793 // Adjust for IT block position.
3794 int Adj = DefAdj + UseAdj;
3795
3796 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3797 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3798 if (Adj >= 0 || (int)Latency > -Adj) {
3799 return Latency + Adj;
3800 }
3801 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003802 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003803}
3804
3805int
3806ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3807 SDNode *DefNode, unsigned DefIdx,
3808 SDNode *UseNode, unsigned UseIdx) const {
3809 if (!DefNode->isMachineOpcode())
3810 return 1;
3811
Evan Cheng6cc775f2011-06-28 19:10:37 +00003812 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003813
Evan Cheng6cc775f2011-06-28 19:10:37 +00003814 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003815 return 0;
3816
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003817 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003818 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003819
Evan Cheng6c1414f2010-10-29 18:09:28 +00003820 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003821 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00003822 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
3823 int Threshold = 1 + Adj;
3824 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00003825 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003826
Evan Cheng6cc775f2011-06-28 19:10:37 +00003827 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003828 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3829 unsigned DefAlign = !DefMN->memoperands_empty()
3830 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3831 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3832 unsigned UseAlign = !UseMN->memoperands_empty()
3833 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003834 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3835 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003836
3837 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003838 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3839 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003840 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3841 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003842 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003843 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003844 case ARM::LDRrs:
3845 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003846 unsigned ShOpVal =
3847 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3848 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3849 if (ShImm == 0 ||
3850 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3851 --Latency;
3852 break;
3853 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003854 case ARM::t2LDRs:
3855 case ARM::t2LDRBs:
3856 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003857 case ARM::t2LDRSHs: {
3858 // Thumb2 mode: lsl only.
3859 unsigned ShAmt =
3860 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3861 if (ShAmt == 0 || ShAmt == 2)
3862 --Latency;
3863 break;
3864 }
3865 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003866 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3867 // FIXME: Properly handle all of the latency adjustments for address
3868 // writeback.
3869 switch (DefMCID.getOpcode()) {
3870 default: break;
3871 case ARM::LDRrs:
3872 case ARM::LDRBrs: {
3873 unsigned ShOpVal =
3874 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3875 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3876 if (ShImm == 0 ||
3877 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3878 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3879 Latency -= 2;
3880 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3881 --Latency;
3882 break;
3883 }
3884 case ARM::t2LDRs:
3885 case ARM::t2LDRBs:
3886 case ARM::t2LDRHs:
3887 case ARM::t2LDRSHs: {
3888 // Thumb2 mode: lsl 0-3 only.
3889 Latency -= 2;
3890 break;
3891 }
3892 }
Evan Chengff310732010-10-28 06:47:08 +00003893 }
3894
Diana Picus92423ce2016-06-27 09:08:23 +00003895 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003896 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003897 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003898 case ARM::VLD1q8:
3899 case ARM::VLD1q16:
3900 case ARM::VLD1q32:
3901 case ARM::VLD1q64:
3902 case ARM::VLD1q8wb_register:
3903 case ARM::VLD1q16wb_register:
3904 case ARM::VLD1q32wb_register:
3905 case ARM::VLD1q64wb_register:
3906 case ARM::VLD1q8wb_fixed:
3907 case ARM::VLD1q16wb_fixed:
3908 case ARM::VLD1q32wb_fixed:
3909 case ARM::VLD1q64wb_fixed:
3910 case ARM::VLD2d8:
3911 case ARM::VLD2d16:
3912 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003913 case ARM::VLD2q8Pseudo:
3914 case ARM::VLD2q16Pseudo:
3915 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003916 case ARM::VLD2d8wb_fixed:
3917 case ARM::VLD2d16wb_fixed:
3918 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003919 case ARM::VLD2q8PseudoWB_fixed:
3920 case ARM::VLD2q16PseudoWB_fixed:
3921 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003922 case ARM::VLD2d8wb_register:
3923 case ARM::VLD2d16wb_register:
3924 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003925 case ARM::VLD2q8PseudoWB_register:
3926 case ARM::VLD2q16PseudoWB_register:
3927 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003928 case ARM::VLD3d8Pseudo:
3929 case ARM::VLD3d16Pseudo:
3930 case ARM::VLD3d32Pseudo:
3931 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003932 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003933 case ARM::VLD3d8Pseudo_UPD:
3934 case ARM::VLD3d16Pseudo_UPD:
3935 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003936 case ARM::VLD3q8Pseudo_UPD:
3937 case ARM::VLD3q16Pseudo_UPD:
3938 case ARM::VLD3q32Pseudo_UPD:
3939 case ARM::VLD3q8oddPseudo:
3940 case ARM::VLD3q16oddPseudo:
3941 case ARM::VLD3q32oddPseudo:
3942 case ARM::VLD3q8oddPseudo_UPD:
3943 case ARM::VLD3q16oddPseudo_UPD:
3944 case ARM::VLD3q32oddPseudo_UPD:
3945 case ARM::VLD4d8Pseudo:
3946 case ARM::VLD4d16Pseudo:
3947 case ARM::VLD4d32Pseudo:
3948 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003949 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003950 case ARM::VLD4d8Pseudo_UPD:
3951 case ARM::VLD4d16Pseudo_UPD:
3952 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003953 case ARM::VLD4q8Pseudo_UPD:
3954 case ARM::VLD4q16Pseudo_UPD:
3955 case ARM::VLD4q32Pseudo_UPD:
3956 case ARM::VLD4q8oddPseudo:
3957 case ARM::VLD4q16oddPseudo:
3958 case ARM::VLD4q32oddPseudo:
3959 case ARM::VLD4q8oddPseudo_UPD:
3960 case ARM::VLD4q16oddPseudo_UPD:
3961 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003962 case ARM::VLD1DUPq8:
3963 case ARM::VLD1DUPq16:
3964 case ARM::VLD1DUPq32:
3965 case ARM::VLD1DUPq8wb_fixed:
3966 case ARM::VLD1DUPq16wb_fixed:
3967 case ARM::VLD1DUPq32wb_fixed:
3968 case ARM::VLD1DUPq8wb_register:
3969 case ARM::VLD1DUPq16wb_register:
3970 case ARM::VLD1DUPq32wb_register:
3971 case ARM::VLD2DUPd8:
3972 case ARM::VLD2DUPd16:
3973 case ARM::VLD2DUPd32:
3974 case ARM::VLD2DUPd8wb_fixed:
3975 case ARM::VLD2DUPd16wb_fixed:
3976 case ARM::VLD2DUPd32wb_fixed:
3977 case ARM::VLD2DUPd8wb_register:
3978 case ARM::VLD2DUPd16wb_register:
3979 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003980 case ARM::VLD4DUPd8Pseudo:
3981 case ARM::VLD4DUPd16Pseudo:
3982 case ARM::VLD4DUPd32Pseudo:
3983 case ARM::VLD4DUPd8Pseudo_UPD:
3984 case ARM::VLD4DUPd16Pseudo_UPD:
3985 case ARM::VLD4DUPd32Pseudo_UPD:
3986 case ARM::VLD1LNq8Pseudo:
3987 case ARM::VLD1LNq16Pseudo:
3988 case ARM::VLD1LNq32Pseudo:
3989 case ARM::VLD1LNq8Pseudo_UPD:
3990 case ARM::VLD1LNq16Pseudo_UPD:
3991 case ARM::VLD1LNq32Pseudo_UPD:
3992 case ARM::VLD2LNd8Pseudo:
3993 case ARM::VLD2LNd16Pseudo:
3994 case ARM::VLD2LNd32Pseudo:
3995 case ARM::VLD2LNq16Pseudo:
3996 case ARM::VLD2LNq32Pseudo:
3997 case ARM::VLD2LNd8Pseudo_UPD:
3998 case ARM::VLD2LNd16Pseudo_UPD:
3999 case ARM::VLD2LNd32Pseudo_UPD:
4000 case ARM::VLD2LNq16Pseudo_UPD:
4001 case ARM::VLD2LNq32Pseudo_UPD:
4002 case ARM::VLD4LNd8Pseudo:
4003 case ARM::VLD4LNd16Pseudo:
4004 case ARM::VLD4LNd32Pseudo:
4005 case ARM::VLD4LNq16Pseudo:
4006 case ARM::VLD4LNq32Pseudo:
4007 case ARM::VLD4LNd8Pseudo_UPD:
4008 case ARM::VLD4LNd16Pseudo_UPD:
4009 case ARM::VLD4LNd32Pseudo_UPD:
4010 case ARM::VLD4LNq16Pseudo_UPD:
4011 case ARM::VLD4LNq32Pseudo_UPD:
4012 // If the address is not 64-bit aligned, the latencies of these
4013 // instructions increases by one.
4014 ++Latency;
4015 break;
4016 }
4017
Evan Chengff310732010-10-28 06:47:08 +00004018 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004019}
Evan Cheng63c76082010-10-19 18:58:51 +00004020
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004021unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4022 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4023 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004024 return 0;
4025
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004026 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004027 return 0;
4028
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004029 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004030
4031 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
4032 // When predicated, CPSR is an additional source operand for CPSR updating
4033 // instructions, this apparently increases their latencies.
4034 return 1;
4035 }
4036 return 0;
4037}
4038
Andrew Trick45446062012-06-05 21:11:27 +00004039unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004040 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004041 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004042 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4043 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004044 return 1;
4045
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004046 // An instruction scheduler typically runs on unbundled instructions, however
4047 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004048 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004049 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004050 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4051 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004052 while (++I != E && I->isInsideBundle()) {
4053 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004054 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004055 }
4056 return Latency;
4057 }
4058
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004059 const MCInstrDesc &MCID = MI.getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004060 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004061 // When predicated, CPSR is an additional source operand for CPSR updating
4062 // instructions, this apparently increases their latencies.
4063 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004064 }
4065 // Be sure to call getStageLatency for an empty itinerary in case it has a
4066 // valid MinLatency property.
4067 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004068 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004069
4070 unsigned Class = MCID.getSchedClass();
4071
4072 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004073 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004074 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004075
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004076 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004077 unsigned Latency = ItinData->getStageLatency(Class);
4078
4079 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004080 unsigned DefAlign =
4081 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4082 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004083 if (Adj >= 0 || (int)Latency > -Adj) {
4084 return Latency + Adj;
4085 }
4086 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004087}
4088
4089int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4090 SDNode *Node) const {
4091 if (!Node->isMachineOpcode())
4092 return 1;
4093
4094 if (!ItinData || ItinData->isEmpty())
4095 return 1;
4096
4097 unsigned Opcode = Node->getMachineOpcode();
4098 switch (Opcode) {
4099 default:
4100 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004101 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004102 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004103 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004104 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004105}
4106
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004107bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4108 const MachineRegisterInfo *MRI,
4109 const MachineInstr &DefMI,
4110 unsigned DefIdx,
4111 const MachineInstr &UseMI,
4112 unsigned UseIdx) const {
4113 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4114 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004115 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004116 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004117 return true;
4118
4119 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004120 unsigned Latency =
4121 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004122 if (Latency <= 3)
4123 return false;
4124 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4125 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4126}
Evan Chenge96b8d72010-10-26 02:08:50 +00004127
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004128bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4129 const MachineInstr &DefMI,
4130 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004131 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004132 if (!ItinData || ItinData->isEmpty())
4133 return false;
4134
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004135 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004136 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004137 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004138 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4139 return (DefCycle != -1 && DefCycle <= 2);
4140 }
4141 return false;
4142}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004143
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004144bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004145 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004146 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004147 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4148 return false;
4149 }
4150 return true;
4151}
4152
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004153// LoadStackGuard has so far only been implemented for MachO. Different code
4154// sequence is needed for other targets.
4155void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4156 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004157 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004158 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4159 "ROPI/RWPI not currently supported with stack guard");
4160
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004161 MachineBasicBlock &MBB = *MI->getParent();
4162 DebugLoc DL = MI->getDebugLoc();
4163 unsigned Reg = MI->getOperand(0).getReg();
4164 const GlobalValue *GV =
4165 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4166 MachineInstrBuilder MIB;
4167
4168 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4169 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4170
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004171 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004172 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4173 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004174 auto Flags = MachineMemOperand::MOLoad |
4175 MachineMemOperand::MODereferenceable |
4176 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004177 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004178 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004179 MIB.addMemOperand(MMO);
4180 AddDefaultPred(MIB);
4181 }
4182
4183 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4184 MIB.addReg(Reg, RegState::Kill).addImm(0);
4185 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4186 AddDefaultPred(MIB);
4187}
4188
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004189bool
4190ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4191 unsigned &AddSubOpc,
4192 bool &NegAcc, bool &HasLane) const {
4193 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4194 if (I == MLxEntryMap.end())
4195 return false;
4196
4197 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4198 MulOpc = Entry.MulOpc;
4199 AddSubOpc = Entry.AddSubOpc;
4200 NegAcc = Entry.NegAcc;
4201 HasLane = Entry.HasLane;
4202 return true;
4203}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004204
4205//===----------------------------------------------------------------------===//
4206// Execution domains.
4207//===----------------------------------------------------------------------===//
4208//
4209// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4210// and some can go down both. The vmov instructions go down the VFP pipeline,
4211// but they can be changed to vorr equivalents that are executed by the NEON
4212// pipeline.
4213//
4214// We use the following execution domain numbering:
4215//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004216enum ARMExeDomain {
4217 ExeGeneric = 0,
4218 ExeVFP = 1,
4219 ExeNEON = 2
4220};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004221//
4222// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4223//
4224std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004225ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004226 // If we don't have access to NEON instructions then we won't be able
4227 // to swizzle anything to the NEON domain. Check to make sure.
4228 if (Subtarget.hasNEON()) {
4229 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4230 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004231 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004232 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004233
Eric Christopher7e70aba2015-03-07 00:12:22 +00004234 // CortexA9 is particularly picky about mixing the two and wants these
4235 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004236 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4237 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4238 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004239 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4240 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004241 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004242 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004243
4244 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004245 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004246
4247 // Certain instructions can go either way on Cortex-A8.
4248 // Treat them as NEON instructions.
4249 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004250 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004251
4252 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004253 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004254
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004255 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004256}
4257
Tim Northover771f1602012-08-29 16:36:07 +00004258static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4259 unsigned SReg, unsigned &Lane) {
4260 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4261 Lane = 0;
4262
4263 if (DReg != ARM::NoRegister)
4264 return DReg;
4265
4266 Lane = 1;
4267 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4268
4269 assert(DReg && "S-register with no D super-register?");
4270 return DReg;
4271}
4272
Andrew Trickd9296ec2012-10-10 05:43:01 +00004273/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004274/// set ImplicitSReg to a register number that must be marked as implicit-use or
4275/// zero if no register needs to be defined as implicit-use.
4276///
4277/// If the function cannot determine if an SPR should be marked implicit use or
4278/// not, it returns false.
4279///
4280/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004281/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004282/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4283/// lane of the DPR).
4284///
4285/// If the other SPR is defined, an implicit-use of it should be added. Else,
4286/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004287///
James Molloyea052562012-09-18 08:31:15 +00004288static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004289 MachineInstr &MI, unsigned DReg,
4290 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004291 // If the DPR is defined or used already, the other SPR lane will be chained
4292 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004293 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004294 ImplicitSReg = 0;
4295 return true;
4296 }
4297
4298 // Otherwise we need to go searching to see if the SPR is set explicitly.
4299 ImplicitSReg = TRI->getSubReg(DReg,
4300 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4301 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004302 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004303
4304 if (LQR == MachineBasicBlock::LQR_Live)
4305 return true;
4306 else if (LQR == MachineBasicBlock::LQR_Unknown)
4307 return false;
4308
4309 // If the register is known not to be live, there is no need to add an
4310 // implicit-use.
4311 ImplicitSReg = 0;
4312 return true;
4313}
Tim Northover771f1602012-08-29 16:36:07 +00004314
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004315void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4316 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004317 unsigned DstReg, SrcReg, DReg;
4318 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004319 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004320 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004321 switch (MI.getOpcode()) {
4322 default:
4323 llvm_unreachable("cannot handle opcode!");
4324 break;
4325 case ARM::VMOVD:
4326 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004327 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004328
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004329 // Zap the predicate operands.
4330 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004331
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004332 // Make sure we've got NEON instructions.
4333 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004334
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004335 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4336 DstReg = MI.getOperand(0).getReg();
4337 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004338
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004339 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4340 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004341
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004342 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4343 MI.setDesc(get(ARM::VORRd));
4344 AddDefaultPred(
4345 MIB.addReg(DstReg, RegState::Define).addReg(SrcReg).addReg(SrcReg));
4346 break;
4347 case ARM::VMOVRS:
4348 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004349 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004350 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004351
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004352 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4353 DstReg = MI.getOperand(0).getReg();
4354 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004355
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004356 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4357 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004358
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004359 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004360
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004361 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4362 // Note that DSrc has been widened and the other lane may be undef, which
4363 // contaminates the entire register.
4364 MI.setDesc(get(ARM::VGETLNi32));
4365 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4366 .addReg(DReg, RegState::Undef)
4367 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004368
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004369 // The old source should be an implicit use, otherwise we might think it
4370 // was dead before here.
4371 MIB.addReg(SrcReg, RegState::Implicit);
4372 break;
4373 case ARM::VMOVSR: {
4374 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004375 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004376 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004377
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004378 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4379 DstReg = MI.getOperand(0).getReg();
4380 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004381
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004382 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004383
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004384 unsigned ImplicitSReg;
4385 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004386 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004387
4388 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4389 MI.RemoveOperand(i - 1);
4390
4391 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4392 // Again DDst may be undefined at the beginning of this instruction.
4393 MI.setDesc(get(ARM::VSETLNi32));
4394 MIB.addReg(DReg, RegState::Define)
4395 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4396 .addReg(SrcReg)
4397 .addImm(Lane);
4398 AddDefaultPred(MIB);
4399
4400 // The narrower destination must be marked as set to keep previous chains
4401 // in place.
4402 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4403 if (ImplicitSReg != 0)
4404 MIB.addReg(ImplicitSReg, RegState::Implicit);
4405 break;
James Molloyea052562012-09-18 08:31:15 +00004406 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004407 case ARM::VMOVS: {
4408 if (Domain != ExeNEON)
4409 break;
4410
4411 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004412 DstReg = MI.getOperand(0).getReg();
4413 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004414
Tim Northoverca9f3842012-08-30 10:17:45 +00004415 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4416 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4417 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4418
James Molloyea052562012-09-18 08:31:15 +00004419 unsigned ImplicitSReg;
4420 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4421 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004422
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004423 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4424 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004425
Tim Northoverca9f3842012-08-30 10:17:45 +00004426 if (DSrc == DDst) {
4427 // Destination can be:
4428 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004429 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004430 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004431 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
4432 .addImm(SrcLane);
Tim Northover726d32c2012-09-01 18:07:29 +00004433 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004434
4435 // Neither the source or the destination are naturally represented any
4436 // more, so add them in manually.
4437 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4438 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004439 if (ImplicitSReg != 0)
4440 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004441 break;
4442 }
4443
4444 // In general there's no single instruction that can perform an S <-> S
4445 // move in NEON space, but a pair of VEXT instructions *can* do the
4446 // job. It turns out that the VEXTs needed will only use DSrc once, with
4447 // the position based purely on the combination of lane-0 and lane-1
4448 // involved. For example
4449 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4450 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4451 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4452 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4453 //
4454 // Pattern of the MachineInstrs is:
4455 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4456 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004457 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4458 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004459
4460 // On the first instruction, both DSrc and DDst may be <undef> if present.
4461 // Specifically when the original instruction didn't have them as an
4462 // <imp-use>.
4463 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004464 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004465 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4466
4467 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004468 CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004469 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4470
Tim Northoverca9f3842012-08-30 10:17:45 +00004471 NewMIB.addImm(1);
4472 AddDefaultPred(NewMIB);
4473
4474 if (SrcLane == DstLane)
4475 NewMIB.addReg(SrcReg, RegState::Implicit);
4476
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004477 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004478 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004479
4480 // On the second instruction, DDst has definitely been defined above, so
4481 // it is not <undef>. DSrc, if present, can be <undef> as above.
4482 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004483 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004484 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4485
4486 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004487 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004488 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4489
Tim Northoverca9f3842012-08-30 10:17:45 +00004490 MIB.addImm(1);
4491 AddDefaultPred(MIB);
4492
4493 if (SrcLane != DstLane)
4494 MIB.addReg(SrcReg, RegState::Implicit);
4495
4496 // As before, the original destination is no longer represented, add it
4497 // implicitly.
4498 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004499 if (ImplicitSReg != 0)
4500 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004501 break;
4502 }
Tim Northoverf6618152012-08-17 11:32:52 +00004503 }
4504
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004505}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004506
Bob Wilsone8a549c2012-09-29 21:43:49 +00004507//===----------------------------------------------------------------------===//
4508// Partial register updates
4509//===----------------------------------------------------------------------===//
4510//
4511// Swift renames NEON registers with 64-bit granularity. That means any
4512// instruction writing an S-reg implicitly reads the containing D-reg. The
4513// problem is mostly avoided by translating f32 operations to v2f32 operations
4514// on D-registers, but f32 loads are still a problem.
4515//
4516// These instructions can load an f32 into a NEON register:
4517//
4518// VLDRS - Only writes S, partial D update.
4519// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4520// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4521//
4522// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004523unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4524 const MachineInstr &MI, unsigned OpNum,
4525 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004526 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4527 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004528 return 0;
4529
4530 assert(TRI && "Need TRI instance");
4531
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004532 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004533 if (MO.readsReg())
4534 return 0;
4535 unsigned Reg = MO.getReg();
4536 int UseOp = -1;
4537
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004538 switch (MI.getOpcode()) {
4539 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004540 case ARM::VLDRS:
4541 case ARM::FCONSTS:
4542 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004543 case ARM::VMOVv8i8:
4544 case ARM::VMOVv4i16:
4545 case ARM::VMOVv2i32:
4546 case ARM::VMOVv2f32:
4547 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004548 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004549 break;
4550
4551 // Explicitly reads the dependency.
4552 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004553 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004554 break;
4555 default:
4556 return 0;
4557 }
4558
4559 // If this instruction actually reads a value from Reg, there is no unwanted
4560 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004561 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00004562 return 0;
4563
4564 // We must be able to clobber the whole D-reg.
4565 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4566 // Virtual register must be a foo:ssub_0<def,undef> operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004567 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004568 return 0;
4569 } else if (ARM::SPRRegClass.contains(Reg)) {
4570 // Physical register: MI must define the full D-reg.
4571 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4572 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004573 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004574 return 0;
4575 }
4576
4577 // MI has an unwanted D-register dependency.
4578 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00004579 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004580}
4581
4582// Break a partial register dependency after getPartialRegUpdateClearance
4583// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004584void ARMBaseInstrInfo::breakPartialRegDependency(
4585 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4586 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004587 assert(TRI && "Need TRI instance");
4588
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004589 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004590 unsigned Reg = MO.getReg();
4591 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4592 "Can't break virtual register dependencies.");
4593 unsigned DReg = Reg;
4594
4595 // If MI defines an S-reg, find the corresponding D super-register.
4596 if (ARM::SPRRegClass.contains(Reg)) {
4597 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4598 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4599 }
4600
4601 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004602 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004603
4604 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4605 // the full D-register by loading the same value to both lanes. The
4606 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004607 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004608 // too big regressions.
4609
4610 // Insert the dependency-breaking FCONSTD before MI.
4611 // 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 +00004612 AddDefaultPred(
4613 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4614 .addImm(96));
4615 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004616}
4617
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004618bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004619 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004620}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004621
4622bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004623 if (MI->getNumOperands() < 4)
4624 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004625 unsigned ShOpVal = MI->getOperand(3).getImm();
4626 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4627 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4628 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4629 ((ShImm == 1 || ShImm == 2) &&
4630 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4631 return true;
4632
4633 return false;
4634}
Quentin Colombetd358e842014-08-22 18:05:22 +00004635
4636bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4637 const MachineInstr &MI, unsigned DefIdx,
4638 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4639 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4640 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4641
4642 switch (MI.getOpcode()) {
4643 case ARM::VMOVDRR:
4644 // dX = VMOVDRR rY, rZ
4645 // is the same as:
4646 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4647 // Populate the InputRegs accordingly.
4648 // rY
4649 const MachineOperand *MOReg = &MI.getOperand(1);
4650 InputRegs.push_back(
4651 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4652 // rZ
4653 MOReg = &MI.getOperand(2);
4654 InputRegs.push_back(
4655 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4656 return true;
4657 }
4658 llvm_unreachable("Target dependent opcode missing");
4659}
4660
4661bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4662 const MachineInstr &MI, unsigned DefIdx,
4663 RegSubRegPairAndIdx &InputReg) const {
4664 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4665 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4666
4667 switch (MI.getOpcode()) {
4668 case ARM::VMOVRRD:
4669 // rX, rY = VMOVRRD dZ
4670 // is the same as:
4671 // rX = EXTRACT_SUBREG dZ, ssub_0
4672 // rY = EXTRACT_SUBREG dZ, ssub_1
4673 const MachineOperand &MOReg = MI.getOperand(2);
4674 InputReg.Reg = MOReg.getReg();
4675 InputReg.SubReg = MOReg.getSubReg();
4676 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4677 return true;
4678 }
4679 llvm_unreachable("Target dependent opcode missing");
4680}
4681
4682bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4683 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4684 RegSubRegPairAndIdx &InsertedReg) const {
4685 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4686 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4687
4688 switch (MI.getOpcode()) {
4689 case ARM::VSETLNi32:
4690 // dX = VSETLNi32 dY, rZ, imm
4691 const MachineOperand &MOBaseReg = MI.getOperand(1);
4692 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4693 const MachineOperand &MOIndex = MI.getOperand(3);
4694 BaseReg.Reg = MOBaseReg.getReg();
4695 BaseReg.SubReg = MOBaseReg.getSubReg();
4696
4697 InsertedReg.Reg = MOInsertedReg.getReg();
4698 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4699 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4700 return true;
4701 }
4702 llvm_unreachable("Target dependent opcode missing");
4703}