blob: 9c4b4961fe8c360bdc005cf1b3ad0d52f68227e6 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
David Goodwinaf7451b2009-07-08 16:09:28 +000014#include "ARM.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000015#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000018#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000020#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000023#include "llvm/CodeGen/LiveVariables.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000024#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000030#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000034#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000035#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000036#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000037#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000040#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000041
David Goodwinaf7451b2009-07-08 16:09:28 +000042using namespace llvm;
43
Chandler Carruthe96dd892014-04-21 22:55:11 +000044#define DEBUG_TYPE "arm-instrinfo"
45
Chandler Carruthd174b722014-04-22 02:03:14 +000046#define GET_INSTRINFO_CTOR_DTOR
47#include "ARMGenInstrInfo.inc"
48
David Goodwinaf7451b2009-07-08 16:09:28 +000049static cl::opt<bool>
50EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
51 cl::desc("Enable ARM 2-addr to 3-addr conv"));
52
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000053static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000054WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000055 cl::desc("Widen ARM vmovs to vmovd when possible"));
56
Bob Wilsone8a549c2012-09-29 21:43:49 +000057static cl::opt<unsigned>
58SwiftPartialUpdateClearance("swift-partial-update-clearance",
59 cl::Hidden, cl::init(12),
60 cl::desc("Clearance before partial register updates"));
61
Evan Cheng62c7b5b2010-12-05 22:04:16 +000062/// ARM_MLxEntry - Record information about MLA / MLS instructions.
63struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000064 uint16_t MLxOpc; // MLA / MLS opcode
65 uint16_t MulOpc; // Expanded multiplication opcode
66 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000067 bool NegAcc; // True if the acc is negated before the add / sub.
68 bool HasLane; // True if instruction has an extra "lane" operand.
69};
70
71static const ARM_MLxEntry ARM_MLxTable[] = {
72 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
73 // fp scalar ops
74 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
75 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
76 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
77 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000078 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
81 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
82
83 // fp SIMD ops
84 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
85 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
86 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
87 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
88 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
89 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
90 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
91 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
92};
93
Anton Korobeynikov14635da2009-11-02 00:10:38 +000094ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000095 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000096 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000097 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
98 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
99 assert(false && "Duplicated entries?");
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
101 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
102 }
103}
104
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000105// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
106// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000107ScheduleHazardRecognizer *
108ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
109 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000110 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000111 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000112 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000113 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
114 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000115 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000116}
117
118ScheduleHazardRecognizer *ARMBaseInstrInfo::
119CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
120 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000121 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000122 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000123 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000124}
125
126MachineInstr *
127ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
128 MachineBasicBlock::iterator &MBBI,
129 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000130 // FIXME: Thumb2 support.
131
David Goodwinaf7451b2009-07-08 16:09:28 +0000132 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000133 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000134
135 MachineInstr *MI = MBBI;
136 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000137 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000138 bool isPre = false;
139 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000140 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000141 case ARMII::IndexModePre:
142 isPre = true;
143 break;
144 case ARMII::IndexModePost:
145 break;
146 }
147
148 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
149 // operation.
150 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
151 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000153
Craig Topper062a2ba2014-04-25 05:30:21 +0000154 MachineInstr *UpdateMI = nullptr;
155 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000156 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000157 const MCInstrDesc &MCID = MI->getDesc();
158 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000159 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000160 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
161 const MachineOperand &Base = MI->getOperand(2);
162 const MachineOperand &Offset = MI->getOperand(NumOps-3);
163 unsigned WBReg = WB.getReg();
164 unsigned BaseReg = Base.getReg();
165 unsigned OffReg = Offset.getReg();
166 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
167 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
168 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000169 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000170 case ARMII::AddrMode2: {
171 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
172 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
173 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000174 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000175 // Can't encode it in a so_imm operand. This transformation will
176 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000177 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000178 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000179 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000180 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000181 .addImm(Pred).addReg(0).addReg(0);
182 } else if (Amt != 0) {
183 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
184 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
185 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000186 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000187 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
188 .addImm(Pred).addReg(0).addReg(0);
189 } else
190 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000191 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000192 .addReg(BaseReg).addReg(OffReg)
193 .addImm(Pred).addReg(0).addReg(0);
194 break;
195 }
196 case ARMII::AddrMode3 : {
197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
198 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
199 if (OffReg == 0)
200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
201 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000203 .addReg(BaseReg).addImm(Amt)
204 .addImm(Pred).addReg(0).addReg(0);
205 else
206 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000207 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 .addReg(BaseReg).addReg(OffReg)
209 .addImm(Pred).addReg(0).addReg(0);
210 break;
211 }
212 }
213
214 std::vector<MachineInstr*> NewMIs;
215 if (isPre) {
216 if (isLoad)
217 MemMI = BuildMI(MF, MI->getDebugLoc(),
218 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000219 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000220 else
221 MemMI = BuildMI(MF, MI->getDebugLoc(),
222 get(MemOpc)).addReg(MI->getOperand(1).getReg())
223 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
224 NewMIs.push_back(MemMI);
225 NewMIs.push_back(UpdateMI);
226 } else {
227 if (isLoad)
228 MemMI = BuildMI(MF, MI->getDebugLoc(),
229 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000230 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000231 else
232 MemMI = BuildMI(MF, MI->getDebugLoc(),
233 get(MemOpc)).addReg(MI->getOperand(1).getReg())
234 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
235 if (WB.isDead())
236 UpdateMI->getOperand(0).setIsDead();
237 NewMIs.push_back(UpdateMI);
238 NewMIs.push_back(MemMI);
239 }
240
241 // Transfer LiveVariables states, kill / dead info.
242 if (LV) {
243 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
244 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000245 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000246 unsigned Reg = MO.getReg();
247
248 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
249 if (MO.isDef()) {
250 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
251 if (MO.isDead())
252 LV->addVirtualRegisterDead(Reg, NewMI);
253 }
254 if (MO.isUse() && MO.isKill()) {
255 for (unsigned j = 0; j < 2; ++j) {
256 // Look at the two new MI's in reverse order.
257 MachineInstr *NewMI = NewMIs[j];
258 if (!NewMI->readsRegister(Reg))
259 continue;
260 LV->addVirtualRegisterKilled(Reg, NewMI);
261 if (VI.removeKill(MI))
262 VI.Kills.push_back(NewMI);
263 break;
264 }
265 }
266 }
267 }
268 }
269
270 MFI->insert(MBBI, NewMIs[1]);
271 MFI->insert(MBBI, NewMIs[0]);
272 return NewMIs[0];
273}
274
275// Branch analysis.
276bool
277ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
278 MachineBasicBlock *&FBB,
279 SmallVectorImpl<MachineOperand> &Cond,
280 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000281 TBB = nullptr;
282 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000283
David Goodwinaf7451b2009-07-08 16:09:28 +0000284 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000285 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000286 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000287 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000288
289 // Walk backwards from the end of the basic block until the branch is
290 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000291 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000292
293 // Flag to be raised on unanalyzeable instructions. This is useful in cases
294 // where we want to clean up on the end of the basic block before we bail
295 // out.
296 bool CantAnalyze = false;
297
298 // Skip over DEBUG values and predicated nonterminators.
299 while (I->isDebugValue() || !I->isTerminator()) {
300 if (I == MBB.begin())
301 return false;
302 --I;
303 }
304
305 if (isIndirectBranchOpcode(I->getOpcode()) ||
306 isJumpTableBranchOpcode(I->getOpcode())) {
307 // Indirect branches and jump tables can't be analyzed, but we still want
308 // to clean up any instructions at the tail of the basic block.
309 CantAnalyze = true;
310 } else if (isUncondBranchOpcode(I->getOpcode())) {
311 TBB = I->getOperand(0).getMBB();
312 } else if (isCondBranchOpcode(I->getOpcode())) {
313 // Bail out if we encounter multiple conditional branches.
314 if (!Cond.empty())
315 return true;
316
317 assert(!FBB && "FBB should have been null.");
318 FBB = TBB;
319 TBB = I->getOperand(0).getMBB();
320 Cond.push_back(I->getOperand(1));
321 Cond.push_back(I->getOperand(2));
322 } else if (I->isReturn()) {
323 // Returns can't be analyzed, but we should run cleanup.
324 CantAnalyze = !isPredicated(I);
325 } else {
326 // We encountered other unrecognized terminator. Bail out immediately.
327 return true;
328 }
329
330 // Cleanup code - to be run for unpredicated unconditional branches and
331 // returns.
332 if (!isPredicated(I) &&
333 (isUncondBranchOpcode(I->getOpcode()) ||
334 isIndirectBranchOpcode(I->getOpcode()) ||
335 isJumpTableBranchOpcode(I->getOpcode()) ||
336 I->isReturn())) {
337 // Forget any previous condition branch information - it no longer applies.
338 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000339 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000340
341 // If we can modify the function, delete everything below this
342 // unconditional branch.
343 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000344 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000345 while (DI != MBB.end()) {
346 MachineInstr *InstToDelete = DI;
347 ++DI;
348 InstToDelete->eraseFromParent();
349 }
350 }
351 }
352
353 if (CantAnalyze)
354 return true;
355
Dale Johannesen4244d122010-04-02 01:38:09 +0000356 if (I == MBB.begin())
357 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000358
Dale Johannesen4244d122010-04-02 01:38:09 +0000359 --I;
360 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000361
Lang Hames24864fe2013-07-19 23:52:47 +0000362 // We made it past the terminators without bailing out - we must have
363 // analyzed this branch successfully.
364 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000365}
366
367
368unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000369 MachineBasicBlock::iterator I = MBB.end();
370 if (I == MBB.begin()) return 0;
371 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000372 while (I->isDebugValue()) {
373 if (I == MBB.begin())
374 return 0;
375 --I;
376 }
Evan Cheng056c6692009-07-27 18:20:05 +0000377 if (!isUncondBranchOpcode(I->getOpcode()) &&
378 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000379 return 0;
380
381 // Remove the branch.
382 I->eraseFromParent();
383
384 I = MBB.end();
385
386 if (I == MBB.begin()) return 1;
387 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000388 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000389 return 1;
390
391 // Remove the branch.
392 I->eraseFromParent();
393 return 2;
394}
395
396unsigned
397ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000398 MachineBasicBlock *FBB,
399 const SmallVectorImpl<MachineOperand> &Cond,
400 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000401 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
402 int BOpc = !AFI->isThumbFunction()
403 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
404 int BccOpc = !AFI->isThumbFunction()
405 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000406 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000407
David Goodwinaf7451b2009-07-08 16:09:28 +0000408 // Shouldn't be a fall through.
409 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
410 assert((Cond.size() == 2 || Cond.size() == 0) &&
411 "ARM branch conditions have two components!");
412
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000413 // For conditional branches, we use addOperand to preserve CPSR flags.
414
Craig Topper062a2ba2014-04-25 05:30:21 +0000415 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000416 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000417 if (isThumb)
418 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
419 else
420 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000421 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000422 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000423 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000424 return 1;
425 }
426
427 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000428 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000429 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000430 if (isThumb)
431 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
432 else
433 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000434 return 2;
435}
436
437bool ARMBaseInstrInfo::
438ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
439 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
440 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
441 return false;
442}
443
Evan Cheng7fae11b2011-12-14 02:11:42 +0000444bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
445 if (MI->isBundle()) {
446 MachineBasicBlock::const_instr_iterator I = MI;
447 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
448 while (++I != E && I->isInsideBundle()) {
449 int PIdx = I->findFirstPredOperandIdx();
450 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
451 return true;
452 }
453 return false;
454 }
455
456 int PIdx = MI->findFirstPredOperandIdx();
457 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
458}
459
David Goodwinaf7451b2009-07-08 16:09:28 +0000460bool ARMBaseInstrInfo::
461PredicateInstruction(MachineInstr *MI,
462 const SmallVectorImpl<MachineOperand> &Pred) const {
463 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000464 if (isUncondBranchOpcode(Opc)) {
465 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000466 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
467 .addImm(Pred[0].getImm())
468 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000469 return true;
470 }
471
472 int PIdx = MI->findFirstPredOperandIdx();
473 if (PIdx != -1) {
474 MachineOperand &PMO = MI->getOperand(PIdx);
475 PMO.setImm(Pred[0].getImm());
476 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
477 return true;
478 }
479 return false;
480}
481
482bool ARMBaseInstrInfo::
483SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
484 const SmallVectorImpl<MachineOperand> &Pred2) const {
485 if (Pred1.size() > 2 || Pred2.size() > 2)
486 return false;
487
488 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
489 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
490 if (CC1 == CC2)
491 return true;
492
493 switch (CC1) {
494 default:
495 return false;
496 case ARMCC::AL:
497 return true;
498 case ARMCC::HS:
499 return CC2 == ARMCC::HI;
500 case ARMCC::LS:
501 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
502 case ARMCC::GE:
503 return CC2 == ARMCC::GT;
504 case ARMCC::LE:
505 return CC2 == ARMCC::LT;
506 }
507}
508
509bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
510 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000511 bool Found = false;
512 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
513 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000514 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
515 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000516 Pred.push_back(MO);
517 Found = true;
518 }
519 }
520
521 return Found;
522}
523
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000524static bool isCPSRDefined(const MachineInstr *MI) {
525 for (const auto &MO : MI->operands())
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000526 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000527 return true;
528 return false;
529}
530
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000531static bool isEligibleForITBlock(const MachineInstr *MI) {
532 switch (MI->getOpcode()) {
533 default: return true;
534 case ARM::tADC: // ADC (register) T1
535 case ARM::tADDi3: // ADD (immediate) T1
536 case ARM::tADDi8: // ADD (immediate) T2
537 case ARM::tADDrr: // ADD (register) T1
538 case ARM::tAND: // AND (register) T1
539 case ARM::tASRri: // ASR (immediate) T1
540 case ARM::tASRrr: // ASR (register) T1
541 case ARM::tBIC: // BIC (register) T1
542 case ARM::tEOR: // EOR (register) T1
543 case ARM::tLSLri: // LSL (immediate) T1
544 case ARM::tLSLrr: // LSL (register) T1
545 case ARM::tLSRri: // LSR (immediate) T1
546 case ARM::tLSRrr: // LSR (register) T1
547 case ARM::tMUL: // MUL T1
548 case ARM::tMVN: // MVN (register) T1
549 case ARM::tORR: // ORR (register) T1
550 case ARM::tROR: // ROR (register) T1
551 case ARM::tRSB: // RSB (immediate) T1
552 case ARM::tSBC: // SBC (register) T1
553 case ARM::tSUBi3: // SUB (immediate) T1
554 case ARM::tSUBi8: // SUB (immediate) T2
555 case ARM::tSUBrr: // SUB (register) T1
556 return !isCPSRDefined(MI);
557 }
558}
559
Evan Chenga33fc862009-11-21 06:21:52 +0000560/// isPredicable - Return true if the specified instruction can be predicated.
561/// By default, this returns true for every instruction with a
562/// PredicateOperand.
563bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000564 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000565 return false;
566
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000567 if (!isEligibleForITBlock(MI))
568 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000569
Joey Goulya5153cb2013-09-09 14:21:49 +0000570 ARMFunctionInfo *AFI =
571 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
572
573 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000574 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000575 return isV8EligibleForIT(MI);
576 } else { // non-Thumb
577 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
578 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000579 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000580
Evan Chenga33fc862009-11-21 06:21:52 +0000581 return true;
582}
David Goodwinaf7451b2009-07-08 16:09:28 +0000583
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000584namespace llvm {
585template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000586 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
587 const MachineOperand &MO = MI->getOperand(i);
588 if (!MO.isReg() || MO.isUndef() || MO.isUse())
589 continue;
590 if (MO.getReg() != ARM::CPSR)
591 continue;
592 if (!MO.isDead())
593 return false;
594 }
595 // all definitions of CPSR are dead
596 return true;
597}
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000598}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000599
David Goodwinaf7451b2009-07-08 16:09:28 +0000600/// GetInstSize - Return the size of the specified MachineInstr.
601///
602unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
603 const MachineBasicBlock &MBB = *MI->getParent();
604 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000605 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000606
Evan Cheng6cc775f2011-06-28 19:10:37 +0000607 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000608 if (MCID.getSize())
609 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000610
David Blaikie46a9f012012-01-20 21:51:11 +0000611 // If this machine instr is an inline asm, measure it.
612 if (MI->getOpcode() == ARM::INLINEASM)
613 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000614 unsigned Opc = MI->getOpcode();
615 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000616 default:
617 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000618 return 0;
619 case TargetOpcode::BUNDLE:
620 return getInstBundleLength(MI);
621 case ARM::MOVi16_ga_pcrel:
622 case ARM::MOVTi16_ga_pcrel:
623 case ARM::t2MOVi16_ga_pcrel:
624 case ARM::t2MOVTi16_ga_pcrel:
625 return 4;
626 case ARM::MOVi32imm:
627 case ARM::t2MOVi32imm:
628 return 8;
629 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000630 case ARM::JUMPTABLE_INSTS:
631 case ARM::JUMPTABLE_ADDRS:
632 case ARM::JUMPTABLE_TBB:
633 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000634 // If this machine instr is a constant pool entry, its size is recorded as
635 // operand #2.
636 return MI->getOperand(2).getImm();
637 case ARM::Int_eh_sjlj_longjmp:
638 return 16;
639 case ARM::tInt_eh_sjlj_longjmp:
640 return 10;
641 case ARM::Int_eh_sjlj_setjmp:
642 case ARM::Int_eh_sjlj_setjmp_nofp:
643 return 20;
644 case ARM::tInt_eh_sjlj_setjmp:
645 case ARM::t2Int_eh_sjlj_setjmp:
646 case ARM::t2Int_eh_sjlj_setjmp_nofp:
647 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000648 case ARM::SPACE:
649 return MI->getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000650 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000651}
652
Evan Cheng7fae11b2011-12-14 02:11:42 +0000653unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
654 unsigned Size = 0;
655 MachineBasicBlock::const_instr_iterator I = MI;
656 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
657 while (++I != E && I->isInsideBundle()) {
658 assert(!I->isBundle() && "No nested bundle!");
659 Size += GetInstSizeInBytes(&*I);
660 }
661 return Size;
662}
663
Tim Northover5d72c5d2014-10-01 19:21:03 +0000664void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
665 MachineBasicBlock::iterator I,
666 unsigned DestReg, bool KillSrc,
667 const ARMSubtarget &Subtarget) const {
668 unsigned Opc = Subtarget.isThumb()
669 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
670 : ARM::MRS;
671
672 MachineInstrBuilder MIB =
673 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
674
675 // There is only 1 A/R class MRS instruction, and it always refers to
676 // APSR. However, there are lots of other possibilities on M-class cores.
677 if (Subtarget.isMClass())
678 MIB.addImm(0x800);
679
680 AddDefaultPred(MIB);
681
682 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
683}
684
685void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
686 MachineBasicBlock::iterator I,
687 unsigned SrcReg, bool KillSrc,
688 const ARMSubtarget &Subtarget) const {
689 unsigned Opc = Subtarget.isThumb()
690 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
691 : ARM::MSR;
692
693 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
694
695 if (Subtarget.isMClass())
696 MIB.addImm(0x800);
697 else
698 MIB.addImm(8);
699
700 MIB.addReg(SrcReg, getKillRegState(KillSrc));
701
702 AddDefaultPred(MIB);
703
704 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
705}
706
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000707void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
708 MachineBasicBlock::iterator I, DebugLoc DL,
709 unsigned DestReg, unsigned SrcReg,
710 bool KillSrc) const {
711 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000712 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000713
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000714 if (GPRDest && GPRSrc) {
715 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000716 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000717 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000718 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000719
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000720 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000721 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000722
Chad Rosierbe762512011-08-20 00:17:25 +0000723 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000724 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000725 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000726 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000727 Opc = ARM::VMOVRS;
728 else if (SPRDest && GPRSrc)
729 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000730 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000731 Opc = ARM::VMOVD;
732 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000733 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000734
Chad Rosierbe762512011-08-20 00:17:25 +0000735 if (Opc) {
736 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000737 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000738 if (Opc == ARM::VORRq)
739 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000740 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000741 return;
742 }
743
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000744 // Handle register classes that require multiple instructions.
745 unsigned BeginIdx = 0;
746 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000747 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000748
749 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000750 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
751 Opc = ARM::VORRq;
752 BeginIdx = ARM::qsub_0;
753 SubRegs = 2;
754 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
755 Opc = ARM::VORRq;
756 BeginIdx = ARM::qsub_0;
757 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000758 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000759 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
760 Opc = ARM::VMOVD;
761 BeginIdx = ARM::dsub_0;
762 SubRegs = 2;
763 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
764 Opc = ARM::VMOVD;
765 BeginIdx = ARM::dsub_0;
766 SubRegs = 3;
767 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
768 Opc = ARM::VMOVD;
769 BeginIdx = ARM::dsub_0;
770 SubRegs = 4;
771 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000772 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000773 BeginIdx = ARM::gsub_0;
774 SubRegs = 2;
775 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
776 Opc = ARM::VMOVD;
777 BeginIdx = ARM::dsub_0;
778 SubRegs = 2;
779 Spacing = 2;
780 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
781 Opc = ARM::VMOVD;
782 BeginIdx = ARM::dsub_0;
783 SubRegs = 3;
784 Spacing = 2;
785 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
786 Opc = ARM::VMOVD;
787 BeginIdx = ARM::dsub_0;
788 SubRegs = 4;
789 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000790 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
791 Opc = ARM::VMOVS;
792 BeginIdx = ARM::ssub_0;
793 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000794 } else if (SrcReg == ARM::CPSR) {
795 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
796 return;
797 } else if (DestReg == ARM::CPSR) {
798 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
799 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000800 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000801
Andrew Trickb57e2252012-08-29 04:41:37 +0000802 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000803
Andrew Trick4cc69492012-08-29 01:58:52 +0000804 const TargetRegisterInfo *TRI = &getRegisterInfo();
805 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000806
807 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
808 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000809 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000810 Spacing = -Spacing;
811 }
812#ifndef NDEBUG
813 SmallSet<unsigned, 4> DstRegs;
814#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000815 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000816 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
817 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000818 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000819#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000820 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000821 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000822#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000823 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000824 // VORR takes two source operands.
825 if (Opc == ARM::VORRq)
826 Mov.addReg(Src);
827 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000828 // MOVr can set CC.
829 if (Opc == ARM::MOVr)
830 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000831 }
832 // Add implicit super-register defs and kills to the last instruction.
833 Mov->addRegisterDefined(DestReg, TRI);
834 if (KillSrc)
835 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000836}
837
Tim Northover798697d2013-04-21 11:57:07 +0000838const MachineInstrBuilder &
839ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
840 unsigned SubIdx, unsigned State,
841 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000842 if (!SubIdx)
843 return MIB.addReg(Reg, State);
844
845 if (TargetRegisterInfo::isPhysicalRegister(Reg))
846 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
847 return MIB.addReg(Reg, State, SubIdx);
848}
849
David Goodwinaf7451b2009-07-08 16:09:28 +0000850void ARMBaseInstrInfo::
851storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
852 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000853 const TargetRegisterClass *RC,
854 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000855 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000856 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000857 MachineFunction &MF = *MBB.getParent();
858 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000859 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000860
861 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000862 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000863 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000864 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000865 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000866
Owen Anderson732f82c2011-08-10 17:21:20 +0000867 switch (RC->getSize()) {
868 case 4:
869 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000871 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000872 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000873 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
874 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000875 .addReg(SrcReg, getKillRegState(isKill))
876 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000877 } else
878 llvm_unreachable("Unknown reg class!");
879 break;
880 case 8:
881 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
882 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000883 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000884 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000885 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000886 if (Subtarget.hasV5TEOps()) {
887 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
888 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
889 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
890 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
891
892 AddDefaultPred(MIB);
893 } else {
894 // Fallback to STM instruction, which has existed since the dawn of
895 // time.
896 MachineInstrBuilder MIB =
897 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
898 .addFrameIndex(FI).addMemOperand(MMO));
899 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
900 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
901 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000902 } else
903 llvm_unreachable("Unknown reg class!");
904 break;
905 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000906 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000907 // Use aligned spills if the stack can be realigned.
908 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000909 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000910 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000911 .addReg(SrcReg, getKillRegState(isKill))
912 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000913 } else {
914 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000915 .addReg(SrcReg, getKillRegState(isKill))
916 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000917 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000918 }
919 } else
920 llvm_unreachable("Unknown reg class!");
921 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000922 case 24:
923 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
924 // Use aligned spills if the stack can be realigned.
925 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
926 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
927 .addFrameIndex(FI).addImm(16)
928 .addReg(SrcReg, getKillRegState(isKill))
929 .addMemOperand(MMO));
930 } else {
931 MachineInstrBuilder MIB =
932 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
933 .addFrameIndex(FI))
934 .addMemOperand(MMO);
935 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
936 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
937 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
938 }
939 } else
940 llvm_unreachable("Unknown reg class!");
941 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000942 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000943 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000944 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
945 // FIXME: It's possible to only store part of the QQ register if the
946 // spilled def has a sub-register index.
947 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000948 .addFrameIndex(FI).addImm(16)
949 .addReg(SrcReg, getKillRegState(isKill))
950 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000951 } else {
952 MachineInstrBuilder MIB =
953 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000954 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000955 .addMemOperand(MMO);
956 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
957 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
958 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
959 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
960 }
961 } else
962 llvm_unreachable("Unknown reg class!");
963 break;
964 case 64:
965 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
966 MachineInstrBuilder MIB =
967 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
968 .addFrameIndex(FI))
969 .addMemOperand(MMO);
970 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
971 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
972 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
973 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
974 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
975 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
976 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
977 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
978 } else
979 llvm_unreachable("Unknown reg class!");
980 break;
981 default:
982 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000983 }
984}
985
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000986unsigned
987ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
988 int &FrameIndex) const {
989 switch (MI->getOpcode()) {
990 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000991 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000992 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
993 if (MI->getOperand(1).isFI() &&
994 MI->getOperand(2).isReg() &&
995 MI->getOperand(3).isImm() &&
996 MI->getOperand(2).getReg() == 0 &&
997 MI->getOperand(3).getImm() == 0) {
998 FrameIndex = MI->getOperand(1).getIndex();
999 return MI->getOperand(0).getReg();
1000 }
1001 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001002 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001003 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001004 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001005 case ARM::VSTRD:
1006 case ARM::VSTRS:
1007 if (MI->getOperand(1).isFI() &&
1008 MI->getOperand(2).isImm() &&
1009 MI->getOperand(2).getImm() == 0) {
1010 FrameIndex = MI->getOperand(1).getIndex();
1011 return MI->getOperand(0).getReg();
1012 }
1013 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001014 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001015 case ARM::VST1d64TPseudo:
1016 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001017 if (MI->getOperand(0).isFI() &&
1018 MI->getOperand(2).getSubReg() == 0) {
1019 FrameIndex = MI->getOperand(0).getIndex();
1020 return MI->getOperand(2).getReg();
1021 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001022 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001023 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001024 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001025 MI->getOperand(0).getSubReg() == 0) {
1026 FrameIndex = MI->getOperand(1).getIndex();
1027 return MI->getOperand(0).getReg();
1028 }
1029 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001030 }
1031
1032 return 0;
1033}
1034
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001035unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1036 int &FrameIndex) const {
1037 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001038 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001039}
1040
David Goodwinaf7451b2009-07-08 16:09:28 +00001041void ARMBaseInstrInfo::
1042loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1043 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001044 const TargetRegisterClass *RC,
1045 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001046 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001047 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001048 MachineFunction &MF = *MBB.getParent();
1049 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001050 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001051 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +00001052 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +00001053 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +00001054 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001055 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001056 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001057
Owen Anderson732f82c2011-08-10 17:21:20 +00001058 switch (RC->getSize()) {
1059 case 4:
1060 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1061 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1062 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001063
Owen Anderson732f82c2011-08-10 17:21:20 +00001064 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1065 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001066 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001067 } else
1068 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001069 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001070 case 8:
1071 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1072 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001073 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001074 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001075 MachineInstrBuilder MIB;
1076
1077 if (Subtarget.hasV5TEOps()) {
1078 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1079 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1080 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1081 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1082
1083 AddDefaultPred(MIB);
1084 } else {
1085 // Fallback to LDM instruction, which has existed since the dawn of
1086 // time.
1087 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1088 .addFrameIndex(FI).addMemOperand(MMO));
1089 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1090 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1091 }
1092
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001093 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1094 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001095 } else
1096 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001097 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001098 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001099 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001100 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001101 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001102 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001103 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001104 } else {
1105 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1106 .addFrameIndex(FI)
1107 .addMemOperand(MMO));
1108 }
1109 } else
1110 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001111 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001112 case 24:
1113 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1114 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1115 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1116 .addFrameIndex(FI).addImm(16)
1117 .addMemOperand(MMO));
1118 } else {
1119 MachineInstrBuilder MIB =
1120 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1121 .addFrameIndex(FI)
1122 .addMemOperand(MMO));
1123 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1124 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1125 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1126 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1127 MIB.addReg(DestReg, RegState::ImplicitDefine);
1128 }
1129 } else
1130 llvm_unreachable("Unknown reg class!");
1131 break;
1132 case 32:
1133 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001134 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1135 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001136 .addFrameIndex(FI).addImm(16)
1137 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001138 } else {
1139 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001140 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1141 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001142 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001143 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1144 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1145 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1146 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001147 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1148 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001149 }
1150 } else
1151 llvm_unreachable("Unknown reg class!");
1152 break;
1153 case 64:
1154 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1155 MachineInstrBuilder MIB =
1156 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1157 .addFrameIndex(FI))
1158 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001159 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1160 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1161 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1162 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1163 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1164 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1165 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1166 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001167 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1168 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001169 } else
1170 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001171 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001172 default:
1173 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001174 }
1175}
1176
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001177unsigned
1178ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1179 int &FrameIndex) const {
1180 switch (MI->getOpcode()) {
1181 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001182 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001183 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1184 if (MI->getOperand(1).isFI() &&
1185 MI->getOperand(2).isReg() &&
1186 MI->getOperand(3).isImm() &&
1187 MI->getOperand(2).getReg() == 0 &&
1188 MI->getOperand(3).getImm() == 0) {
1189 FrameIndex = MI->getOperand(1).getIndex();
1190 return MI->getOperand(0).getReg();
1191 }
1192 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001193 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001194 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001195 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001196 case ARM::VLDRD:
1197 case ARM::VLDRS:
1198 if (MI->getOperand(1).isFI() &&
1199 MI->getOperand(2).isImm() &&
1200 MI->getOperand(2).getImm() == 0) {
1201 FrameIndex = MI->getOperand(1).getIndex();
1202 return MI->getOperand(0).getReg();
1203 }
1204 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001205 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001206 case ARM::VLD1d64TPseudo:
1207 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001208 if (MI->getOperand(1).isFI() &&
1209 MI->getOperand(0).getSubReg() == 0) {
1210 FrameIndex = MI->getOperand(1).getIndex();
1211 return MI->getOperand(0).getReg();
1212 }
1213 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001214 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001215 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001216 MI->getOperand(0).getSubReg() == 0) {
1217 FrameIndex = MI->getOperand(1).getIndex();
1218 return MI->getOperand(0).getReg();
1219 }
1220 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001221 }
1222
1223 return 0;
1224}
1225
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001226unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1227 int &FrameIndex) const {
1228 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001229 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001230}
1231
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001232bool
1233ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1234 MachineFunction &MF = *MI->getParent()->getParent();
1235 Reloc::Model RM = MF.getTarget().getRelocationModel();
1236
1237 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
1238 assert(getSubtarget().getTargetTriple().getObjectFormat() ==
1239 Triple::MachO &&
1240 "LOAD_STACK_GUARD currently supported only for MachO.");
1241 expandLoadStackGuard(MI, RM);
1242 MI->getParent()->erase(MI);
1243 return true;
1244 }
1245
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001246 // This hook gets to expand COPY instructions before they become
1247 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1248 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1249 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001250 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1251 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001252 return false;
1253
1254 // Look for a copy between even S-registers. That is where we keep floats
1255 // when using NEON v2f32 instructions for f32 arithmetic.
1256 unsigned DstRegS = MI->getOperand(0).getReg();
1257 unsigned SrcRegS = MI->getOperand(1).getReg();
1258 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1259 return false;
1260
1261 const TargetRegisterInfo *TRI = &getRegisterInfo();
1262 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1263 &ARM::DPRRegClass);
1264 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1265 &ARM::DPRRegClass);
1266 if (!DstRegD || !SrcRegD)
1267 return false;
1268
1269 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1270 // legal if the COPY already defines the full DstRegD, and it isn't a
1271 // sub-register insertion.
1272 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1273 return false;
1274
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001275 // A dead copy shouldn't show up here, but reject it just in case.
1276 if (MI->getOperand(0).isDead())
1277 return false;
1278
1279 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001280 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001281 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001282
1283 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1284 // or some other super-register.
1285 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1286 if (ImpDefIdx != -1)
1287 MI->RemoveOperand(ImpDefIdx);
1288
1289 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001290 MI->setDesc(get(ARM::VMOVD));
1291 MI->getOperand(0).setReg(DstRegD);
1292 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001293 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001294
1295 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1296 // register scavenger and machine verifier, so we need to indicate that we
1297 // are reading an undefined value from SrcRegD, but a proper value from
1298 // SrcRegS.
1299 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001300 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001301
1302 // SrcRegD may actually contain an unrelated value in the ssub_1
1303 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1304 if (MI->getOperand(1).isKill()) {
1305 MI->getOperand(1).setIsKill(false);
1306 MI->addRegisterKilled(SrcRegS, TRI, true);
1307 }
1308
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001309 DEBUG(dbgs() << "replaced by: " << *MI);
1310 return true;
1311}
1312
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001313/// Create a copy of a const pool value. Update CPI to the new index and return
1314/// the label UID.
1315static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1316 MachineConstantPool *MCP = MF.getConstantPool();
1317 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1318
1319 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1320 assert(MCPE.isMachineConstantPoolEntry() &&
1321 "Expecting a machine constantpool entry!");
1322 ARMConstantPoolValue *ACPV =
1323 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1324
Evan Chengdfce83c2011-01-17 08:03:18 +00001325 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001326 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001327
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001328 // FIXME: The below assumes PIC relocation model and that the function
1329 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1330 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1331 // instructions, so that's probably OK, but is PIC always correct when
1332 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001333 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001334 NewCPV = ARMConstantPoolConstant::
1335 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1336 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001337 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001338 NewCPV = ARMConstantPoolSymbol::
1339 Create(MF.getFunction()->getContext(),
1340 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001341 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001342 NewCPV = ARMConstantPoolConstant::
1343 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1344 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001345 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001346 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1347 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001348 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001349 NewCPV = ARMConstantPoolMBB::
1350 Create(MF.getFunction()->getContext(),
1351 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001352 else
1353 llvm_unreachable("Unexpected ARM constantpool value type!!");
1354 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1355 return PCLabelId;
1356}
1357
Evan Chengfe864422009-11-08 00:15:23 +00001358void ARMBaseInstrInfo::
1359reMaterialize(MachineBasicBlock &MBB,
1360 MachineBasicBlock::iterator I,
1361 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001362 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001363 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001364 unsigned Opcode = Orig->getOpcode();
1365 switch (Opcode) {
1366 default: {
1367 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001368 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001369 MBB.insert(I, MI);
1370 break;
1371 }
1372 case ARM::tLDRpci_pic:
1373 case ARM::t2LDRpci_pic: {
1374 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001375 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001376 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001377 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1378 DestReg)
1379 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001380 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001381 break;
1382 }
1383 }
Evan Chengfe864422009-11-08 00:15:23 +00001384}
1385
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001386MachineInstr *
1387ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001388 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001389 switch(Orig->getOpcode()) {
1390 case ARM::tLDRpci_pic:
1391 case ARM::t2LDRpci_pic: {
1392 unsigned CPI = Orig->getOperand(1).getIndex();
1393 unsigned PCLabelId = duplicateCPV(MF, CPI);
1394 Orig->getOperand(1).setIndex(CPI);
1395 Orig->getOperand(2).setImm(PCLabelId);
1396 break;
1397 }
1398 }
1399 return MI;
1400}
1401
Evan Chenge9c46c22010-03-03 01:44:33 +00001402bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001403 const MachineInstr *MI1,
1404 const MachineRegisterInfo *MRI) const {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001405 unsigned Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001406 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001407 Opcode == ARM::t2LDRpci_pic ||
1408 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001409 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001410 Opcode == ARM::LDRLIT_ga_pcrel ||
1411 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1412 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001413 Opcode == ARM::MOV_ga_pcrel ||
1414 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001415 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001416 if (MI1->getOpcode() != Opcode)
1417 return false;
1418 if (MI0->getNumOperands() != MI1->getNumOperands())
1419 return false;
1420
1421 const MachineOperand &MO0 = MI0->getOperand(1);
1422 const MachineOperand &MO1 = MI1->getOperand(1);
1423 if (MO0.getOffset() != MO1.getOffset())
1424 return false;
1425
Tim Northover72360d22013-12-02 10:35:41 +00001426 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1427 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1428 Opcode == ARM::tLDRLIT_ga_pcrel ||
1429 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001430 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001431 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001432 // Ignore the PC labels.
1433 return MO0.getGlobal() == MO1.getGlobal();
1434
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001435 const MachineFunction *MF = MI0->getParent()->getParent();
1436 const MachineConstantPool *MCP = MF->getConstantPool();
1437 int CPI0 = MO0.getIndex();
1438 int CPI1 = MO1.getIndex();
1439 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1440 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001441 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1442 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1443 if (isARMCP0 && isARMCP1) {
1444 ARMConstantPoolValue *ACPV0 =
1445 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1446 ARMConstantPoolValue *ACPV1 =
1447 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1448 return ACPV0->hasSameValue(ACPV1);
1449 } else if (!isARMCP0 && !isARMCP1) {
1450 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1451 }
1452 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001453 } else if (Opcode == ARM::PICLDR) {
1454 if (MI1->getOpcode() != Opcode)
1455 return false;
1456 if (MI0->getNumOperands() != MI1->getNumOperands())
1457 return false;
1458
1459 unsigned Addr0 = MI0->getOperand(1).getReg();
1460 unsigned Addr1 = MI1->getOperand(1).getReg();
1461 if (Addr0 != Addr1) {
1462 if (!MRI ||
1463 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1464 !TargetRegisterInfo::isVirtualRegister(Addr1))
1465 return false;
1466
1467 // This assumes SSA form.
1468 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1469 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1470 // Check if the loaded value, e.g. a constantpool of a global address, are
1471 // the same.
1472 if (!produceSameValue(Def0, Def1, MRI))
1473 return false;
1474 }
1475
1476 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1477 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1478 const MachineOperand &MO0 = MI0->getOperand(i);
1479 const MachineOperand &MO1 = MI1->getOperand(i);
1480 if (!MO0.isIdenticalTo(MO1))
1481 return false;
1482 }
1483 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001484 }
1485
Evan Chenge9c46c22010-03-03 01:44:33 +00001486 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001487}
1488
Bill Wendlingf4707472010-06-23 23:00:16 +00001489/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1490/// determine if two loads are loading from the same base address. It should
1491/// only return true if the base pointers are the same and the only differences
1492/// between the two addresses is the offset. It also returns the offsets by
1493/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001494///
1495/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1496/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001497bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1498 int64_t &Offset1,
1499 int64_t &Offset2) const {
1500 // Don't worry about Thumb: just ARM and Thumb2.
1501 if (Subtarget.isThumb1Only()) return false;
1502
1503 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1504 return false;
1505
1506 switch (Load1->getMachineOpcode()) {
1507 default:
1508 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001509 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001510 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001511 case ARM::LDRD:
1512 case ARM::LDRH:
1513 case ARM::LDRSB:
1514 case ARM::LDRSH:
1515 case ARM::VLDRD:
1516 case ARM::VLDRS:
1517 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001518 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001519 case ARM::t2LDRDi8:
1520 case ARM::t2LDRSHi8:
1521 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001522 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001523 case ARM::t2LDRSHi12:
1524 break;
1525 }
1526
1527 switch (Load2->getMachineOpcode()) {
1528 default:
1529 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001530 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001531 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001532 case ARM::LDRD:
1533 case ARM::LDRH:
1534 case ARM::LDRSB:
1535 case ARM::LDRSH:
1536 case ARM::VLDRD:
1537 case ARM::VLDRS:
1538 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001539 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001540 case ARM::t2LDRSHi8:
1541 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001542 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001543 case ARM::t2LDRSHi12:
1544 break;
1545 }
1546
1547 // Check if base addresses and chain operands match.
1548 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1549 Load1->getOperand(4) != Load2->getOperand(4))
1550 return false;
1551
1552 // Index should be Reg0.
1553 if (Load1->getOperand(3) != Load2->getOperand(3))
1554 return false;
1555
1556 // Determine the offsets.
1557 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1558 isa<ConstantSDNode>(Load2->getOperand(1))) {
1559 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1560 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1561 return true;
1562 }
1563
1564 return false;
1565}
1566
1567/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001568/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001569/// be scheduled togther. On some targets if two loads are loading from
1570/// addresses in the same cache line, it's better if they are scheduled
1571/// together. This function takes two integers that represent the load offsets
1572/// from the common base address. It returns true if it decides it's desirable
1573/// to schedule the two loads together. "NumLoads" is the number of loads that
1574/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001575///
1576/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1577/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001578bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1579 int64_t Offset1, int64_t Offset2,
1580 unsigned NumLoads) const {
1581 // Don't worry about Thumb: just ARM and Thumb2.
1582 if (Subtarget.isThumb1Only()) return false;
1583
1584 assert(Offset2 > Offset1);
1585
1586 if ((Offset2 - Offset1) / 8 > 64)
1587 return false;
1588
Renato Golinb184cd92013-08-14 16:35:29 +00001589 // Check if the machine opcodes are different. If they are different
1590 // then we consider them to not be of the same base address,
1591 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1592 // In this case, they are considered to be the same because they are different
1593 // encoding forms of the same basic instruction.
1594 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1595 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1596 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1597 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1598 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001599 return false; // FIXME: overly conservative?
1600
1601 // Four loads in a row should be sufficient.
1602 if (NumLoads >= 3)
1603 return false;
1604
1605 return true;
1606}
1607
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001608bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1609 const MachineBasicBlock *MBB,
1610 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001611 // Debug info is never a scheduling boundary. It's necessary to be explicit
1612 // due to the special treatment of IT instructions below, otherwise a
1613 // dbg_value followed by an IT will result in the IT instruction being
1614 // considered a scheduling hazard, which is wrong. It should be the actual
1615 // instruction preceding the dbg_value instruction(s), just like it is
1616 // when debug info is not present.
1617 if (MI->isDebugValue())
1618 return false;
1619
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001620 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001621 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001622 return true;
1623
1624 // Treat the start of the IT block as a scheduling boundary, but schedule
1625 // t2IT along with all instructions following it.
1626 // FIXME: This is a big hammer. But the alternative is to add all potential
1627 // true and anti dependencies to IT block instructions as implicit operands
1628 // to the t2IT instruction. The added compile time and complexity does not
1629 // seem worth it.
1630 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001631 // Make sure to skip any dbg_value instructions
1632 while (++I != MBB->end() && I->isDebugValue())
1633 ;
1634 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001635 return true;
1636
1637 // Don't attempt to schedule around any instruction that defines
1638 // a stack-oriented pointer, as it's unlikely to be profitable. This
1639 // saves compile time, because it doesn't require every single
1640 // stack slot reference to depend on the instruction that does the
1641 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001642 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001643 // No ARM calling conventions change the stack pointer. (X86 calling
1644 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001645 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001646 return true;
1647
1648 return false;
1649}
1650
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001651bool ARMBaseInstrInfo::
1652isProfitableToIfCvt(MachineBasicBlock &MBB,
1653 unsigned NumCycles, unsigned ExtraPredCycles,
1654 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001655 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001656 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001657
Peter Collingbourne65295232015-04-23 20:31:30 +00001658 // If we are optimizing for size, see if the branch in the predecessor can be
1659 // lowered to cbn?z by the constant island lowering pass, and return false if
1660 // so. This results in a shorter instruction sequence.
1661 const Function *F = MBB.getParent()->getFunction();
1662 if (F->hasFnAttribute(Attribute::OptimizeForSize) ||
1663 F->hasFnAttribute(Attribute::MinSize)) {
1664 MachineBasicBlock *Pred = *MBB.pred_begin();
1665 if (!Pred->empty()) {
1666 MachineInstr *LastMI = &*Pred->rbegin();
1667 if (LastMI->getOpcode() == ARM::t2Bcc) {
1668 MachineBasicBlock::iterator CmpMI = LastMI;
1669 if (CmpMI != Pred->begin()) {
1670 --CmpMI;
1671 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1672 CmpMI->getOpcode() == ARM::t2CMPri) {
1673 unsigned Reg = CmpMI->getOperand(0).getReg();
1674 unsigned PredReg = 0;
1675 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1676 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1677 isARMLowRegister(Reg))
1678 return false;
1679 }
1680 }
1681 }
1682 }
1683 }
1684
Owen Anderson88af7d02010-09-28 18:32:13 +00001685 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001686 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1687 UnpredCost /= Probability.getDenominator();
1688 UnpredCost += 1; // The branch itself
1689 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001690
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001691 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001692}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001693
Evan Cheng02b184d2010-06-25 22:42:03 +00001694bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001695isProfitableToIfCvt(MachineBasicBlock &TMBB,
1696 unsigned TCycles, unsigned TExtra,
1697 MachineBasicBlock &FMBB,
1698 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001699 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001700 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001701 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001702
Owen Anderson88af7d02010-09-28 18:32:13 +00001703 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001704 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1705 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001706
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001707 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1708 unsigned FUnpredCost = Comp * FCycles;
1709 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001710
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001711 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1712 UnpredCost += 1; // The branch itself
1713 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1714
1715 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001716}
1717
Bob Wilsone8a549c2012-09-29 21:43:49 +00001718bool
1719ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1720 MachineBasicBlock &FMBB) const {
1721 // Reduce false anti-dependencies to let Swift's out-of-order execution
1722 // engine do its thing.
1723 return Subtarget.isSwift();
1724}
1725
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001726/// getInstrPredicate - If instruction is predicated, returns its predicate
1727/// condition, otherwise returns AL. It also returns the condition code
1728/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001729ARMCC::CondCodes
1730llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001731 int PIdx = MI->findFirstPredOperandIdx();
1732 if (PIdx == -1) {
1733 PredReg = 0;
1734 return ARMCC::AL;
1735 }
1736
1737 PredReg = MI->getOperand(PIdx+1).getReg();
1738 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1739}
1740
1741
Matthias Braunfa3872e2015-05-18 20:27:55 +00001742unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001743 if (Opc == ARM::B)
1744 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001745 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001746 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001747 if (Opc == ARM::t2B)
1748 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001749
1750 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001751}
1752
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001753/// commuteInstruction - Handle commutable instructions.
1754MachineInstr *
1755ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1756 switch (MI->getOpcode()) {
1757 case ARM::MOVCCr:
1758 case ARM::t2MOVCCr: {
1759 // MOVCC can be commuted by inverting the condition.
1760 unsigned PredReg = 0;
1761 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1762 // MOVCC AL can't be inverted. Shouldn't happen.
1763 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001764 return nullptr;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001765 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001766 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001767 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001768 // After swapping the MOVCC operands, also invert the condition.
1769 MI->getOperand(MI->findFirstPredOperandIdx())
1770 .setImm(ARMCC::getOppositeCondition(CC));
1771 return MI;
1772 }
1773 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001774 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001775}
Evan Cheng780748d2009-07-28 05:48:47 +00001776
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001777/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001778/// return the defining instruction.
1779static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1780 const MachineRegisterInfo &MRI,
1781 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001782 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001783 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001784 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001785 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001786 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001787 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001788 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001789 // MI is folded into the MOVCC by predicating it.
1790 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001791 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001792 // Check if MI has any non-dead defs or physreg uses. This also detects
1793 // predicated instructions which will be reading CPSR.
1794 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1795 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001796 // Reject frame index operands, PEI can't handle the predicated pseudos.
1797 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001798 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001799 if (!MO.isReg())
1800 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001801 // MI can't have any tied operands, that would conflict with predication.
1802 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001803 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001804 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001805 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001806 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001807 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001808 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001809 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001810 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001811 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001812 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001813}
1814
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001815bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1816 SmallVectorImpl<MachineOperand> &Cond,
1817 unsigned &TrueOp, unsigned &FalseOp,
1818 bool &Optimizable) const {
1819 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1820 "Unknown select instruction");
1821 // MOVCC operands:
1822 // 0: Def.
1823 // 1: True use.
1824 // 2: False use.
1825 // 3: Condition code.
1826 // 4: CPSR use.
1827 TrueOp = 1;
1828 FalseOp = 2;
1829 Cond.push_back(MI->getOperand(3));
1830 Cond.push_back(MI->getOperand(4));
1831 // We can always fold a def.
1832 Optimizable = true;
1833 return false;
1834}
1835
Mehdi Amini22e59742015-01-13 07:07:13 +00001836MachineInstr *
1837ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1838 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1839 bool PreferFalse) const {
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001840 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1841 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001842 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001843 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1844 bool Invert = !DefMI;
1845 if (!DefMI)
1846 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1847 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001848 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001849
Matthias Braun2f169f92013-10-04 16:52:56 +00001850 // Find new register class to use.
1851 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1852 unsigned DestReg = MI->getOperand(0).getReg();
1853 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1854 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001855 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001856
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001857 // Create a new predicated version of DefMI.
1858 // Rfalse is the first use.
1859 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001860 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001861
1862 // Copy all the DefMI operands, excluding its (null) predicate.
1863 const MCInstrDesc &DefDesc = DefMI->getDesc();
1864 for (unsigned i = 1, e = DefDesc.getNumOperands();
1865 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1866 NewMI.addOperand(DefMI->getOperand(i));
1867
1868 unsigned CondCode = MI->getOperand(3).getImm();
1869 if (Invert)
1870 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1871 else
1872 NewMI.addImm(CondCode);
1873 NewMI.addOperand(MI->getOperand(4));
1874
1875 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1876 if (NewMI->hasOptionalDef())
1877 AddDefaultCC(NewMI);
1878
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001879 // The output register value when the predicate is false is an implicit
1880 // register operand tied to the first def.
1881 // The tie makes the register allocator ensure the FalseReg is allocated the
1882 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001883 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001884 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001885 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1886
Mehdi Amini22e59742015-01-13 07:07:13 +00001887 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1888 SeenMIs.insert(NewMI);
1889 SeenMIs.erase(DefMI);
1890
Pete Cooper2127b002015-04-30 23:57:47 +00001891 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1892 // DefMI would be invalid when tranferred inside the loop. Checking for a
1893 // loop is expensive, but at least remove kill flags if they are in different
1894 // BBs.
1895 if (DefMI->getParent() != MI->getParent())
1896 NewMI->clearKillInfo();
1897
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001898 // The caller will erase MI, but not DefMI.
1899 DefMI->eraseFromParent();
1900 return NewMI;
1901}
1902
Andrew Trick924123a2011-09-21 02:20:46 +00001903/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1904/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1905/// def operand.
1906///
1907/// This will go away once we can teach tblgen how to set the optional CPSR def
1908/// operand itself.
1909struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001910 uint16_t PseudoOpc;
1911 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001912};
1913
Craig Topper2fbd1302012-05-24 03:59:11 +00001914static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001915 {ARM::ADDSri, ARM::ADDri},
1916 {ARM::ADDSrr, ARM::ADDrr},
1917 {ARM::ADDSrsi, ARM::ADDrsi},
1918 {ARM::ADDSrsr, ARM::ADDrsr},
1919
1920 {ARM::SUBSri, ARM::SUBri},
1921 {ARM::SUBSrr, ARM::SUBrr},
1922 {ARM::SUBSrsi, ARM::SUBrsi},
1923 {ARM::SUBSrsr, ARM::SUBrsr},
1924
1925 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001926 {ARM::RSBSrsi, ARM::RSBrsi},
1927 {ARM::RSBSrsr, ARM::RSBrsr},
1928
1929 {ARM::t2ADDSri, ARM::t2ADDri},
1930 {ARM::t2ADDSrr, ARM::t2ADDrr},
1931 {ARM::t2ADDSrs, ARM::t2ADDrs},
1932
1933 {ARM::t2SUBSri, ARM::t2SUBri},
1934 {ARM::t2SUBSrr, ARM::t2SUBrr},
1935 {ARM::t2SUBSrs, ARM::t2SUBrs},
1936
1937 {ARM::t2RSBSri, ARM::t2RSBri},
1938 {ARM::t2RSBSrs, ARM::t2RSBrs},
1939};
1940
1941unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001942 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1943 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1944 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001945 return 0;
1946}
1947
Evan Cheng780748d2009-07-28 05:48:47 +00001948void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1949 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1950 unsigned DestReg, unsigned BaseReg, int NumBytes,
1951 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001952 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001953 if (NumBytes == 0 && DestReg != BaseReg) {
1954 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1955 .addReg(BaseReg, RegState::Kill)
1956 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1957 .setMIFlags(MIFlags);
1958 return;
1959 }
1960
Evan Cheng780748d2009-07-28 05:48:47 +00001961 bool isSub = NumBytes < 0;
1962 if (isSub) NumBytes = -NumBytes;
1963
1964 while (NumBytes) {
1965 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1966 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1967 assert(ThisVal && "Didn't extract field correctly");
1968
1969 // We will handle these bits from offset, clear them.
1970 NumBytes &= ~ThisVal;
1971
1972 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1973
1974 // Build the new ADD / SUB.
1975 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1976 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1977 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001978 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1979 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00001980 BaseReg = DestReg;
1981 }
1982}
1983
Weiming Zhao01524852014-03-20 23:28:16 +00001984static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1985 MachineInstr *MI) {
1986 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1987 Subreg.isValid(); ++Subreg)
1988 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1989 MachineBasicBlock::LQR_Dead)
1990 return true;
1991 return false;
1992}
Tim Northoverdee86042013-12-02 14:46:26 +00001993bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1994 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00001995 unsigned NumBytes) {
1996 // This optimisation potentially adds lots of load and store
1997 // micro-operations, it's only really a great benefit to code-size.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001998 if (!MF.getFunction()->hasFnAttribute(Attribute::MinSize))
Tim Northover93bcc662013-11-08 17:18:07 +00001999 return false;
2000
2001 // If only one register is pushed/popped, LLVM can use an LDR/STR
2002 // instead. We can't modify those so make sure we're dealing with an
2003 // instruction we understand.
2004 bool IsPop = isPopOpcode(MI->getOpcode());
2005 bool IsPush = isPushOpcode(MI->getOpcode());
2006 if (!IsPush && !IsPop)
2007 return false;
2008
2009 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2010 MI->getOpcode() == ARM::VLDMDIA_UPD;
2011 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2012 MI->getOpcode() == ARM::tPOP ||
2013 MI->getOpcode() == ARM::tPOP_RET;
2014
2015 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2016 MI->getOperand(1).getReg() == ARM::SP)) &&
2017 "trying to fold sp update into non-sp-updating push/pop");
2018
2019 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2020 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2021 // if this is violated.
2022 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2023 return false;
2024
2025 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2026 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2027 int RegListIdx = IsT1PushPop ? 2 : 4;
2028
2029 // Calculate the space we'll need in terms of registers.
2030 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2031 unsigned RD0Reg, RegsNeeded;
2032 if (IsVFPPushPop) {
2033 RD0Reg = ARM::D0;
2034 RegsNeeded = NumBytes / 8;
2035 } else {
2036 RD0Reg = ARM::R0;
2037 RegsNeeded = NumBytes / 4;
2038 }
2039
2040 // We're going to have to strip all list operands off before
2041 // re-adding them since the order matters, so save the existing ones
2042 // for later.
2043 SmallVector<MachineOperand, 4> RegList;
2044 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2045 RegList.push_back(MI->getOperand(i));
2046
Tim Northover93bcc662013-11-08 17:18:07 +00002047 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002048 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002049
2050 // Now try to find enough space in the reglist to allocate NumBytes.
2051 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002052 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002053 if (!IsPop) {
2054 // Pushing any register is completely harmless, mark the
2055 // register involved as undef since we don't care about it in
2056 // the slightest.
2057 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2058 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002059 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002060 continue;
2061 }
2062
Tim Northover45479dc2013-12-01 14:16:24 +00002063 // However, we can only pop an extra register if it's not live. For
2064 // registers live within the function we might clobber a return value
2065 // register; the other way a register can be live here is if it's
2066 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00002067 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2068 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2069 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00002070 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00002071 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00002072 // VFP pops don't allow holes in the register list, so any skip is fatal
2073 // for our transformation. GPR pops do, so we should just keep looking.
2074 if (IsVFPPushPop)
2075 return false;
2076 else
2077 continue;
2078 }
Tim Northover93bcc662013-11-08 17:18:07 +00002079
2080 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002081 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2082 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002083 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002084 }
2085
2086 if (RegsNeeded > 0)
2087 return false;
2088
2089 // Finally we know we can profitably perform the optimisation so go
2090 // ahead: strip all existing registers off and add them back again
2091 // in the right order.
2092 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2093 MI->RemoveOperand(i);
2094
2095 // Add the complete list back in.
2096 MachineInstrBuilder MIB(MF, &*MI);
2097 for (int i = RegList.size() - 1; i >= 0; --i)
2098 MIB.addOperand(RegList[i]);
2099
2100 return true;
2101}
2102
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002103bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2104 unsigned FrameReg, int &Offset,
2105 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002106 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002107 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002108 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2109 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002110
Evan Cheng780748d2009-07-28 05:48:47 +00002111 // Memory operands in inline assembly always use AddrMode2.
2112 if (Opcode == ARM::INLINEASM)
2113 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002114
Evan Cheng780748d2009-07-28 05:48:47 +00002115 if (Opcode == ARM::ADDri) {
2116 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2117 if (Offset == 0) {
2118 // Turn it into a move.
2119 MI.setDesc(TII.get(ARM::MOVr));
2120 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2121 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002122 Offset = 0;
2123 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002124 } else if (Offset < 0) {
2125 Offset = -Offset;
2126 isSub = true;
2127 MI.setDesc(TII.get(ARM::SUBri));
2128 }
2129
2130 // Common case: small offset, fits into instruction.
2131 if (ARM_AM::getSOImmVal(Offset) != -1) {
2132 // Replace the FrameIndex with sp / fp
2133 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2134 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002135 Offset = 0;
2136 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002137 }
2138
2139 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2140 // as possible.
2141 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2142 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2143
2144 // We will handle these bits from offset, clear them.
2145 Offset &= ~ThisImmVal;
2146
2147 // Get the properly encoded SOImmVal field.
2148 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2149 "Bit extraction didn't work?");
2150 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2151 } else {
2152 unsigned ImmIdx = 0;
2153 int InstrOffs = 0;
2154 unsigned NumBits = 0;
2155 unsigned Scale = 1;
2156 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002157 case ARMII::AddrMode_i12: {
2158 ImmIdx = FrameRegIdx + 1;
2159 InstrOffs = MI.getOperand(ImmIdx).getImm();
2160 NumBits = 12;
2161 break;
2162 }
Evan Cheng780748d2009-07-28 05:48:47 +00002163 case ARMII::AddrMode2: {
2164 ImmIdx = FrameRegIdx+2;
2165 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2166 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2167 InstrOffs *= -1;
2168 NumBits = 12;
2169 break;
2170 }
2171 case ARMII::AddrMode3: {
2172 ImmIdx = FrameRegIdx+2;
2173 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2174 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2175 InstrOffs *= -1;
2176 NumBits = 8;
2177 break;
2178 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002179 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002180 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002181 // Can't fold any offset even if it's zero.
2182 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002183 case ARMII::AddrMode5: {
2184 ImmIdx = FrameRegIdx+1;
2185 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2186 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2187 InstrOffs *= -1;
2188 NumBits = 8;
2189 Scale = 4;
2190 break;
2191 }
2192 default:
2193 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002194 }
2195
2196 Offset += InstrOffs * Scale;
2197 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2198 if (Offset < 0) {
2199 Offset = -Offset;
2200 isSub = true;
2201 }
2202
2203 // Attempt to fold address comp. if opcode has offset bits
2204 if (NumBits > 0) {
2205 // Common case: small offset, fits into instruction.
2206 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2207 int ImmedOffset = Offset / Scale;
2208 unsigned Mask = (1 << NumBits) - 1;
2209 if ((unsigned)Offset <= Mask * Scale) {
2210 // Replace the FrameIndex with sp
2211 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002212 // FIXME: When addrmode2 goes away, this will simplify (like the
2213 // T2 version), as the LDR.i12 versions don't need the encoding
2214 // tricks for the offset value.
2215 if (isSub) {
2216 if (AddrMode == ARMII::AddrMode_i12)
2217 ImmedOffset = -ImmedOffset;
2218 else
2219 ImmedOffset |= 1 << NumBits;
2220 }
Evan Cheng780748d2009-07-28 05:48:47 +00002221 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002222 Offset = 0;
2223 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002224 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002225
Evan Cheng780748d2009-07-28 05:48:47 +00002226 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2227 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002228 if (isSub) {
2229 if (AddrMode == ARMII::AddrMode_i12)
2230 ImmedOffset = -ImmedOffset;
2231 else
2232 ImmedOffset |= 1 << NumBits;
2233 }
Evan Cheng780748d2009-07-28 05:48:47 +00002234 ImmOp.ChangeToImmediate(ImmedOffset);
2235 Offset &= ~(Mask*Scale);
2236 }
2237 }
2238
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002239 Offset = (isSub) ? -Offset : Offset;
2240 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002241}
Bill Wendling7de9d522010-08-06 01:32:48 +00002242
Manman Ren6fa76dc2012-06-29 21:33:59 +00002243/// analyzeCompare - For a comparison instruction, return the source registers
2244/// in SrcReg and SrcReg2 if having two register operands, and the value it
2245/// compares against in CmpValue. Return true if the comparison instruction
2246/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002247bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002248analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2249 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002250 switch (MI->getOpcode()) {
2251 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002252 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002253 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002254 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002255 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002256 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002257 CmpValue = MI->getOperand(1).getImm();
2258 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002259 case ARM::CMPrr:
2260 case ARM::t2CMPrr:
2261 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002262 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002263 CmpMask = ~0;
2264 CmpValue = 0;
2265 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002266 case ARM::TSTri:
2267 case ARM::t2TSTri:
2268 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002269 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002270 CmpMask = MI->getOperand(1).getImm();
2271 CmpValue = 0;
2272 return true;
2273 }
2274
2275 return false;
2276}
2277
Gabor Greifd36e3e82010-09-29 10:12:08 +00002278/// isSuitableForMask - Identify a suitable 'and' instruction that
2279/// operates on the given source register and applies the same mask
2280/// as a 'tst' instruction. Provide a limited look-through for copies.
2281/// When successful, MI will hold the found instruction.
2282static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002283 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002284 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002285 case ARM::ANDri:
2286 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002287 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002288 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002289 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002290 return true;
2291 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002292 }
2293
2294 return false;
2295}
2296
Manman Renb1b3db62012-06-29 22:06:19 +00002297/// getSwappedCondition - assume the flags are set by MI(a,b), return
2298/// the condition code if we modify the instructions such that flags are
2299/// set by MI(b,a).
2300inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2301 switch (CC) {
2302 default: return ARMCC::AL;
2303 case ARMCC::EQ: return ARMCC::EQ;
2304 case ARMCC::NE: return ARMCC::NE;
2305 case ARMCC::HS: return ARMCC::LS;
2306 case ARMCC::LO: return ARMCC::HI;
2307 case ARMCC::HI: return ARMCC::LO;
2308 case ARMCC::LS: return ARMCC::HS;
2309 case ARMCC::GE: return ARMCC::LE;
2310 case ARMCC::LT: return ARMCC::GT;
2311 case ARMCC::GT: return ARMCC::LT;
2312 case ARMCC::LE: return ARMCC::GE;
2313 }
2314}
2315
2316/// isRedundantFlagInstr - check whether the first instruction, whose only
2317/// purpose is to update flags, can be made redundant.
2318/// CMPrr can be made redundant by SUBrr if the operands are the same.
2319/// CMPri can be made redundant by SUBri if the operands are the same.
2320/// This function can be extended later on.
2321inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2322 unsigned SrcReg2, int ImmValue,
2323 MachineInstr *OI) {
2324 if ((CmpI->getOpcode() == ARM::CMPrr ||
2325 CmpI->getOpcode() == ARM::t2CMPrr) &&
2326 (OI->getOpcode() == ARM::SUBrr ||
2327 OI->getOpcode() == ARM::t2SUBrr) &&
2328 ((OI->getOperand(1).getReg() == SrcReg &&
2329 OI->getOperand(2).getReg() == SrcReg2) ||
2330 (OI->getOperand(1).getReg() == SrcReg2 &&
2331 OI->getOperand(2).getReg() == SrcReg)))
2332 return true;
2333
2334 if ((CmpI->getOpcode() == ARM::CMPri ||
2335 CmpI->getOpcode() == ARM::t2CMPri) &&
2336 (OI->getOpcode() == ARM::SUBri ||
2337 OI->getOpcode() == ARM::t2SUBri) &&
2338 OI->getOperand(1).getReg() == SrcReg &&
2339 OI->getOperand(2).getImm() == ImmValue)
2340 return true;
2341 return false;
2342}
2343
Manman Ren6fa76dc2012-06-29 21:33:59 +00002344/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2345/// comparison into one that sets the zero bit in the flags register;
2346/// Remove a redundant Compare instruction if an earlier instruction can set the
2347/// flags in the same way as Compare.
2348/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2349/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2350/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002351bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002352optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2353 int CmpMask, int CmpValue,
2354 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002355 // Get the unique definition of SrcReg.
2356 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2357 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002358
Gabor Greifadbbb932010-09-21 12:01:15 +00002359 // Masked compares sometimes use the same register as the corresponding 'and'.
2360 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002361 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002362 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002363 for (MachineRegisterInfo::use_instr_iterator
2364 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2365 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002366 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002367 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002368 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2369 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002370 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002371 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002372 break;
2373 }
2374 if (!MI) return false;
2375 }
2376 }
2377
Manman Rendc8ad002012-05-11 01:30:47 +00002378 // Get ready to iterate backward from CmpInstr.
2379 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2380 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002381
2382 // Early exit if CmpInstr is at the beginning of the BB.
2383 if (I == B) return false;
2384
Manman Rendc8ad002012-05-11 01:30:47 +00002385 // There are two possible candidates which can be changed to set CPSR:
2386 // One is MI, the other is a SUB instruction.
2387 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2388 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002389 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002390 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002391 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002392 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002393 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002394 // Conservatively refuse to convert an instruction which isn't in the same
2395 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002396 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2397 // Thus we cannot return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002398 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002399 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002400 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002401 else
2402 return false;
2403 }
2404
2405 // Check that CPSR isn't set between the comparison instruction and the one we
2406 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002407 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002408 --I;
2409 for (; I != E; --I) {
2410 const MachineInstr &Instr = *I;
2411
Manman Renb1b3db62012-06-29 22:06:19 +00002412 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2413 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002414 // This instruction modifies or uses CPSR after the one we want to
2415 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002416 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002417
Manman Renb1b3db62012-06-29 22:06:19 +00002418 // Check whether CmpInstr can be made redundant by the current instruction.
2419 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002420 Sub = &*I;
2421 break;
2422 }
2423
Evan Chengd757c882010-09-21 23:49:07 +00002424 if (I == B)
2425 // The 'and' is below the comparison instruction.
2426 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002427 }
2428
Manman Rendc8ad002012-05-11 01:30:47 +00002429 // Return false if no candidates exist.
2430 if (!MI && !Sub)
2431 return false;
2432
2433 // The single candidate is called MI.
2434 if (!MI) MI = Sub;
2435
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002436 // We can't use a predicated instruction - it doesn't always write the flags.
2437 if (isPredicated(MI))
2438 return false;
2439
Bill Wendling7de9d522010-08-06 01:32:48 +00002440 switch (MI->getOpcode()) {
2441 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002442 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002443 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002444 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002445 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002446 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002447 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002448 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002449 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002450 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002451 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002452 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002453 case ARM::SBCri:
2454 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002455 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002456 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002457 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002458 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002459 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002460 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002461 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002462 case ARM::t2SBCri:
2463 case ARM::ANDrr:
2464 case ARM::ANDri:
2465 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002466 case ARM::t2ANDri:
2467 case ARM::ORRrr:
2468 case ARM::ORRri:
2469 case ARM::t2ORRrr:
2470 case ARM::t2ORRri:
2471 case ARM::EORrr:
2472 case ARM::EORri:
2473 case ARM::t2EORrr:
2474 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002475 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002476 // When checking against MI: if it's a conditional code that requires
2477 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002478 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2479 // If we are done with the basic block, we need to check whether CPSR is
2480 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002481 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2482 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002483 bool isSafe = false;
2484 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002485 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002486 while (!isSafe && ++I != E) {
2487 const MachineInstr &Instr = *I;
2488 for (unsigned IO = 0, EO = Instr.getNumOperands();
2489 !isSafe && IO != EO; ++IO) {
2490 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002491 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2492 isSafe = true;
2493 break;
2494 }
Evan Cheng425489d2011-03-23 22:52:04 +00002495 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2496 continue;
2497 if (MO.isDef()) {
2498 isSafe = true;
2499 break;
2500 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002501 // Condition code is after the operand before CPSR except for VSELs.
2502 ARMCC::CondCodes CC;
2503 bool IsInstrVSel = true;
2504 switch (Instr.getOpcode()) {
2505 default:
2506 IsInstrVSel = false;
2507 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2508 break;
2509 case ARM::VSELEQD:
2510 case ARM::VSELEQS:
2511 CC = ARMCC::EQ;
2512 break;
2513 case ARM::VSELGTD:
2514 case ARM::VSELGTS:
2515 CC = ARMCC::GT;
2516 break;
2517 case ARM::VSELGED:
2518 case ARM::VSELGES:
2519 CC = ARMCC::GE;
2520 break;
2521 case ARM::VSELVSS:
2522 case ARM::VSELVSD:
2523 CC = ARMCC::VS;
2524 break;
2525 }
2526
Manman Renb1b3db62012-06-29 22:06:19 +00002527 if (Sub) {
2528 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2529 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002530 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002531 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2532 // on CMP needs to be updated to be based on SUB.
2533 // Push the condition code operands to OperandsToUpdate.
2534 // If it is safe to remove CmpInstr, the condition code of these
2535 // operands will be modified.
2536 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002537 Sub->getOperand(2).getReg() == SrcReg) {
2538 // VSel doesn't support condition code update.
2539 if (IsInstrVSel)
2540 return false;
2541 OperandsToUpdate.push_back(
2542 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2543 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002544 } else {
2545 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002546 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002547 case ARMCC::EQ: // Z
2548 case ARMCC::NE: // Z
2549 case ARMCC::MI: // N
2550 case ARMCC::PL: // N
2551 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002552 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002553 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002554 case ARMCC::HS: // C
2555 case ARMCC::LO: // C
2556 case ARMCC::VS: // V
2557 case ARMCC::VC: // V
2558 case ARMCC::HI: // C Z
2559 case ARMCC::LS: // C Z
2560 case ARMCC::GE: // N V
2561 case ARMCC::LT: // N V
2562 case ARMCC::GT: // Z N V
2563 case ARMCC::LE: // Z N V
2564 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002565 return false;
2566 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002567 }
Evan Cheng425489d2011-03-23 22:52:04 +00002568 }
2569 }
2570
Manman Ren34cb93e2012-07-11 22:51:44 +00002571 // If CPSR is not killed nor re-defined, we should check whether it is
2572 // live-out. If it is live-out, do not optimize.
2573 if (!isSafe) {
2574 MachineBasicBlock *MBB = CmpInstr->getParent();
2575 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2576 SE = MBB->succ_end(); SI != SE; ++SI)
2577 if ((*SI)->isLiveIn(ARM::CPSR))
2578 return false;
2579 }
Evan Cheng425489d2011-03-23 22:52:04 +00002580
Evan Cheng65536472010-11-17 08:06:50 +00002581 // Toggle the optional operand to CPSR.
2582 MI->getOperand(5).setReg(ARM::CPSR);
2583 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002584 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002585 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002586
2587 // Modify the condition code of operands in OperandsToUpdate.
2588 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2589 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002590 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2591 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002592 return true;
2593 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002594 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002595
2596 return false;
2597}
Evan Cheng367a5df2010-09-09 18:18:55 +00002598
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002599bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2600 MachineInstr *DefMI, unsigned Reg,
2601 MachineRegisterInfo *MRI) const {
2602 // Fold large immediates into add, sub, or, xor.
2603 unsigned DefOpc = DefMI->getOpcode();
2604 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2605 return false;
2606 if (!DefMI->getOperand(1).isImm())
2607 // Could be t2MOVi32imm <ga:xx>
2608 return false;
2609
2610 if (!MRI->hasOneNonDBGUse(Reg))
2611 return false;
2612
Evan Chenga2b48d92012-03-26 23:31:00 +00002613 const MCInstrDesc &DefMCID = DefMI->getDesc();
2614 if (DefMCID.hasOptionalDef()) {
2615 unsigned NumOps = DefMCID.getNumOperands();
2616 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2617 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2618 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2619 // to delete DefMI.
2620 return false;
2621 }
2622
2623 const MCInstrDesc &UseMCID = UseMI->getDesc();
2624 if (UseMCID.hasOptionalDef()) {
2625 unsigned NumOps = UseMCID.getNumOperands();
2626 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2627 // If the instruction sets the flag, do not attempt this optimization
2628 // since it may change the semantics of the code.
2629 return false;
2630 }
2631
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002632 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002633 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002634 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002635 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002636 bool Commute = false;
2637 switch (UseOpc) {
2638 default: return false;
2639 case ARM::SUBrr:
2640 case ARM::ADDrr:
2641 case ARM::ORRrr:
2642 case ARM::EORrr:
2643 case ARM::t2SUBrr:
2644 case ARM::t2ADDrr:
2645 case ARM::t2ORRrr:
2646 case ARM::t2EORrr: {
2647 Commute = UseMI->getOperand(2).getReg() != Reg;
2648 switch (UseOpc) {
2649 default: break;
2650 case ARM::SUBrr: {
2651 if (Commute)
2652 return false;
2653 ImmVal = -ImmVal;
2654 NewUseOpc = ARM::SUBri;
2655 // Fallthrough
2656 }
2657 case ARM::ADDrr:
2658 case ARM::ORRrr:
2659 case ARM::EORrr: {
2660 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2661 return false;
2662 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2663 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2664 switch (UseOpc) {
2665 default: break;
2666 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2667 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2668 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2669 }
2670 break;
2671 }
2672 case ARM::t2SUBrr: {
2673 if (Commute)
2674 return false;
2675 ImmVal = -ImmVal;
2676 NewUseOpc = ARM::t2SUBri;
2677 // Fallthrough
2678 }
2679 case ARM::t2ADDrr:
2680 case ARM::t2ORRrr:
2681 case ARM::t2EORrr: {
2682 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2683 return false;
2684 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2685 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2686 switch (UseOpc) {
2687 default: break;
2688 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2689 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2690 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2691 }
2692 break;
2693 }
2694 }
2695 }
2696 }
2697
2698 unsigned OpIdx = Commute ? 2 : 1;
2699 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2700 bool isKill = UseMI->getOperand(OpIdx).isKill();
2701 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2702 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002703 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002704 get(NewUseOpc), NewReg)
2705 .addReg(Reg1, getKillRegState(isKill))
2706 .addImm(SOImmValV1)));
2707 UseMI->setDesc(get(NewUseOpc));
2708 UseMI->getOperand(1).setReg(NewReg);
2709 UseMI->getOperand(1).setIsKill();
2710 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2711 DefMI->eraseFromParent();
2712 return true;
2713}
2714
Bob Wilsone8a549c2012-09-29 21:43:49 +00002715static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2716 const MachineInstr *MI) {
2717 switch (MI->getOpcode()) {
2718 default: {
2719 const MCInstrDesc &Desc = MI->getDesc();
2720 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2721 assert(UOps >= 0 && "bad # UOps");
2722 return UOps;
2723 }
2724
2725 case ARM::LDRrs:
2726 case ARM::LDRBrs:
2727 case ARM::STRrs:
2728 case ARM::STRBrs: {
2729 unsigned ShOpVal = MI->getOperand(3).getImm();
2730 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2731 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2732 if (!isSub &&
2733 (ShImm == 0 ||
2734 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2735 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2736 return 1;
2737 return 2;
2738 }
2739
2740 case ARM::LDRH:
2741 case ARM::STRH: {
2742 if (!MI->getOperand(2).getReg())
2743 return 1;
2744
2745 unsigned ShOpVal = MI->getOperand(3).getImm();
2746 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2747 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2748 if (!isSub &&
2749 (ShImm == 0 ||
2750 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2751 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2752 return 1;
2753 return 2;
2754 }
2755
2756 case ARM::LDRSB:
2757 case ARM::LDRSH:
2758 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2759
2760 case ARM::LDRSB_POST:
2761 case ARM::LDRSH_POST: {
2762 unsigned Rt = MI->getOperand(0).getReg();
2763 unsigned Rm = MI->getOperand(3).getReg();
2764 return (Rt == Rm) ? 4 : 3;
2765 }
2766
2767 case ARM::LDR_PRE_REG:
2768 case ARM::LDRB_PRE_REG: {
2769 unsigned Rt = MI->getOperand(0).getReg();
2770 unsigned Rm = MI->getOperand(3).getReg();
2771 if (Rt == Rm)
2772 return 3;
2773 unsigned ShOpVal = MI->getOperand(4).getImm();
2774 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2775 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2776 if (!isSub &&
2777 (ShImm == 0 ||
2778 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2779 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2780 return 2;
2781 return 3;
2782 }
2783
2784 case ARM::STR_PRE_REG:
2785 case ARM::STRB_PRE_REG: {
2786 unsigned ShOpVal = MI->getOperand(4).getImm();
2787 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2788 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2789 if (!isSub &&
2790 (ShImm == 0 ||
2791 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2792 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2793 return 2;
2794 return 3;
2795 }
2796
2797 case ARM::LDRH_PRE:
2798 case ARM::STRH_PRE: {
2799 unsigned Rt = MI->getOperand(0).getReg();
2800 unsigned Rm = MI->getOperand(3).getReg();
2801 if (!Rm)
2802 return 2;
2803 if (Rt == Rm)
2804 return 3;
2805 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2806 ? 3 : 2;
2807 }
2808
2809 case ARM::LDR_POST_REG:
2810 case ARM::LDRB_POST_REG:
2811 case ARM::LDRH_POST: {
2812 unsigned Rt = MI->getOperand(0).getReg();
2813 unsigned Rm = MI->getOperand(3).getReg();
2814 return (Rt == Rm) ? 3 : 2;
2815 }
2816
2817 case ARM::LDR_PRE_IMM:
2818 case ARM::LDRB_PRE_IMM:
2819 case ARM::LDR_POST_IMM:
2820 case ARM::LDRB_POST_IMM:
2821 case ARM::STRB_POST_IMM:
2822 case ARM::STRB_POST_REG:
2823 case ARM::STRB_PRE_IMM:
2824 case ARM::STRH_POST:
2825 case ARM::STR_POST_IMM:
2826 case ARM::STR_POST_REG:
2827 case ARM::STR_PRE_IMM:
2828 return 2;
2829
2830 case ARM::LDRSB_PRE:
2831 case ARM::LDRSH_PRE: {
2832 unsigned Rm = MI->getOperand(3).getReg();
2833 if (Rm == 0)
2834 return 3;
2835 unsigned Rt = MI->getOperand(0).getReg();
2836 if (Rt == Rm)
2837 return 4;
2838 unsigned ShOpVal = MI->getOperand(4).getImm();
2839 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2840 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2841 if (!isSub &&
2842 (ShImm == 0 ||
2843 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2844 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2845 return 3;
2846 return 4;
2847 }
2848
2849 case ARM::LDRD: {
2850 unsigned Rt = MI->getOperand(0).getReg();
2851 unsigned Rn = MI->getOperand(2).getReg();
2852 unsigned Rm = MI->getOperand(3).getReg();
2853 if (Rm)
2854 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2855 return (Rt == Rn) ? 3 : 2;
2856 }
2857
2858 case ARM::STRD: {
2859 unsigned Rm = MI->getOperand(3).getReg();
2860 if (Rm)
2861 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2862 return 2;
2863 }
2864
2865 case ARM::LDRD_POST:
2866 case ARM::t2LDRD_POST:
2867 return 3;
2868
2869 case ARM::STRD_POST:
2870 case ARM::t2STRD_POST:
2871 return 4;
2872
2873 case ARM::LDRD_PRE: {
2874 unsigned Rt = MI->getOperand(0).getReg();
2875 unsigned Rn = MI->getOperand(3).getReg();
2876 unsigned Rm = MI->getOperand(4).getReg();
2877 if (Rm)
2878 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2879 return (Rt == Rn) ? 4 : 3;
2880 }
2881
2882 case ARM::t2LDRD_PRE: {
2883 unsigned Rt = MI->getOperand(0).getReg();
2884 unsigned Rn = MI->getOperand(3).getReg();
2885 return (Rt == Rn) ? 4 : 3;
2886 }
2887
2888 case ARM::STRD_PRE: {
2889 unsigned Rm = MI->getOperand(4).getReg();
2890 if (Rm)
2891 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2892 return 3;
2893 }
2894
2895 case ARM::t2STRD_PRE:
2896 return 3;
2897
2898 case ARM::t2LDR_POST:
2899 case ARM::t2LDRB_POST:
2900 case ARM::t2LDRB_PRE:
2901 case ARM::t2LDRSBi12:
2902 case ARM::t2LDRSBi8:
2903 case ARM::t2LDRSBpci:
2904 case ARM::t2LDRSBs:
2905 case ARM::t2LDRH_POST:
2906 case ARM::t2LDRH_PRE:
2907 case ARM::t2LDRSBT:
2908 case ARM::t2LDRSB_POST:
2909 case ARM::t2LDRSB_PRE:
2910 case ARM::t2LDRSH_POST:
2911 case ARM::t2LDRSH_PRE:
2912 case ARM::t2LDRSHi12:
2913 case ARM::t2LDRSHi8:
2914 case ARM::t2LDRSHpci:
2915 case ARM::t2LDRSHs:
2916 return 2;
2917
2918 case ARM::t2LDRDi8: {
2919 unsigned Rt = MI->getOperand(0).getReg();
2920 unsigned Rn = MI->getOperand(2).getReg();
2921 return (Rt == Rn) ? 3 : 2;
2922 }
2923
2924 case ARM::t2STRB_POST:
2925 case ARM::t2STRB_PRE:
2926 case ARM::t2STRBs:
2927 case ARM::t2STRDi8:
2928 case ARM::t2STRH_POST:
2929 case ARM::t2STRH_PRE:
2930 case ARM::t2STRHs:
2931 case ARM::t2STR_POST:
2932 case ARM::t2STR_PRE:
2933 case ARM::t2STRs:
2934 return 2;
2935 }
2936}
2937
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002938// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2939// can't be easily determined return 0 (missing MachineMemOperand).
2940//
2941// FIXME: The current MachineInstr design does not support relying on machine
2942// mem operands to determine the width of a memory access. Instead, we expect
2943// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002944// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002945// two reasons:
2946//
2947// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2948// operands. This is much more dangerous than using the MachineMemOperand
2949// sizes because CodeGen passes can insert/remove optional machine operands. In
2950// fact, it's totally incorrect for preRA passes and appears to be wrong for
2951// postRA passes as well.
2952//
2953// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2954// machine model that calls this should handle the unknown (zero size) case.
2955//
2956// Long term, we should require a target hook that verifies MachineMemOperand
2957// sizes during MC lowering. That target hook should be local to MC lowering
2958// because we can't ensure that it is aware of other MI forms. Doing this will
2959// ensure that MachineMemOperands are correctly propagated through all passes.
2960unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2961 unsigned Size = 0;
2962 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2963 E = MI->memoperands_end(); I != E; ++I) {
2964 Size += (*I)->getSize();
2965 }
2966 return Size / 4;
2967}
2968
Evan Cheng367a5df2010-09-09 18:18:55 +00002969unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002970ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2971 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002972 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002973 return 1;
2974
Evan Cheng6cc775f2011-06-28 19:10:37 +00002975 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002976 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002977 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002978 if (ItinUOps >= 0) {
2979 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2980 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2981
Andrew Trickf161e392012-07-02 18:10:42 +00002982 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002983 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002984
2985 unsigned Opc = MI->getOpcode();
2986 switch (Opc) {
2987 default:
2988 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002989 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002990 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002991 return 2;
2992
2993 // The number of uOps for load / store multiple are determined by the number
2994 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002995 //
Evan Chengbf407072010-09-10 01:29:16 +00002996 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2997 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002998 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002999 //
Evan Chengbf407072010-09-10 01:29:16 +00003000 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003001 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3002 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3003 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003004 case ARM::VLDMDIA_UPD:
3005 case ARM::VLDMDDB_UPD:
3006 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003007 case ARM::VLDMSIA_UPD:
3008 case ARM::VLDMSDB_UPD:
3009 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003010 case ARM::VSTMDIA_UPD:
3011 case ARM::VSTMDDB_UPD:
3012 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003013 case ARM::VSTMSIA_UPD:
3014 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003015 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3016 return (NumRegs / 2) + (NumRegs % 2) + 1;
3017 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003018
3019 case ARM::LDMIA_RET:
3020 case ARM::LDMIA:
3021 case ARM::LDMDA:
3022 case ARM::LDMDB:
3023 case ARM::LDMIB:
3024 case ARM::LDMIA_UPD:
3025 case ARM::LDMDA_UPD:
3026 case ARM::LDMDB_UPD:
3027 case ARM::LDMIB_UPD:
3028 case ARM::STMIA:
3029 case ARM::STMDA:
3030 case ARM::STMDB:
3031 case ARM::STMIB:
3032 case ARM::STMIA_UPD:
3033 case ARM::STMDA_UPD:
3034 case ARM::STMDB_UPD:
3035 case ARM::STMIB_UPD:
3036 case ARM::tLDMIA:
3037 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003038 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003039 case ARM::tPOP_RET:
3040 case ARM::tPOP:
3041 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003042 case ARM::t2LDMIA_RET:
3043 case ARM::t2LDMIA:
3044 case ARM::t2LDMDB:
3045 case ARM::t2LDMIA_UPD:
3046 case ARM::t2LDMDB_UPD:
3047 case ARM::t2STMIA:
3048 case ARM::t2STMDB:
3049 case ARM::t2STMIA_UPD:
3050 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003051 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003052 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003053 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3054 switch (Opc) {
3055 default: break;
3056 case ARM::VLDMDIA_UPD:
3057 case ARM::VLDMDDB_UPD:
3058 case ARM::VLDMSIA_UPD:
3059 case ARM::VLDMSDB_UPD:
3060 case ARM::VSTMDIA_UPD:
3061 case ARM::VSTMDDB_UPD:
3062 case ARM::VSTMSIA_UPD:
3063 case ARM::VSTMSDB_UPD:
3064 case ARM::LDMIA_UPD:
3065 case ARM::LDMDA_UPD:
3066 case ARM::LDMDB_UPD:
3067 case ARM::LDMIB_UPD:
3068 case ARM::STMIA_UPD:
3069 case ARM::STMDA_UPD:
3070 case ARM::STMDB_UPD:
3071 case ARM::STMIB_UPD:
3072 case ARM::tLDMIA_UPD:
3073 case ARM::tSTMIA_UPD:
3074 case ARM::t2LDMIA_UPD:
3075 case ARM::t2LDMDB_UPD:
3076 case ARM::t2STMIA_UPD:
3077 case ARM::t2STMDB_UPD:
3078 ++UOps; // One for base register writeback.
3079 break;
3080 case ARM::LDMIA_RET:
3081 case ARM::tPOP_RET:
3082 case ARM::t2LDMIA_RET:
3083 UOps += 2; // One for base reg wb, one for write to pc.
3084 break;
3085 }
3086 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003087 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003088 if (NumRegs < 4)
3089 return 2;
3090 // 4 registers would be issued: 2, 2.
3091 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003092 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003093 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003094 ++A8UOps;
3095 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003096 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003097 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003098 // If there are odd number of registers or if it's not 64-bit aligned,
3099 // then it takes an extra AGU (Address Generation Unit) cycle.
3100 if ((NumRegs % 2) ||
3101 !MI->hasOneMemOperand() ||
3102 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003103 ++A9UOps;
3104 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003105 } else {
3106 // Assume the worst.
3107 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003108 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003109 }
3110 }
3111}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003112
3113int
Evan Cheng412e37b2010-10-07 23:12:15 +00003114ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003115 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003116 unsigned DefClass,
3117 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003118 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003119 if (RegNo <= 0)
3120 // Def is the address writeback.
3121 return ItinData->getOperandCycle(DefClass, DefIdx);
3122
3123 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003124 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003125 // (regno / 2) + (regno % 2) + 1
3126 DefCycle = RegNo / 2 + 1;
3127 if (RegNo % 2)
3128 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003129 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003130 DefCycle = RegNo;
3131 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003132
Evan Cheng6cc775f2011-06-28 19:10:37 +00003133 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003134 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003135 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003136 case ARM::VLDMSIA_UPD:
3137 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003138 isSLoad = true;
3139 break;
3140 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003141
Evan Cheng412e37b2010-10-07 23:12:15 +00003142 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3143 // then it takes an extra cycle.
3144 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3145 ++DefCycle;
3146 } else {
3147 // Assume the worst.
3148 DefCycle = RegNo + 2;
3149 }
3150
3151 return DefCycle;
3152}
3153
3154int
3155ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003156 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003157 unsigned DefClass,
3158 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003159 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003160 if (RegNo <= 0)
3161 // Def is the address writeback.
3162 return ItinData->getOperandCycle(DefClass, DefIdx);
3163
3164 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003165 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003166 // 4 registers would be issued: 1, 2, 1.
3167 // 5 registers would be issued: 1, 2, 2.
3168 DefCycle = RegNo / 2;
3169 if (DefCycle < 1)
3170 DefCycle = 1;
3171 // Result latency is issue cycle + 2: E2.
3172 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003173 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003174 DefCycle = (RegNo / 2);
3175 // If there are odd number of registers or if it's not 64-bit aligned,
3176 // then it takes an extra AGU (Address Generation Unit) cycle.
3177 if ((RegNo % 2) || DefAlign < 8)
3178 ++DefCycle;
3179 // Result latency is AGU cycles + 2.
3180 DefCycle += 2;
3181 } else {
3182 // Assume the worst.
3183 DefCycle = RegNo + 2;
3184 }
3185
3186 return DefCycle;
3187}
3188
3189int
3190ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003191 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003192 unsigned UseClass,
3193 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003194 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003195 if (RegNo <= 0)
3196 return ItinData->getOperandCycle(UseClass, UseIdx);
3197
3198 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003199 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003200 // (regno / 2) + (regno % 2) + 1
3201 UseCycle = RegNo / 2 + 1;
3202 if (RegNo % 2)
3203 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003204 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003205 UseCycle = RegNo;
3206 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003207
Evan Cheng6cc775f2011-06-28 19:10:37 +00003208 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003209 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003210 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003211 case ARM::VSTMSIA_UPD:
3212 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003213 isSStore = true;
3214 break;
3215 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003216
Evan Cheng412e37b2010-10-07 23:12:15 +00003217 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3218 // then it takes an extra cycle.
3219 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3220 ++UseCycle;
3221 } else {
3222 // Assume the worst.
3223 UseCycle = RegNo + 2;
3224 }
3225
3226 return UseCycle;
3227}
3228
3229int
3230ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003231 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003232 unsigned UseClass,
3233 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003234 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003235 if (RegNo <= 0)
3236 return ItinData->getOperandCycle(UseClass, UseIdx);
3237
3238 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003239 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003240 UseCycle = RegNo / 2;
3241 if (UseCycle < 2)
3242 UseCycle = 2;
3243 // Read in E3.
3244 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003245 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003246 UseCycle = (RegNo / 2);
3247 // If there are odd number of registers or if it's not 64-bit aligned,
3248 // then it takes an extra AGU (Address Generation Unit) cycle.
3249 if ((RegNo % 2) || UseAlign < 8)
3250 ++UseCycle;
3251 } else {
3252 // Assume the worst.
3253 UseCycle = 1;
3254 }
3255 return UseCycle;
3256}
3257
3258int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003259ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003260 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003261 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003262 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003263 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003264 unsigned DefClass = DefMCID.getSchedClass();
3265 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003266
Evan Cheng6cc775f2011-06-28 19:10:37 +00003267 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003268 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3269
3270 // This may be a def / use of a variable_ops instruction, the operand
3271 // latency might be determinable dynamically. Let the target try to
3272 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003273 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003274 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003275 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003276 default:
3277 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3278 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003279
3280 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003281 case ARM::VLDMDIA_UPD:
3282 case ARM::VLDMDDB_UPD:
3283 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003284 case ARM::VLDMSIA_UPD:
3285 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003286 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003287 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003288
3289 case ARM::LDMIA_RET:
3290 case ARM::LDMIA:
3291 case ARM::LDMDA:
3292 case ARM::LDMDB:
3293 case ARM::LDMIB:
3294 case ARM::LDMIA_UPD:
3295 case ARM::LDMDA_UPD:
3296 case ARM::LDMDB_UPD:
3297 case ARM::LDMIB_UPD:
3298 case ARM::tLDMIA:
3299 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003300 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003301 case ARM::t2LDMIA_RET:
3302 case ARM::t2LDMIA:
3303 case ARM::t2LDMDB:
3304 case ARM::t2LDMIA_UPD:
3305 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003306 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003307 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003308 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003309 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003310
3311 if (DefCycle == -1)
3312 // We can't seem to determine the result latency of the def, assume it's 2.
3313 DefCycle = 2;
3314
3315 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003316 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003317 default:
3318 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3319 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003320
3321 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003322 case ARM::VSTMDIA_UPD:
3323 case ARM::VSTMDDB_UPD:
3324 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003325 case ARM::VSTMSIA_UPD:
3326 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003327 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003328 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003329
3330 case ARM::STMIA:
3331 case ARM::STMDA:
3332 case ARM::STMDB:
3333 case ARM::STMIB:
3334 case ARM::STMIA_UPD:
3335 case ARM::STMDA_UPD:
3336 case ARM::STMDB_UPD:
3337 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003338 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003339 case ARM::tPOP_RET:
3340 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003341 case ARM::t2STMIA:
3342 case ARM::t2STMDB:
3343 case ARM::t2STMIA_UPD:
3344 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003345 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003346 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003347 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003348
3349 if (UseCycle == -1)
3350 // Assume it's read in the first stage.
3351 UseCycle = 1;
3352
3353 UseCycle = DefCycle - UseCycle + 1;
3354 if (UseCycle > 0) {
3355 if (LdmBypass) {
3356 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3357 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003358 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003359 UseClass, UseIdx))
3360 --UseCycle;
3361 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003362 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003363 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003364 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003365 }
3366
3367 return UseCycle;
3368}
3369
Evan Cheng7fae11b2011-12-14 02:11:42 +00003370static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003371 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003372 unsigned &DefIdx, unsigned &Dist) {
3373 Dist = 0;
3374
3375 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003376 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003377 assert(II->isInsideBundle() && "Empty bundle?");
3378
3379 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003380 while (II->isInsideBundle()) {
3381 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3382 if (Idx != -1)
3383 break;
3384 --II;
3385 ++Dist;
3386 }
3387
3388 assert(Idx != -1 && "Cannot find bundled definition!");
3389 DefIdx = Idx;
3390 return II;
3391}
3392
3393static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003394 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003395 unsigned &UseIdx, unsigned &Dist) {
3396 Dist = 0;
3397
3398 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3399 assert(II->isInsideBundle() && "Empty bundle?");
3400 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3401
3402 // FIXME: This doesn't properly handle multiple uses.
3403 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003404 while (II != E && II->isInsideBundle()) {
3405 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3406 if (Idx != -1)
3407 break;
3408 if (II->getOpcode() != ARM::t2IT)
3409 ++Dist;
3410 ++II;
3411 }
3412
Evan Chengda103bf2011-12-14 20:00:08 +00003413 if (Idx == -1) {
3414 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003415 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003416 }
3417
Evan Cheng7fae11b2011-12-14 02:11:42 +00003418 UseIdx = Idx;
3419 return II;
3420}
3421
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003422/// Return the number of cycles to add to (or subtract from) the static
3423/// itinerary based on the def opcode and alignment. The caller will ensure that
3424/// adjusted latency is at least one cycle.
3425static int adjustDefLatency(const ARMSubtarget &Subtarget,
3426 const MachineInstr *DefMI,
3427 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3428 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003429 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003430 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3431 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003432 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003433 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003434 case ARM::LDRrs:
3435 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003436 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3437 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3438 if (ShImm == 0 ||
3439 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003440 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003441 break;
3442 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003443 case ARM::t2LDRs:
3444 case ARM::t2LDRBs:
3445 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003446 case ARM::t2LDRSHs: {
3447 // Thumb2 mode: lsl only.
3448 unsigned ShAmt = DefMI->getOperand(3).getImm();
3449 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003450 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003451 break;
3452 }
3453 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003454 } else if (Subtarget.isSwift()) {
3455 // FIXME: Properly handle all of the latency adjustments for address
3456 // writeback.
3457 switch (DefMCID->getOpcode()) {
3458 default: break;
3459 case ARM::LDRrs:
3460 case ARM::LDRBrs: {
3461 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3462 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3463 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3464 if (!isSub &&
3465 (ShImm == 0 ||
3466 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3467 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3468 Adjust -= 2;
3469 else if (!isSub &&
3470 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3471 --Adjust;
3472 break;
3473 }
3474 case ARM::t2LDRs:
3475 case ARM::t2LDRBs:
3476 case ARM::t2LDRHs:
3477 case ARM::t2LDRSHs: {
3478 // Thumb2 mode: lsl only.
3479 unsigned ShAmt = DefMI->getOperand(3).getImm();
3480 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3481 Adjust -= 2;
3482 break;
3483 }
3484 }
Evan Chengff310732010-10-28 06:47:08 +00003485 }
3486
Silviu Barangab47bb942012-09-13 15:05:10 +00003487 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003488 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003489 default: break;
3490 case ARM::VLD1q8:
3491 case ARM::VLD1q16:
3492 case ARM::VLD1q32:
3493 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003494 case ARM::VLD1q8wb_fixed:
3495 case ARM::VLD1q16wb_fixed:
3496 case ARM::VLD1q32wb_fixed:
3497 case ARM::VLD1q64wb_fixed:
3498 case ARM::VLD1q8wb_register:
3499 case ARM::VLD1q16wb_register:
3500 case ARM::VLD1q32wb_register:
3501 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003502 case ARM::VLD2d8:
3503 case ARM::VLD2d16:
3504 case ARM::VLD2d32:
3505 case ARM::VLD2q8:
3506 case ARM::VLD2q16:
3507 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003508 case ARM::VLD2d8wb_fixed:
3509 case ARM::VLD2d16wb_fixed:
3510 case ARM::VLD2d32wb_fixed:
3511 case ARM::VLD2q8wb_fixed:
3512 case ARM::VLD2q16wb_fixed:
3513 case ARM::VLD2q32wb_fixed:
3514 case ARM::VLD2d8wb_register:
3515 case ARM::VLD2d16wb_register:
3516 case ARM::VLD2d32wb_register:
3517 case ARM::VLD2q8wb_register:
3518 case ARM::VLD2q16wb_register:
3519 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003520 case ARM::VLD3d8:
3521 case ARM::VLD3d16:
3522 case ARM::VLD3d32:
3523 case ARM::VLD1d64T:
3524 case ARM::VLD3d8_UPD:
3525 case ARM::VLD3d16_UPD:
3526 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003527 case ARM::VLD1d64Twb_fixed:
3528 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003529 case ARM::VLD3q8_UPD:
3530 case ARM::VLD3q16_UPD:
3531 case ARM::VLD3q32_UPD:
3532 case ARM::VLD4d8:
3533 case ARM::VLD4d16:
3534 case ARM::VLD4d32:
3535 case ARM::VLD1d64Q:
3536 case ARM::VLD4d8_UPD:
3537 case ARM::VLD4d16_UPD:
3538 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003539 case ARM::VLD1d64Qwb_fixed:
3540 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003541 case ARM::VLD4q8_UPD:
3542 case ARM::VLD4q16_UPD:
3543 case ARM::VLD4q32_UPD:
3544 case ARM::VLD1DUPq8:
3545 case ARM::VLD1DUPq16:
3546 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003547 case ARM::VLD1DUPq8wb_fixed:
3548 case ARM::VLD1DUPq16wb_fixed:
3549 case ARM::VLD1DUPq32wb_fixed:
3550 case ARM::VLD1DUPq8wb_register:
3551 case ARM::VLD1DUPq16wb_register:
3552 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003553 case ARM::VLD2DUPd8:
3554 case ARM::VLD2DUPd16:
3555 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003556 case ARM::VLD2DUPd8wb_fixed:
3557 case ARM::VLD2DUPd16wb_fixed:
3558 case ARM::VLD2DUPd32wb_fixed:
3559 case ARM::VLD2DUPd8wb_register:
3560 case ARM::VLD2DUPd16wb_register:
3561 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003562 case ARM::VLD4DUPd8:
3563 case ARM::VLD4DUPd16:
3564 case ARM::VLD4DUPd32:
3565 case ARM::VLD4DUPd8_UPD:
3566 case ARM::VLD4DUPd16_UPD:
3567 case ARM::VLD4DUPd32_UPD:
3568 case ARM::VLD1LNd8:
3569 case ARM::VLD1LNd16:
3570 case ARM::VLD1LNd32:
3571 case ARM::VLD1LNd8_UPD:
3572 case ARM::VLD1LNd16_UPD:
3573 case ARM::VLD1LNd32_UPD:
3574 case ARM::VLD2LNd8:
3575 case ARM::VLD2LNd16:
3576 case ARM::VLD2LNd32:
3577 case ARM::VLD2LNq16:
3578 case ARM::VLD2LNq32:
3579 case ARM::VLD2LNd8_UPD:
3580 case ARM::VLD2LNd16_UPD:
3581 case ARM::VLD2LNd32_UPD:
3582 case ARM::VLD2LNq16_UPD:
3583 case ARM::VLD2LNq32_UPD:
3584 case ARM::VLD4LNd8:
3585 case ARM::VLD4LNd16:
3586 case ARM::VLD4LNd32:
3587 case ARM::VLD4LNq16:
3588 case ARM::VLD4LNq32:
3589 case ARM::VLD4LNd8_UPD:
3590 case ARM::VLD4LNd16_UPD:
3591 case ARM::VLD4LNd32_UPD:
3592 case ARM::VLD4LNq16_UPD:
3593 case ARM::VLD4LNq32_UPD:
3594 // If the address is not 64-bit aligned, the latencies of these
3595 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003596 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003597 break;
3598 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003599 }
3600 return Adjust;
3601}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003602
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003603
3604
3605int
3606ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3607 const MachineInstr *DefMI, unsigned DefIdx,
3608 const MachineInstr *UseMI,
3609 unsigned UseIdx) const {
3610 // No operand latency. The caller may fall back to getInstrLatency.
3611 if (!ItinData || ItinData->isEmpty())
3612 return -1;
3613
3614 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3615 unsigned Reg = DefMO.getReg();
3616 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3617 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3618
3619 unsigned DefAdj = 0;
3620 if (DefMI->isBundle()) {
3621 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3622 DefMCID = &DefMI->getDesc();
3623 }
3624 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3625 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3626 return 1;
3627 }
3628
3629 unsigned UseAdj = 0;
3630 if (UseMI->isBundle()) {
3631 unsigned NewUseIdx;
3632 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3633 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003634 if (!NewUseMI)
3635 return -1;
3636
3637 UseMI = NewUseMI;
3638 UseIdx = NewUseIdx;
3639 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003640 }
3641
3642 if (Reg == ARM::CPSR) {
3643 if (DefMI->getOpcode() == ARM::FMSTAT) {
3644 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003645 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003646 }
3647
3648 // CPSR set and branch can be paired in the same cycle.
3649 if (UseMI->isBranch())
3650 return 0;
3651
3652 // Otherwise it takes the instruction latency (generally one).
3653 unsigned Latency = getInstrLatency(ItinData, DefMI);
3654
3655 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3656 // its uses. Instructions which are otherwise scheduled between them may
3657 // incur a code size penalty (not able to use the CPSR setting 16-bit
3658 // instructions).
3659 if (Latency > 0 && Subtarget.isThumb2()) {
3660 const MachineFunction *MF = DefMI->getParent()->getParent();
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003661 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003662 --Latency;
3663 }
3664 return Latency;
3665 }
3666
Andrew Trick77d0b882012-06-22 02:50:33 +00003667 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3668 return -1;
3669
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003670 unsigned DefAlign = DefMI->hasOneMemOperand()
3671 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3672 unsigned UseAlign = UseMI->hasOneMemOperand()
3673 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3674
3675 // Get the itinerary's latency if possible, and handle variable_ops.
3676 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3677 *UseMCID, UseIdx, UseAlign);
3678 // Unable to find operand latency. The caller may resort to getInstrLatency.
3679 if (Latency < 0)
3680 return Latency;
3681
3682 // Adjust for IT block position.
3683 int Adj = DefAdj + UseAdj;
3684
3685 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3686 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3687 if (Adj >= 0 || (int)Latency > -Adj) {
3688 return Latency + Adj;
3689 }
3690 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003691 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003692}
3693
3694int
3695ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3696 SDNode *DefNode, unsigned DefIdx,
3697 SDNode *UseNode, unsigned UseIdx) const {
3698 if (!DefNode->isMachineOpcode())
3699 return 1;
3700
Evan Cheng6cc775f2011-06-28 19:10:37 +00003701 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003702
Evan Cheng6cc775f2011-06-28 19:10:37 +00003703 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003704 return 0;
3705
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003706 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003707 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003708
Evan Cheng6c1414f2010-10-29 18:09:28 +00003709 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003710 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003711 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003712 return Latency <= 2 ? 1 : Latency - 1;
3713 else
3714 return Latency <= 3 ? 1 : Latency - 2;
3715 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003716
Evan Cheng6cc775f2011-06-28 19:10:37 +00003717 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003718 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3719 unsigned DefAlign = !DefMN->memoperands_empty()
3720 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3721 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3722 unsigned UseAlign = !UseMN->memoperands_empty()
3723 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003724 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3725 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003726
3727 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003728 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3729 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003730 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3731 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003732 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003733 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003734 case ARM::LDRrs:
3735 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003736 unsigned ShOpVal =
3737 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3738 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3739 if (ShImm == 0 ||
3740 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3741 --Latency;
3742 break;
3743 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003744 case ARM::t2LDRs:
3745 case ARM::t2LDRBs:
3746 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003747 case ARM::t2LDRSHs: {
3748 // Thumb2 mode: lsl only.
3749 unsigned ShAmt =
3750 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3751 if (ShAmt == 0 || ShAmt == 2)
3752 --Latency;
3753 break;
3754 }
3755 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003756 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3757 // FIXME: Properly handle all of the latency adjustments for address
3758 // writeback.
3759 switch (DefMCID.getOpcode()) {
3760 default: break;
3761 case ARM::LDRrs:
3762 case ARM::LDRBrs: {
3763 unsigned ShOpVal =
3764 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3765 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3766 if (ShImm == 0 ||
3767 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3768 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3769 Latency -= 2;
3770 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3771 --Latency;
3772 break;
3773 }
3774 case ARM::t2LDRs:
3775 case ARM::t2LDRBs:
3776 case ARM::t2LDRHs:
3777 case ARM::t2LDRSHs: {
3778 // Thumb2 mode: lsl 0-3 only.
3779 Latency -= 2;
3780 break;
3781 }
3782 }
Evan Chengff310732010-10-28 06:47:08 +00003783 }
3784
Silviu Barangab47bb942012-09-13 15:05:10 +00003785 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003786 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003787 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003788 case ARM::VLD1q8:
3789 case ARM::VLD1q16:
3790 case ARM::VLD1q32:
3791 case ARM::VLD1q64:
3792 case ARM::VLD1q8wb_register:
3793 case ARM::VLD1q16wb_register:
3794 case ARM::VLD1q32wb_register:
3795 case ARM::VLD1q64wb_register:
3796 case ARM::VLD1q8wb_fixed:
3797 case ARM::VLD1q16wb_fixed:
3798 case ARM::VLD1q32wb_fixed:
3799 case ARM::VLD1q64wb_fixed:
3800 case ARM::VLD2d8:
3801 case ARM::VLD2d16:
3802 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003803 case ARM::VLD2q8Pseudo:
3804 case ARM::VLD2q16Pseudo:
3805 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003806 case ARM::VLD2d8wb_fixed:
3807 case ARM::VLD2d16wb_fixed:
3808 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003809 case ARM::VLD2q8PseudoWB_fixed:
3810 case ARM::VLD2q16PseudoWB_fixed:
3811 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003812 case ARM::VLD2d8wb_register:
3813 case ARM::VLD2d16wb_register:
3814 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003815 case ARM::VLD2q8PseudoWB_register:
3816 case ARM::VLD2q16PseudoWB_register:
3817 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003818 case ARM::VLD3d8Pseudo:
3819 case ARM::VLD3d16Pseudo:
3820 case ARM::VLD3d32Pseudo:
3821 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003822 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003823 case ARM::VLD3d8Pseudo_UPD:
3824 case ARM::VLD3d16Pseudo_UPD:
3825 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003826 case ARM::VLD3q8Pseudo_UPD:
3827 case ARM::VLD3q16Pseudo_UPD:
3828 case ARM::VLD3q32Pseudo_UPD:
3829 case ARM::VLD3q8oddPseudo:
3830 case ARM::VLD3q16oddPseudo:
3831 case ARM::VLD3q32oddPseudo:
3832 case ARM::VLD3q8oddPseudo_UPD:
3833 case ARM::VLD3q16oddPseudo_UPD:
3834 case ARM::VLD3q32oddPseudo_UPD:
3835 case ARM::VLD4d8Pseudo:
3836 case ARM::VLD4d16Pseudo:
3837 case ARM::VLD4d32Pseudo:
3838 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003839 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003840 case ARM::VLD4d8Pseudo_UPD:
3841 case ARM::VLD4d16Pseudo_UPD:
3842 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003843 case ARM::VLD4q8Pseudo_UPD:
3844 case ARM::VLD4q16Pseudo_UPD:
3845 case ARM::VLD4q32Pseudo_UPD:
3846 case ARM::VLD4q8oddPseudo:
3847 case ARM::VLD4q16oddPseudo:
3848 case ARM::VLD4q32oddPseudo:
3849 case ARM::VLD4q8oddPseudo_UPD:
3850 case ARM::VLD4q16oddPseudo_UPD:
3851 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003852 case ARM::VLD1DUPq8:
3853 case ARM::VLD1DUPq16:
3854 case ARM::VLD1DUPq32:
3855 case ARM::VLD1DUPq8wb_fixed:
3856 case ARM::VLD1DUPq16wb_fixed:
3857 case ARM::VLD1DUPq32wb_fixed:
3858 case ARM::VLD1DUPq8wb_register:
3859 case ARM::VLD1DUPq16wb_register:
3860 case ARM::VLD1DUPq32wb_register:
3861 case ARM::VLD2DUPd8:
3862 case ARM::VLD2DUPd16:
3863 case ARM::VLD2DUPd32:
3864 case ARM::VLD2DUPd8wb_fixed:
3865 case ARM::VLD2DUPd16wb_fixed:
3866 case ARM::VLD2DUPd32wb_fixed:
3867 case ARM::VLD2DUPd8wb_register:
3868 case ARM::VLD2DUPd16wb_register:
3869 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003870 case ARM::VLD4DUPd8Pseudo:
3871 case ARM::VLD4DUPd16Pseudo:
3872 case ARM::VLD4DUPd32Pseudo:
3873 case ARM::VLD4DUPd8Pseudo_UPD:
3874 case ARM::VLD4DUPd16Pseudo_UPD:
3875 case ARM::VLD4DUPd32Pseudo_UPD:
3876 case ARM::VLD1LNq8Pseudo:
3877 case ARM::VLD1LNq16Pseudo:
3878 case ARM::VLD1LNq32Pseudo:
3879 case ARM::VLD1LNq8Pseudo_UPD:
3880 case ARM::VLD1LNq16Pseudo_UPD:
3881 case ARM::VLD1LNq32Pseudo_UPD:
3882 case ARM::VLD2LNd8Pseudo:
3883 case ARM::VLD2LNd16Pseudo:
3884 case ARM::VLD2LNd32Pseudo:
3885 case ARM::VLD2LNq16Pseudo:
3886 case ARM::VLD2LNq32Pseudo:
3887 case ARM::VLD2LNd8Pseudo_UPD:
3888 case ARM::VLD2LNd16Pseudo_UPD:
3889 case ARM::VLD2LNd32Pseudo_UPD:
3890 case ARM::VLD2LNq16Pseudo_UPD:
3891 case ARM::VLD2LNq32Pseudo_UPD:
3892 case ARM::VLD4LNd8Pseudo:
3893 case ARM::VLD4LNd16Pseudo:
3894 case ARM::VLD4LNd32Pseudo:
3895 case ARM::VLD4LNq16Pseudo:
3896 case ARM::VLD4LNq32Pseudo:
3897 case ARM::VLD4LNd8Pseudo_UPD:
3898 case ARM::VLD4LNd16Pseudo_UPD:
3899 case ARM::VLD4LNd32Pseudo_UPD:
3900 case ARM::VLD4LNq16Pseudo_UPD:
3901 case ARM::VLD4LNq32Pseudo_UPD:
3902 // If the address is not 64-bit aligned, the latencies of these
3903 // instructions increases by one.
3904 ++Latency;
3905 break;
3906 }
3907
Evan Chengff310732010-10-28 06:47:08 +00003908 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003909}
Evan Cheng63c76082010-10-19 18:58:51 +00003910
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003911unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3912 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3913 MI->isRegSequence() || MI->isImplicitDef())
3914 return 0;
3915
3916 if (MI->isBundle())
3917 return 0;
3918
3919 const MCInstrDesc &MCID = MI->getDesc();
3920
3921 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3922 // When predicated, CPSR is an additional source operand for CPSR updating
3923 // instructions, this apparently increases their latencies.
3924 return 1;
3925 }
3926 return 0;
3927}
3928
Andrew Trick45446062012-06-05 21:11:27 +00003929unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3930 const MachineInstr *MI,
3931 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003932 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3933 MI->isRegSequence() || MI->isImplicitDef())
3934 return 1;
3935
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003936 // An instruction scheduler typically runs on unbundled instructions, however
3937 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003938 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003939 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003940 MachineBasicBlock::const_instr_iterator I = MI;
3941 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3942 while (++I != E && I->isInsideBundle()) {
3943 if (I->getOpcode() != ARM::t2IT)
3944 Latency += getInstrLatency(ItinData, I, PredCost);
3945 }
3946 return Latency;
3947 }
3948
Evan Cheng6cc775f2011-06-28 19:10:37 +00003949 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003950 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003951 // When predicated, CPSR is an additional source operand for CPSR updating
3952 // instructions, this apparently increases their latencies.
3953 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003954 }
3955 // Be sure to call getStageLatency for an empty itinerary in case it has a
3956 // valid MinLatency property.
3957 if (!ItinData)
3958 return MI->mayLoad() ? 3 : 1;
3959
3960 unsigned Class = MCID.getSchedClass();
3961
3962 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003963 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003964 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003965
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003966 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003967 unsigned Latency = ItinData->getStageLatency(Class);
3968
3969 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3970 unsigned DefAlign = MI->hasOneMemOperand()
3971 ? (*MI->memoperands_begin())->getAlignment() : 0;
3972 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3973 if (Adj >= 0 || (int)Latency > -Adj) {
3974 return Latency + Adj;
3975 }
3976 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003977}
3978
3979int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3980 SDNode *Node) const {
3981 if (!Node->isMachineOpcode())
3982 return 1;
3983
3984 if (!ItinData || ItinData->isEmpty())
3985 return 1;
3986
3987 unsigned Opcode = Node->getMachineOpcode();
3988 switch (Opcode) {
3989 default:
3990 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003991 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003992 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003993 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003994 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003995}
3996
Evan Cheng63c76082010-10-19 18:58:51 +00003997bool ARMBaseInstrInfo::
3998hasHighOperandLatency(const InstrItineraryData *ItinData,
3999 const MachineRegisterInfo *MRI,
4000 const MachineInstr *DefMI, unsigned DefIdx,
4001 const MachineInstr *UseMI, unsigned UseIdx) const {
4002 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4003 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4004 if (Subtarget.isCortexA8() &&
4005 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4006 // CortexA8 VFP instructions are not pipelined.
4007 return true;
4008
4009 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00004010 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00004011 if (Latency < 0)
4012 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00004013 if (Latency <= 3)
4014 return false;
4015 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4016 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4017}
Evan Chenge96b8d72010-10-26 02:08:50 +00004018
4019bool ARMBaseInstrInfo::
4020hasLowDefLatency(const InstrItineraryData *ItinData,
4021 const MachineInstr *DefMI, unsigned DefIdx) const {
4022 if (!ItinData || ItinData->isEmpty())
4023 return false;
4024
4025 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4026 if (DDomain == ARMII::DomainGeneral) {
4027 unsigned DefClass = DefMI->getDesc().getSchedClass();
4028 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4029 return (DefCycle != -1 && DefCycle <= 2);
4030 }
4031 return false;
4032}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004033
Andrew Trick924123a2011-09-21 02:20:46 +00004034bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4035 StringRef &ErrInfo) const {
4036 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4037 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4038 return false;
4039 }
4040 return true;
4041}
4042
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004043// LoadStackGuard has so far only been implemented for MachO. Different code
4044// sequence is needed for other targets.
4045void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4046 unsigned LoadImmOpc,
4047 unsigned LoadOpc,
4048 Reloc::Model RM) const {
4049 MachineBasicBlock &MBB = *MI->getParent();
4050 DebugLoc DL = MI->getDebugLoc();
4051 unsigned Reg = MI->getOperand(0).getReg();
4052 const GlobalValue *GV =
4053 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4054 MachineInstrBuilder MIB;
4055
4056 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4057 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4058
4059 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4060 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4061 MIB.addReg(Reg, RegState::Kill).addImm(0);
4062 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
4063 MachineMemOperand *MMO = MBB.getParent()->
4064 getMachineMemOperand(MachinePointerInfo::getGOT(), Flag, 4, 4);
4065 MIB.addMemOperand(MMO);
4066 AddDefaultPred(MIB);
4067 }
4068
4069 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4070 MIB.addReg(Reg, RegState::Kill).addImm(0);
4071 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4072 AddDefaultPred(MIB);
4073}
4074
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004075bool
4076ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4077 unsigned &AddSubOpc,
4078 bool &NegAcc, bool &HasLane) const {
4079 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4080 if (I == MLxEntryMap.end())
4081 return false;
4082
4083 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4084 MulOpc = Entry.MulOpc;
4085 AddSubOpc = Entry.AddSubOpc;
4086 NegAcc = Entry.NegAcc;
4087 HasLane = Entry.HasLane;
4088 return true;
4089}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004090
4091//===----------------------------------------------------------------------===//
4092// Execution domains.
4093//===----------------------------------------------------------------------===//
4094//
4095// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4096// and some can go down both. The vmov instructions go down the VFP pipeline,
4097// but they can be changed to vorr equivalents that are executed by the NEON
4098// pipeline.
4099//
4100// We use the following execution domain numbering:
4101//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004102enum ARMExeDomain {
4103 ExeGeneric = 0,
4104 ExeVFP = 1,
4105 ExeNEON = 2
4106};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004107//
4108// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4109//
4110std::pair<uint16_t, uint16_t>
4111ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004112 // If we don't have access to NEON instructions then we won't be able
4113 // to swizzle anything to the NEON domain. Check to make sure.
4114 if (Subtarget.hasNEON()) {
4115 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4116 // if they are not predicated.
4117 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4118 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004119
Eric Christopher7e70aba2015-03-07 00:12:22 +00004120 // CortexA9 is particularly picky about mixing the two and wants these
4121 // converted.
4122 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4123 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4124 MI->getOpcode() == ARM::VMOVS))
4125 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4126 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004127 // No other instructions can be swizzled, so just determine their domain.
4128 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4129
4130 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004131 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004132
4133 // Certain instructions can go either way on Cortex-A8.
4134 // Treat them as NEON instructions.
4135 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004136 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004137
4138 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004139 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004140
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004141 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004142}
4143
Tim Northover771f1602012-08-29 16:36:07 +00004144static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4145 unsigned SReg, unsigned &Lane) {
4146 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4147 Lane = 0;
4148
4149 if (DReg != ARM::NoRegister)
4150 return DReg;
4151
4152 Lane = 1;
4153 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4154
4155 assert(DReg && "S-register with no D super-register?");
4156 return DReg;
4157}
4158
Andrew Trickd9296ec2012-10-10 05:43:01 +00004159/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004160/// set ImplicitSReg to a register number that must be marked as implicit-use or
4161/// zero if no register needs to be defined as implicit-use.
4162///
4163/// If the function cannot determine if an SPR should be marked implicit use or
4164/// not, it returns false.
4165///
4166/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004167/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004168/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4169/// lane of the DPR).
4170///
4171/// If the other SPR is defined, an implicit-use of it should be added. Else,
4172/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004173///
James Molloyea052562012-09-18 08:31:15 +00004174static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4175 MachineInstr *MI,
4176 unsigned DReg, unsigned Lane,
4177 unsigned &ImplicitSReg) {
4178 // If the DPR is defined or used already, the other SPR lane will be chained
4179 // correctly, so there is nothing to be done.
4180 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4181 ImplicitSReg = 0;
4182 return true;
4183 }
4184
4185 // Otherwise we need to go searching to see if the SPR is set explicitly.
4186 ImplicitSReg = TRI->getSubReg(DReg,
4187 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4188 MachineBasicBlock::LivenessQueryResult LQR =
4189 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4190
4191 if (LQR == MachineBasicBlock::LQR_Live)
4192 return true;
4193 else if (LQR == MachineBasicBlock::LQR_Unknown)
4194 return false;
4195
4196 // If the register is known not to be live, there is no need to add an
4197 // implicit-use.
4198 ImplicitSReg = 0;
4199 return true;
4200}
Tim Northover771f1602012-08-29 16:36:07 +00004201
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004202void
4203ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004204 unsigned DstReg, SrcReg, DReg;
4205 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004206 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004207 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004208 switch (MI->getOpcode()) {
4209 default:
4210 llvm_unreachable("cannot handle opcode!");
4211 break;
4212 case ARM::VMOVD:
4213 if (Domain != ExeNEON)
4214 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004215
Tim Northoverf6618152012-08-17 11:32:52 +00004216 // Zap the predicate operands.
4217 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004218
Eric Christopher7e70aba2015-03-07 00:12:22 +00004219 // Make sure we've got NEON instructions.
4220 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4221
Tim Northover771f1602012-08-29 16:36:07 +00004222 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4223 DstReg = MI->getOperand(0).getReg();
4224 SrcReg = MI->getOperand(1).getReg();
4225
4226 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4227 MI->RemoveOperand(i-1);
4228
4229 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004230 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004231 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4232 .addReg(SrcReg)
4233 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004234 break;
4235 case ARM::VMOVRS:
4236 if (Domain != ExeNEON)
4237 break;
4238 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4239
Tim Northover771f1602012-08-29 16:36:07 +00004240 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004241 DstReg = MI->getOperand(0).getReg();
4242 SrcReg = MI->getOperand(1).getReg();
4243
Tim Northover771f1602012-08-29 16:36:07 +00004244 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4245 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004246
Tim Northover771f1602012-08-29 16:36:07 +00004247 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004248
Tim Northover771f1602012-08-29 16:36:07 +00004249 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4250 // Note that DSrc has been widened and the other lane may be undef, which
4251 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004252 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004253 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4254 .addReg(DReg, RegState::Undef)
4255 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004256
Tim Northover771f1602012-08-29 16:36:07 +00004257 // The old source should be an implicit use, otherwise we might think it
4258 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004259 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004260 break;
James Molloyea052562012-09-18 08:31:15 +00004261 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004262 if (Domain != ExeNEON)
4263 break;
4264 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4265
Tim Northover771f1602012-08-29 16:36:07 +00004266 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004267 DstReg = MI->getOperand(0).getReg();
4268 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004269
Tim Northover771f1602012-08-29 16:36:07 +00004270 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4271
James Molloyea052562012-09-18 08:31:15 +00004272 unsigned ImplicitSReg;
4273 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4274 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004275
Tim Northoverc8d867d2012-09-05 18:37:53 +00004276 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4277 MI->RemoveOperand(i-1);
4278
Tim Northover771f1602012-08-29 16:36:07 +00004279 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4280 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004281 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004282 MIB.addReg(DReg, RegState::Define)
4283 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4284 .addReg(SrcReg)
4285 .addImm(Lane);
4286 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004287
Tim Northover726d32c2012-09-01 18:07:29 +00004288 // The narrower destination must be marked as set to keep previous chains
4289 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004290 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004291 if (ImplicitSReg != 0)
4292 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004293 break;
James Molloyea052562012-09-18 08:31:15 +00004294 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004295 case ARM::VMOVS: {
4296 if (Domain != ExeNEON)
4297 break;
4298
4299 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4300 DstReg = MI->getOperand(0).getReg();
4301 SrcReg = MI->getOperand(1).getReg();
4302
Tim Northoverca9f3842012-08-30 10:17:45 +00004303 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4304 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4305 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4306
James Molloyea052562012-09-18 08:31:15 +00004307 unsigned ImplicitSReg;
4308 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4309 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004310
Tim Northoverc8d867d2012-09-05 18:37:53 +00004311 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4312 MI->RemoveOperand(i-1);
4313
Tim Northoverca9f3842012-08-30 10:17:45 +00004314 if (DSrc == DDst) {
4315 // Destination can be:
4316 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4317 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004318 MIB.addReg(DDst, RegState::Define)
4319 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4320 .addImm(SrcLane);
4321 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004322
4323 // Neither the source or the destination are naturally represented any
4324 // more, so add them in manually.
4325 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4326 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004327 if (ImplicitSReg != 0)
4328 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004329 break;
4330 }
4331
4332 // In general there's no single instruction that can perform an S <-> S
4333 // move in NEON space, but a pair of VEXT instructions *can* do the
4334 // job. It turns out that the VEXTs needed will only use DSrc once, with
4335 // the position based purely on the combination of lane-0 and lane-1
4336 // involved. For example
4337 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4338 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4339 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4340 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4341 //
4342 // Pattern of the MachineInstrs is:
4343 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4344 MachineInstrBuilder NewMIB;
4345 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4346 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004347
4348 // On the first instruction, both DSrc and DDst may be <undef> if present.
4349 // Specifically when the original instruction didn't have them as an
4350 // <imp-use>.
4351 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4352 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4353 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4354
4355 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4356 CurUndef = !MI->readsRegister(CurReg, TRI);
4357 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4358
Tim Northoverca9f3842012-08-30 10:17:45 +00004359 NewMIB.addImm(1);
4360 AddDefaultPred(NewMIB);
4361
4362 if (SrcLane == DstLane)
4363 NewMIB.addReg(SrcReg, RegState::Implicit);
4364
4365 MI->setDesc(get(ARM::VEXTd32));
4366 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004367
4368 // On the second instruction, DDst has definitely been defined above, so
4369 // it is not <undef>. DSrc, if present, can be <undef> as above.
4370 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4371 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4372 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4373
4374 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4375 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4376 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4377
Tim Northoverca9f3842012-08-30 10:17:45 +00004378 MIB.addImm(1);
4379 AddDefaultPred(MIB);
4380
4381 if (SrcLane != DstLane)
4382 MIB.addReg(SrcReg, RegState::Implicit);
4383
4384 // As before, the original destination is no longer represented, add it
4385 // implicitly.
4386 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004387 if (ImplicitSReg != 0)
4388 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004389 break;
4390 }
Tim Northoverf6618152012-08-17 11:32:52 +00004391 }
4392
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004393}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004394
Bob Wilsone8a549c2012-09-29 21:43:49 +00004395//===----------------------------------------------------------------------===//
4396// Partial register updates
4397//===----------------------------------------------------------------------===//
4398//
4399// Swift renames NEON registers with 64-bit granularity. That means any
4400// instruction writing an S-reg implicitly reads the containing D-reg. The
4401// problem is mostly avoided by translating f32 operations to v2f32 operations
4402// on D-registers, but f32 loads are still a problem.
4403//
4404// These instructions can load an f32 into a NEON register:
4405//
4406// VLDRS - Only writes S, partial D update.
4407// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4408// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4409//
4410// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004411unsigned ARMBaseInstrInfo::
4412getPartialRegUpdateClearance(const MachineInstr *MI,
4413 unsigned OpNum,
4414 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004415 if (!SwiftPartialUpdateClearance ||
4416 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004417 return 0;
4418
4419 assert(TRI && "Need TRI instance");
4420
4421 const MachineOperand &MO = MI->getOperand(OpNum);
4422 if (MO.readsReg())
4423 return 0;
4424 unsigned Reg = MO.getReg();
4425 int UseOp = -1;
4426
4427 switch(MI->getOpcode()) {
4428 // Normal instructions writing only an S-register.
4429 case ARM::VLDRS:
4430 case ARM::FCONSTS:
4431 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004432 case ARM::VMOVv8i8:
4433 case ARM::VMOVv4i16:
4434 case ARM::VMOVv2i32:
4435 case ARM::VMOVv2f32:
4436 case ARM::VMOVv1i64:
4437 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4438 break;
4439
4440 // Explicitly reads the dependency.
4441 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004442 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004443 break;
4444 default:
4445 return 0;
4446 }
4447
4448 // If this instruction actually reads a value from Reg, there is no unwanted
4449 // dependency.
4450 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4451 return 0;
4452
4453 // We must be able to clobber the whole D-reg.
4454 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4455 // Virtual register must be a foo:ssub_0<def,undef> operand.
4456 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4457 return 0;
4458 } else if (ARM::SPRRegClass.contains(Reg)) {
4459 // Physical register: MI must define the full D-reg.
4460 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4461 &ARM::DPRRegClass);
4462 if (!DReg || !MI->definesRegister(DReg, TRI))
4463 return 0;
4464 }
4465
4466 // MI has an unwanted D-register dependency.
4467 // Avoid defs in the previous N instructrions.
4468 return SwiftPartialUpdateClearance;
4469}
4470
4471// Break a partial register dependency after getPartialRegUpdateClearance
4472// returned non-zero.
4473void ARMBaseInstrInfo::
4474breakPartialRegDependency(MachineBasicBlock::iterator MI,
4475 unsigned OpNum,
4476 const TargetRegisterInfo *TRI) const {
4477 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4478 assert(TRI && "Need TRI instance");
4479
4480 const MachineOperand &MO = MI->getOperand(OpNum);
4481 unsigned Reg = MO.getReg();
4482 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4483 "Can't break virtual register dependencies.");
4484 unsigned DReg = Reg;
4485
4486 // If MI defines an S-reg, find the corresponding D super-register.
4487 if (ARM::SPRRegClass.contains(Reg)) {
4488 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4489 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4490 }
4491
4492 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4493 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4494
4495 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4496 // the full D-register by loading the same value to both lanes. The
4497 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004498 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004499 // too big regressions.
4500
4501 // Insert the dependency-breaking FCONSTD before MI.
4502 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4503 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4504 get(ARM::FCONSTD), DReg).addImm(96));
4505 MI->addRegisterKilled(DReg, TRI, true);
4506}
4507
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004508bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004509 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004510}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004511
4512bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004513 if (MI->getNumOperands() < 4)
4514 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004515 unsigned ShOpVal = MI->getOperand(3).getImm();
4516 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4517 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4518 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4519 ((ShImm == 1 || ShImm == 2) &&
4520 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4521 return true;
4522
4523 return false;
4524}
Quentin Colombetd358e842014-08-22 18:05:22 +00004525
4526bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4527 const MachineInstr &MI, unsigned DefIdx,
4528 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4529 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4530 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4531
4532 switch (MI.getOpcode()) {
4533 case ARM::VMOVDRR:
4534 // dX = VMOVDRR rY, rZ
4535 // is the same as:
4536 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4537 // Populate the InputRegs accordingly.
4538 // rY
4539 const MachineOperand *MOReg = &MI.getOperand(1);
4540 InputRegs.push_back(
4541 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4542 // rZ
4543 MOReg = &MI.getOperand(2);
4544 InputRegs.push_back(
4545 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4546 return true;
4547 }
4548 llvm_unreachable("Target dependent opcode missing");
4549}
4550
4551bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4552 const MachineInstr &MI, unsigned DefIdx,
4553 RegSubRegPairAndIdx &InputReg) const {
4554 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4555 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4556
4557 switch (MI.getOpcode()) {
4558 case ARM::VMOVRRD:
4559 // rX, rY = VMOVRRD dZ
4560 // is the same as:
4561 // rX = EXTRACT_SUBREG dZ, ssub_0
4562 // rY = EXTRACT_SUBREG dZ, ssub_1
4563 const MachineOperand &MOReg = MI.getOperand(2);
4564 InputReg.Reg = MOReg.getReg();
4565 InputReg.SubReg = MOReg.getSubReg();
4566 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4567 return true;
4568 }
4569 llvm_unreachable("Target dependent opcode missing");
4570}
4571
4572bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4573 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4574 RegSubRegPairAndIdx &InsertedReg) const {
4575 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4576 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4577
4578 switch (MI.getOpcode()) {
4579 case ARM::VSETLNi32:
4580 // dX = VSETLNi32 dY, rZ, imm
4581 const MachineOperand &MOBaseReg = MI.getOperand(1);
4582 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4583 const MachineOperand &MOIndex = MI.getOperand(3);
4584 BaseReg.Reg = MOBaseReg.getReg();
4585 BaseReg.SubReg = MOBaseReg.getSubReg();
4586
4587 InsertedReg.Reg = MOInsertedReg.getReg();
4588 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4589 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4590 return true;
4591 }
4592 llvm_unreachable("Target dependent opcode missing");
4593}