blob: 337531aaab04c259bedea93aea3fce9c5de81af8 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
David Goodwinaf7451b2009-07-08 16:09:28 +000014#include "ARM.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000015#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000018#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000020#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000023#include "llvm/CodeGen/LiveVariables.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000024#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000030#include "llvm/CodeGen/SelectionDAGNodes.h"
Matthias Braun88e21312015-06-13 03:42:11 +000031#include "llvm/CodeGen/TargetSchedule.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000036#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000037#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000038#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000041#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000042
David Goodwinaf7451b2009-07-08 16:09:28 +000043using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "arm-instrinfo"
46
Chandler Carruthd174b722014-04-22 02:03:14 +000047#define GET_INSTRINFO_CTOR_DTOR
48#include "ARMGenInstrInfo.inc"
49
David Goodwinaf7451b2009-07-08 16:09:28 +000050static cl::opt<bool>
51EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
52 cl::desc("Enable ARM 2-addr to 3-addr conv"));
53
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000054static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000055WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000056 cl::desc("Widen ARM vmovs to vmovd when possible"));
57
Bob Wilsone8a549c2012-09-29 21:43:49 +000058static cl::opt<unsigned>
59SwiftPartialUpdateClearance("swift-partial-update-clearance",
60 cl::Hidden, cl::init(12),
61 cl::desc("Clearance before partial register updates"));
62
Evan Cheng62c7b5b2010-12-05 22:04:16 +000063/// ARM_MLxEntry - Record information about MLA / MLS instructions.
64struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000065 uint16_t MLxOpc; // MLA / MLS opcode
66 uint16_t MulOpc; // Expanded multiplication opcode
67 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000068 bool NegAcc; // True if the acc is negated before the add / sub.
69 bool HasLane; // True if instruction has an extra "lane" operand.
70};
71
72static const ARM_MLxEntry ARM_MLxTable[] = {
73 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
74 // fp scalar ops
75 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
76 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
77 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
78 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000079 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
81 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
82 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
83
84 // fp SIMD ops
85 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
86 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
87 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
88 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
89 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
90 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
91 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
92 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
93};
94
Anton Korobeynikov14635da2009-11-02 00:10:38 +000095ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000096 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000097 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000098 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
99 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
100 assert(false && "Duplicated entries?");
101 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
102 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
103 }
104}
105
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000106// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
107// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000108ScheduleHazardRecognizer *
109ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
110 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000111 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000112 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000113 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000114 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
115 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000116 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000117}
118
119ScheduleHazardRecognizer *ARMBaseInstrInfo::
120CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
121 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000122 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000123 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000124 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000125}
126
127MachineInstr *
128ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
129 MachineBasicBlock::iterator &MBBI,
130 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000131 // FIXME: Thumb2 support.
132
David Goodwinaf7451b2009-07-08 16:09:28 +0000133 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000134 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000135
136 MachineInstr *MI = MBBI;
137 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000138 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000139 bool isPre = false;
140 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000142 case ARMII::IndexModePre:
143 isPre = true;
144 break;
145 case ARMII::IndexModePost:
146 break;
147 }
148
149 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
150 // operation.
151 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
152 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000154
Craig Topper062a2ba2014-04-25 05:30:21 +0000155 MachineInstr *UpdateMI = nullptr;
156 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000157 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000158 const MCInstrDesc &MCID = MI->getDesc();
159 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000160 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000161 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
162 const MachineOperand &Base = MI->getOperand(2);
163 const MachineOperand &Offset = MI->getOperand(NumOps-3);
164 unsigned WBReg = WB.getReg();
165 unsigned BaseReg = Base.getReg();
166 unsigned OffReg = Offset.getReg();
167 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
168 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
169 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000170 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000171 case ARMII::AddrMode2: {
172 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
173 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
174 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000175 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 // Can't encode it in a so_imm operand. This transformation will
177 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000178 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000179 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000180 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000181 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000182 .addImm(Pred).addReg(0).addReg(0);
183 } else if (Amt != 0) {
184 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
185 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
186 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000187 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000188 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
189 .addImm(Pred).addReg(0).addReg(0);
190 } else
191 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000192 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000193 .addReg(BaseReg).addReg(OffReg)
194 .addImm(Pred).addReg(0).addReg(0);
195 break;
196 }
197 case ARMII::AddrMode3 : {
198 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
199 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
200 if (OffReg == 0)
201 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
202 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000203 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000204 .addReg(BaseReg).addImm(Amt)
205 .addImm(Pred).addReg(0).addReg(0);
206 else
207 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000208 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000209 .addReg(BaseReg).addReg(OffReg)
210 .addImm(Pred).addReg(0).addReg(0);
211 break;
212 }
213 }
214
215 std::vector<MachineInstr*> NewMIs;
216 if (isPre) {
217 if (isLoad)
218 MemMI = BuildMI(MF, MI->getDebugLoc(),
219 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000220 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000221 else
222 MemMI = BuildMI(MF, MI->getDebugLoc(),
223 get(MemOpc)).addReg(MI->getOperand(1).getReg())
224 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
225 NewMIs.push_back(MemMI);
226 NewMIs.push_back(UpdateMI);
227 } else {
228 if (isLoad)
229 MemMI = BuildMI(MF, MI->getDebugLoc(),
230 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000231 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000232 else
233 MemMI = BuildMI(MF, MI->getDebugLoc(),
234 get(MemOpc)).addReg(MI->getOperand(1).getReg())
235 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
236 if (WB.isDead())
237 UpdateMI->getOperand(0).setIsDead();
238 NewMIs.push_back(UpdateMI);
239 NewMIs.push_back(MemMI);
240 }
241
242 // Transfer LiveVariables states, kill / dead info.
243 if (LV) {
244 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
245 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000246 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000247 unsigned Reg = MO.getReg();
248
249 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
250 if (MO.isDef()) {
251 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
252 if (MO.isDead())
253 LV->addVirtualRegisterDead(Reg, NewMI);
254 }
255 if (MO.isUse() && MO.isKill()) {
256 for (unsigned j = 0; j < 2; ++j) {
257 // Look at the two new MI's in reverse order.
258 MachineInstr *NewMI = NewMIs[j];
259 if (!NewMI->readsRegister(Reg))
260 continue;
261 LV->addVirtualRegisterKilled(Reg, NewMI);
262 if (VI.removeKill(MI))
263 VI.Kills.push_back(NewMI);
264 break;
265 }
266 }
267 }
268 }
269 }
270
271 MFI->insert(MBBI, NewMIs[1]);
272 MFI->insert(MBBI, NewMIs[0]);
273 return NewMIs[0];
274}
275
276// Branch analysis.
277bool
278ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
279 MachineBasicBlock *&FBB,
280 SmallVectorImpl<MachineOperand> &Cond,
281 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000282 TBB = nullptr;
283 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000284
David Goodwinaf7451b2009-07-08 16:09:28 +0000285 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000286 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000287 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000288 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000289
290 // Walk backwards from the end of the basic block until the branch is
291 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000292 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000293
294 // Flag to be raised on unanalyzeable instructions. This is useful in cases
295 // where we want to clean up on the end of the basic block before we bail
296 // out.
297 bool CantAnalyze = false;
298
299 // Skip over DEBUG values and predicated nonterminators.
300 while (I->isDebugValue() || !I->isTerminator()) {
301 if (I == MBB.begin())
302 return false;
303 --I;
304 }
305
306 if (isIndirectBranchOpcode(I->getOpcode()) ||
307 isJumpTableBranchOpcode(I->getOpcode())) {
308 // Indirect branches and jump tables can't be analyzed, but we still want
309 // to clean up any instructions at the tail of the basic block.
310 CantAnalyze = true;
311 } else if (isUncondBranchOpcode(I->getOpcode())) {
312 TBB = I->getOperand(0).getMBB();
313 } else if (isCondBranchOpcode(I->getOpcode())) {
314 // Bail out if we encounter multiple conditional branches.
315 if (!Cond.empty())
316 return true;
317
318 assert(!FBB && "FBB should have been null.");
319 FBB = TBB;
320 TBB = I->getOperand(0).getMBB();
321 Cond.push_back(I->getOperand(1));
322 Cond.push_back(I->getOperand(2));
323 } else if (I->isReturn()) {
324 // Returns can't be analyzed, but we should run cleanup.
325 CantAnalyze = !isPredicated(I);
326 } else {
327 // We encountered other unrecognized terminator. Bail out immediately.
328 return true;
329 }
330
331 // Cleanup code - to be run for unpredicated unconditional branches and
332 // returns.
333 if (!isPredicated(I) &&
334 (isUncondBranchOpcode(I->getOpcode()) ||
335 isIndirectBranchOpcode(I->getOpcode()) ||
336 isJumpTableBranchOpcode(I->getOpcode()) ||
337 I->isReturn())) {
338 // Forget any previous condition branch information - it no longer applies.
339 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000340 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000341
342 // If we can modify the function, delete everything below this
343 // unconditional branch.
344 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000345 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000346 while (DI != MBB.end()) {
347 MachineInstr *InstToDelete = DI;
348 ++DI;
349 InstToDelete->eraseFromParent();
350 }
351 }
352 }
353
354 if (CantAnalyze)
355 return true;
356
Dale Johannesen4244d122010-04-02 01:38:09 +0000357 if (I == MBB.begin())
358 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000359
Dale Johannesen4244d122010-04-02 01:38:09 +0000360 --I;
361 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000362
Lang Hames24864fe2013-07-19 23:52:47 +0000363 // We made it past the terminators without bailing out - we must have
364 // analyzed this branch successfully.
365 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000366}
367
368
369unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000370 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
371 if (I == MBB.end())
372 return 0;
373
Evan Cheng056c6692009-07-27 18:20:05 +0000374 if (!isUncondBranchOpcode(I->getOpcode()) &&
375 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000376 return 0;
377
378 // Remove the branch.
379 I->eraseFromParent();
380
381 I = MBB.end();
382
383 if (I == MBB.begin()) return 1;
384 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000385 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000386 return 1;
387
388 // Remove the branch.
389 I->eraseFromParent();
390 return 2;
391}
392
393unsigned
394ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000395 MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000396 ArrayRef<MachineOperand> Cond,
Stuart Hastings0125b642010-06-17 22:43:56 +0000397 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000398 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
399 int BOpc = !AFI->isThumbFunction()
400 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
401 int BccOpc = !AFI->isThumbFunction()
402 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000403 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000404
David Goodwinaf7451b2009-07-08 16:09:28 +0000405 // Shouldn't be a fall through.
406 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
407 assert((Cond.size() == 2 || Cond.size() == 0) &&
408 "ARM branch conditions have two components!");
409
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000410 // For conditional branches, we use addOperand to preserve CPSR flags.
411
Craig Topper062a2ba2014-04-25 05:30:21 +0000412 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000413 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000414 if (isThumb)
415 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
416 else
417 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000418 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000419 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000420 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000421 return 1;
422 }
423
424 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000425 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000426 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000427 if (isThumb)
428 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
429 else
430 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000431 return 2;
432}
433
434bool ARMBaseInstrInfo::
435ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
436 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
437 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
438 return false;
439}
440
Evan Cheng7fae11b2011-12-14 02:11:42 +0000441bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
442 if (MI->isBundle()) {
443 MachineBasicBlock::const_instr_iterator I = MI;
444 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
445 while (++I != E && I->isInsideBundle()) {
446 int PIdx = I->findFirstPredOperandIdx();
447 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
448 return true;
449 }
450 return false;
451 }
452
453 int PIdx = MI->findFirstPredOperandIdx();
454 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
455}
456
David Goodwinaf7451b2009-07-08 16:09:28 +0000457bool ARMBaseInstrInfo::
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000458PredicateInstruction(MachineInstr *MI, ArrayRef<MachineOperand> Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000459 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000460 if (isUncondBranchOpcode(Opc)) {
461 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000462 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
463 .addImm(Pred[0].getImm())
464 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000465 return true;
466 }
467
468 int PIdx = MI->findFirstPredOperandIdx();
469 if (PIdx != -1) {
470 MachineOperand &PMO = MI->getOperand(PIdx);
471 PMO.setImm(Pred[0].getImm());
472 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
473 return true;
474 }
475 return false;
476}
477
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000478bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
479 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000480 if (Pred1.size() > 2 || Pred2.size() > 2)
481 return false;
482
483 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
484 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
485 if (CC1 == CC2)
486 return true;
487
488 switch (CC1) {
489 default:
490 return false;
491 case ARMCC::AL:
492 return true;
493 case ARMCC::HS:
494 return CC2 == ARMCC::HI;
495 case ARMCC::LS:
496 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
497 case ARMCC::GE:
498 return CC2 == ARMCC::GT;
499 case ARMCC::LE:
500 return CC2 == ARMCC::LT;
501 }
502}
503
504bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
505 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000506 bool Found = false;
507 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
508 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000509 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
510 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000511 Pred.push_back(MO);
512 Found = true;
513 }
514 }
515
516 return Found;
517}
518
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000519static bool isCPSRDefined(const MachineInstr *MI) {
520 for (const auto &MO : MI->operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000521 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000522 return true;
523 return false;
524}
525
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000526static bool isEligibleForITBlock(const MachineInstr *MI) {
527 switch (MI->getOpcode()) {
528 default: return true;
529 case ARM::tADC: // ADC (register) T1
530 case ARM::tADDi3: // ADD (immediate) T1
531 case ARM::tADDi8: // ADD (immediate) T2
532 case ARM::tADDrr: // ADD (register) T1
533 case ARM::tAND: // AND (register) T1
534 case ARM::tASRri: // ASR (immediate) T1
535 case ARM::tASRrr: // ASR (register) T1
536 case ARM::tBIC: // BIC (register) T1
537 case ARM::tEOR: // EOR (register) T1
538 case ARM::tLSLri: // LSL (immediate) T1
539 case ARM::tLSLrr: // LSL (register) T1
540 case ARM::tLSRri: // LSR (immediate) T1
541 case ARM::tLSRrr: // LSR (register) T1
542 case ARM::tMUL: // MUL T1
543 case ARM::tMVN: // MVN (register) T1
544 case ARM::tORR: // ORR (register) T1
545 case ARM::tROR: // ROR (register) T1
546 case ARM::tRSB: // RSB (immediate) T1
547 case ARM::tSBC: // SBC (register) T1
548 case ARM::tSUBi3: // SUB (immediate) T1
549 case ARM::tSUBi8: // SUB (immediate) T2
550 case ARM::tSUBrr: // SUB (register) T1
551 return !isCPSRDefined(MI);
552 }
553}
554
Evan Chenga33fc862009-11-21 06:21:52 +0000555/// isPredicable - Return true if the specified instruction can be predicated.
556/// By default, this returns true for every instruction with a
557/// PredicateOperand.
558bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000559 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000560 return false;
561
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000562 if (!isEligibleForITBlock(MI))
563 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000564
Joey Goulya5153cb2013-09-09 14:21:49 +0000565 ARMFunctionInfo *AFI =
566 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
567
568 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000569 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000570 return isV8EligibleForIT(MI);
571 } else { // non-Thumb
572 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
573 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000574 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000575
Evan Chenga33fc862009-11-21 06:21:52 +0000576 return true;
577}
David Goodwinaf7451b2009-07-08 16:09:28 +0000578
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000579namespace llvm {
580template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000581 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
582 const MachineOperand &MO = MI->getOperand(i);
583 if (!MO.isReg() || MO.isUndef() || MO.isUse())
584 continue;
585 if (MO.getReg() != ARM::CPSR)
586 continue;
587 if (!MO.isDead())
588 return false;
589 }
590 // all definitions of CPSR are dead
591 return true;
592}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000593}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000594
David Goodwinaf7451b2009-07-08 16:09:28 +0000595/// GetInstSize - Return the size of the specified MachineInstr.
596///
597unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
598 const MachineBasicBlock &MBB = *MI->getParent();
599 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000600 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000601
Evan Cheng6cc775f2011-06-28 19:10:37 +0000602 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000603 if (MCID.getSize())
604 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000605
David Blaikie46a9f012012-01-20 21:51:11 +0000606 // If this machine instr is an inline asm, measure it.
607 if (MI->getOpcode() == ARM::INLINEASM)
608 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000609 unsigned Opc = MI->getOpcode();
610 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000611 default:
612 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000613 return 0;
614 case TargetOpcode::BUNDLE:
615 return getInstBundleLength(MI);
616 case ARM::MOVi16_ga_pcrel:
617 case ARM::MOVTi16_ga_pcrel:
618 case ARM::t2MOVi16_ga_pcrel:
619 case ARM::t2MOVTi16_ga_pcrel:
620 return 4;
621 case ARM::MOVi32imm:
622 case ARM::t2MOVi32imm:
623 return 8;
624 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000625 case ARM::JUMPTABLE_INSTS:
626 case ARM::JUMPTABLE_ADDRS:
627 case ARM::JUMPTABLE_TBB:
628 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000629 // If this machine instr is a constant pool entry, its size is recorded as
630 // operand #2.
631 return MI->getOperand(2).getImm();
632 case ARM::Int_eh_sjlj_longjmp:
633 return 16;
634 case ARM::tInt_eh_sjlj_longjmp:
635 return 10;
636 case ARM::Int_eh_sjlj_setjmp:
637 case ARM::Int_eh_sjlj_setjmp_nofp:
638 return 20;
639 case ARM::tInt_eh_sjlj_setjmp:
640 case ARM::t2Int_eh_sjlj_setjmp:
641 case ARM::t2Int_eh_sjlj_setjmp_nofp:
642 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000643 case ARM::SPACE:
644 return MI->getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000645 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000646}
647
Evan Cheng7fae11b2011-12-14 02:11:42 +0000648unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
649 unsigned Size = 0;
650 MachineBasicBlock::const_instr_iterator I = MI;
651 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
652 while (++I != E && I->isInsideBundle()) {
653 assert(!I->isBundle() && "No nested bundle!");
654 Size += GetInstSizeInBytes(&*I);
655 }
656 return Size;
657}
658
Tim Northover5d72c5d2014-10-01 19:21:03 +0000659void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
660 MachineBasicBlock::iterator I,
661 unsigned DestReg, bool KillSrc,
662 const ARMSubtarget &Subtarget) const {
663 unsigned Opc = Subtarget.isThumb()
664 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
665 : ARM::MRS;
666
667 MachineInstrBuilder MIB =
668 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
669
670 // There is only 1 A/R class MRS instruction, and it always refers to
671 // APSR. However, there are lots of other possibilities on M-class cores.
672 if (Subtarget.isMClass())
673 MIB.addImm(0x800);
674
675 AddDefaultPred(MIB);
676
677 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
678}
679
680void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
681 MachineBasicBlock::iterator I,
682 unsigned SrcReg, bool KillSrc,
683 const ARMSubtarget &Subtarget) const {
684 unsigned Opc = Subtarget.isThumb()
685 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
686 : ARM::MSR;
687
688 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
689
690 if (Subtarget.isMClass())
691 MIB.addImm(0x800);
692 else
693 MIB.addImm(8);
694
695 MIB.addReg(SrcReg, getKillRegState(KillSrc));
696
697 AddDefaultPred(MIB);
698
699 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
700}
701
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000702void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
703 MachineBasicBlock::iterator I, DebugLoc DL,
704 unsigned DestReg, unsigned SrcReg,
705 bool KillSrc) const {
706 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000707 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000708
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000709 if (GPRDest && GPRSrc) {
710 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000711 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000712 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000713 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000714
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000715 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000716 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000717
Chad Rosierbe762512011-08-20 00:17:25 +0000718 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000719 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000720 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000721 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000722 Opc = ARM::VMOVRS;
723 else if (SPRDest && GPRSrc)
724 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000725 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000726 Opc = ARM::VMOVD;
727 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000728 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000729
Chad Rosierbe762512011-08-20 00:17:25 +0000730 if (Opc) {
731 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000732 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000733 if (Opc == ARM::VORRq)
734 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000735 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000736 return;
737 }
738
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000739 // Handle register classes that require multiple instructions.
740 unsigned BeginIdx = 0;
741 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000742 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000743
744 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000745 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
746 Opc = ARM::VORRq;
747 BeginIdx = ARM::qsub_0;
748 SubRegs = 2;
749 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
750 Opc = ARM::VORRq;
751 BeginIdx = ARM::qsub_0;
752 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000753 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000754 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
755 Opc = ARM::VMOVD;
756 BeginIdx = ARM::dsub_0;
757 SubRegs = 2;
758 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
759 Opc = ARM::VMOVD;
760 BeginIdx = ARM::dsub_0;
761 SubRegs = 3;
762 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
763 Opc = ARM::VMOVD;
764 BeginIdx = ARM::dsub_0;
765 SubRegs = 4;
766 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000767 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000768 BeginIdx = ARM::gsub_0;
769 SubRegs = 2;
770 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
771 Opc = ARM::VMOVD;
772 BeginIdx = ARM::dsub_0;
773 SubRegs = 2;
774 Spacing = 2;
775 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
776 Opc = ARM::VMOVD;
777 BeginIdx = ARM::dsub_0;
778 SubRegs = 3;
779 Spacing = 2;
780 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
781 Opc = ARM::VMOVD;
782 BeginIdx = ARM::dsub_0;
783 SubRegs = 4;
784 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000785 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
786 Opc = ARM::VMOVS;
787 BeginIdx = ARM::ssub_0;
788 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000789 } else if (SrcReg == ARM::CPSR) {
790 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
791 return;
792 } else if (DestReg == ARM::CPSR) {
793 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
794 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000795 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000796
Andrew Trickb57e2252012-08-29 04:41:37 +0000797 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000798
Andrew Trick4cc69492012-08-29 01:58:52 +0000799 const TargetRegisterInfo *TRI = &getRegisterInfo();
800 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000801
802 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
803 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000804 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000805 Spacing = -Spacing;
806 }
807#ifndef NDEBUG
808 SmallSet<unsigned, 4> DstRegs;
809#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000810 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000811 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
812 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000813 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000814#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000815 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000816 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000817#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000818 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000819 // VORR takes two source operands.
820 if (Opc == ARM::VORRq)
821 Mov.addReg(Src);
822 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000823 // MOVr can set CC.
824 if (Opc == ARM::MOVr)
825 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000826 }
827 // Add implicit super-register defs and kills to the last instruction.
828 Mov->addRegisterDefined(DestReg, TRI);
829 if (KillSrc)
830 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000831}
832
Tim Northover798697d2013-04-21 11:57:07 +0000833const MachineInstrBuilder &
834ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
835 unsigned SubIdx, unsigned State,
836 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000837 if (!SubIdx)
838 return MIB.addReg(Reg, State);
839
840 if (TargetRegisterInfo::isPhysicalRegister(Reg))
841 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
842 return MIB.addReg(Reg, State, SubIdx);
843}
844
David Goodwinaf7451b2009-07-08 16:09:28 +0000845void ARMBaseInstrInfo::
846storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
847 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000848 const TargetRegisterClass *RC,
849 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000850 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000851 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000852 MachineFunction &MF = *MBB.getParent();
853 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000854 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000855
Alex Lorenze40c8a22015-08-11 23:09:45 +0000856 MachineMemOperand *MMO = MF.getMachineMemOperand(
857 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
858 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000859
Owen Anderson732f82c2011-08-10 17:21:20 +0000860 switch (RC->getSize()) {
861 case 4:
862 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
863 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000864 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000865 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000866 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
867 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000868 .addReg(SrcReg, getKillRegState(isKill))
869 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000870 } else
871 llvm_unreachable("Unknown reg class!");
872 break;
873 case 8:
874 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
875 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000876 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000877 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000878 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000879 if (Subtarget.hasV5TEOps()) {
880 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
881 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
882 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
883 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
884
885 AddDefaultPred(MIB);
886 } else {
887 // Fallback to STM instruction, which has existed since the dawn of
888 // time.
889 MachineInstrBuilder MIB =
890 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
891 .addFrameIndex(FI).addMemOperand(MMO));
892 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
893 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
894 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000895 } else
896 llvm_unreachable("Unknown reg class!");
897 break;
898 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000899 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000900 // Use aligned spills if the stack can be realigned.
901 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000902 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000903 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000904 .addReg(SrcReg, getKillRegState(isKill))
905 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000906 } else {
907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000908 .addReg(SrcReg, getKillRegState(isKill))
909 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000910 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000911 }
912 } else
913 llvm_unreachable("Unknown reg class!");
914 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000915 case 24:
916 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
917 // Use aligned spills if the stack can be realigned.
918 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
919 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
920 .addFrameIndex(FI).addImm(16)
921 .addReg(SrcReg, getKillRegState(isKill))
922 .addMemOperand(MMO));
923 } else {
924 MachineInstrBuilder MIB =
925 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
926 .addFrameIndex(FI))
927 .addMemOperand(MMO);
928 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
929 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
930 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
931 }
932 } else
933 llvm_unreachable("Unknown reg class!");
934 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000935 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000936 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000937 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
938 // FIXME: It's possible to only store part of the QQ register if the
939 // spilled def has a sub-register index.
940 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000941 .addFrameIndex(FI).addImm(16)
942 .addReg(SrcReg, getKillRegState(isKill))
943 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000944 } else {
945 MachineInstrBuilder MIB =
946 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000947 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000948 .addMemOperand(MMO);
949 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
950 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
951 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
952 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
953 }
954 } else
955 llvm_unreachable("Unknown reg class!");
956 break;
957 case 64:
958 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
959 MachineInstrBuilder MIB =
960 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
961 .addFrameIndex(FI))
962 .addMemOperand(MMO);
963 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
964 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
965 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
966 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
967 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
969 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
970 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
971 } else
972 llvm_unreachable("Unknown reg class!");
973 break;
974 default:
975 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000976 }
977}
978
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000979unsigned
980ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
981 int &FrameIndex) const {
982 switch (MI->getOpcode()) {
983 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000984 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000985 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
986 if (MI->getOperand(1).isFI() &&
987 MI->getOperand(2).isReg() &&
988 MI->getOperand(3).isImm() &&
989 MI->getOperand(2).getReg() == 0 &&
990 MI->getOperand(3).getImm() == 0) {
991 FrameIndex = MI->getOperand(1).getIndex();
992 return MI->getOperand(0).getReg();
993 }
994 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000995 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000996 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000997 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000998 case ARM::VSTRD:
999 case ARM::VSTRS:
1000 if (MI->getOperand(1).isFI() &&
1001 MI->getOperand(2).isImm() &&
1002 MI->getOperand(2).getImm() == 0) {
1003 FrameIndex = MI->getOperand(1).getIndex();
1004 return MI->getOperand(0).getReg();
1005 }
1006 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001007 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001008 case ARM::VST1d64TPseudo:
1009 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001010 if (MI->getOperand(0).isFI() &&
1011 MI->getOperand(2).getSubReg() == 0) {
1012 FrameIndex = MI->getOperand(0).getIndex();
1013 return MI->getOperand(2).getReg();
1014 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001015 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001016 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001017 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001018 MI->getOperand(0).getSubReg() == 0) {
1019 FrameIndex = MI->getOperand(1).getIndex();
1020 return MI->getOperand(0).getReg();
1021 }
1022 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001023 }
1024
1025 return 0;
1026}
1027
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001028unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1029 int &FrameIndex) const {
1030 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001031 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001032}
1033
David Goodwinaf7451b2009-07-08 16:09:28 +00001034void ARMBaseInstrInfo::
1035loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1036 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001037 const TargetRegisterClass *RC,
1038 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001039 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001040 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001041 MachineFunction &MF = *MBB.getParent();
1042 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001043 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001044 MachineMemOperand *MMO = MF.getMachineMemOperand(
1045 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1046 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001047
Owen Anderson732f82c2011-08-10 17:21:20 +00001048 switch (RC->getSize()) {
1049 case 4:
1050 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1051 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1052 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001053
Owen Anderson732f82c2011-08-10 17:21:20 +00001054 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1055 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001056 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001057 } else
1058 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001059 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001060 case 8:
1061 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1062 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001063 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001064 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001065 MachineInstrBuilder MIB;
1066
1067 if (Subtarget.hasV5TEOps()) {
1068 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1069 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1070 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1071 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1072
1073 AddDefaultPred(MIB);
1074 } else {
1075 // Fallback to LDM instruction, which has existed since the dawn of
1076 // time.
1077 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1078 .addFrameIndex(FI).addMemOperand(MMO));
1079 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1080 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1081 }
1082
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001083 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1084 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001085 } else
1086 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001087 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001088 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001089 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001090 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001091 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001092 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001093 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001094 } else {
1095 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1096 .addFrameIndex(FI)
1097 .addMemOperand(MMO));
1098 }
1099 } else
1100 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001101 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001102 case 24:
1103 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1104 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1105 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1106 .addFrameIndex(FI).addImm(16)
1107 .addMemOperand(MMO));
1108 } else {
1109 MachineInstrBuilder MIB =
1110 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1111 .addFrameIndex(FI)
1112 .addMemOperand(MMO));
1113 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1114 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1115 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1116 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1117 MIB.addReg(DestReg, RegState::ImplicitDefine);
1118 }
1119 } else
1120 llvm_unreachable("Unknown reg class!");
1121 break;
1122 case 32:
1123 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001124 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1125 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001126 .addFrameIndex(FI).addImm(16)
1127 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001128 } else {
1129 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001130 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1131 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001132 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001133 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1134 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1135 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1136 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001137 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1138 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001139 }
1140 } else
1141 llvm_unreachable("Unknown reg class!");
1142 break;
1143 case 64:
1144 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1145 MachineInstrBuilder MIB =
1146 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1147 .addFrameIndex(FI))
1148 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001149 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1150 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1151 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1152 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1153 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1154 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1155 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1156 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001157 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1158 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001159 } else
1160 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001161 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001162 default:
1163 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001164 }
1165}
1166
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001167unsigned
1168ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1169 int &FrameIndex) const {
1170 switch (MI->getOpcode()) {
1171 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001172 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001173 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1174 if (MI->getOperand(1).isFI() &&
1175 MI->getOperand(2).isReg() &&
1176 MI->getOperand(3).isImm() &&
1177 MI->getOperand(2).getReg() == 0 &&
1178 MI->getOperand(3).getImm() == 0) {
1179 FrameIndex = MI->getOperand(1).getIndex();
1180 return MI->getOperand(0).getReg();
1181 }
1182 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001183 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001184 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001185 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001186 case ARM::VLDRD:
1187 case ARM::VLDRS:
1188 if (MI->getOperand(1).isFI() &&
1189 MI->getOperand(2).isImm() &&
1190 MI->getOperand(2).getImm() == 0) {
1191 FrameIndex = MI->getOperand(1).getIndex();
1192 return MI->getOperand(0).getReg();
1193 }
1194 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001195 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001196 case ARM::VLD1d64TPseudo:
1197 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001198 if (MI->getOperand(1).isFI() &&
1199 MI->getOperand(0).getSubReg() == 0) {
1200 FrameIndex = MI->getOperand(1).getIndex();
1201 return MI->getOperand(0).getReg();
1202 }
1203 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001204 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001205 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001206 MI->getOperand(0).getSubReg() == 0) {
1207 FrameIndex = MI->getOperand(1).getIndex();
1208 return MI->getOperand(0).getReg();
1209 }
1210 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001211 }
1212
1213 return 0;
1214}
1215
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001216unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1217 int &FrameIndex) const {
1218 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001219 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001220}
1221
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001222bool
1223ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1224 MachineFunction &MF = *MI->getParent()->getParent();
1225 Reloc::Model RM = MF.getTarget().getRelocationModel();
1226
1227 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001228 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001229 "LOAD_STACK_GUARD currently supported only for MachO.");
1230 expandLoadStackGuard(MI, RM);
1231 MI->getParent()->erase(MI);
1232 return true;
1233 }
1234
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001235 // This hook gets to expand COPY instructions before they become
1236 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1237 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1238 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001239 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1240 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001241 return false;
1242
1243 // Look for a copy between even S-registers. That is where we keep floats
1244 // when using NEON v2f32 instructions for f32 arithmetic.
1245 unsigned DstRegS = MI->getOperand(0).getReg();
1246 unsigned SrcRegS = MI->getOperand(1).getReg();
1247 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1248 return false;
1249
1250 const TargetRegisterInfo *TRI = &getRegisterInfo();
1251 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1252 &ARM::DPRRegClass);
1253 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1254 &ARM::DPRRegClass);
1255 if (!DstRegD || !SrcRegD)
1256 return false;
1257
1258 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1259 // legal if the COPY already defines the full DstRegD, and it isn't a
1260 // sub-register insertion.
1261 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1262 return false;
1263
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001264 // A dead copy shouldn't show up here, but reject it just in case.
1265 if (MI->getOperand(0).isDead())
1266 return false;
1267
1268 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001269 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001270 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001271
1272 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1273 // or some other super-register.
1274 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1275 if (ImpDefIdx != -1)
1276 MI->RemoveOperand(ImpDefIdx);
1277
1278 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001279 MI->setDesc(get(ARM::VMOVD));
1280 MI->getOperand(0).setReg(DstRegD);
1281 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001282 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001283
1284 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1285 // register scavenger and machine verifier, so we need to indicate that we
1286 // are reading an undefined value from SrcRegD, but a proper value from
1287 // SrcRegS.
1288 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001289 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001290
1291 // SrcRegD may actually contain an unrelated value in the ssub_1
1292 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1293 if (MI->getOperand(1).isKill()) {
1294 MI->getOperand(1).setIsKill(false);
1295 MI->addRegisterKilled(SrcRegS, TRI, true);
1296 }
1297
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001298 DEBUG(dbgs() << "replaced by: " << *MI);
1299 return true;
1300}
1301
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001302/// Create a copy of a const pool value. Update CPI to the new index and return
1303/// the label UID.
1304static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1305 MachineConstantPool *MCP = MF.getConstantPool();
1306 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1307
1308 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1309 assert(MCPE.isMachineConstantPoolEntry() &&
1310 "Expecting a machine constantpool entry!");
1311 ARMConstantPoolValue *ACPV =
1312 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1313
Evan Chengdfce83c2011-01-17 08:03:18 +00001314 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001315 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001316
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001317 // FIXME: The below assumes PIC relocation model and that the function
1318 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1319 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1320 // instructions, so that's probably OK, but is PIC always correct when
1321 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001322 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001323 NewCPV = ARMConstantPoolConstant::
1324 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1325 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001326 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001327 NewCPV = ARMConstantPoolSymbol::
1328 Create(MF.getFunction()->getContext(),
1329 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001330 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001331 NewCPV = ARMConstantPoolConstant::
1332 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1333 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001334 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001335 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1336 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001337 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001338 NewCPV = ARMConstantPoolMBB::
1339 Create(MF.getFunction()->getContext(),
1340 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001341 else
1342 llvm_unreachable("Unexpected ARM constantpool value type!!");
1343 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1344 return PCLabelId;
1345}
1346
Evan Chengfe864422009-11-08 00:15:23 +00001347void ARMBaseInstrInfo::
1348reMaterialize(MachineBasicBlock &MBB,
1349 MachineBasicBlock::iterator I,
1350 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001351 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001352 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001353 unsigned Opcode = Orig->getOpcode();
1354 switch (Opcode) {
1355 default: {
1356 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001357 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001358 MBB.insert(I, MI);
1359 break;
1360 }
1361 case ARM::tLDRpci_pic:
1362 case ARM::t2LDRpci_pic: {
1363 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001364 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001365 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001366 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1367 DestReg)
1368 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001369 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001370 break;
1371 }
1372 }
Evan Chengfe864422009-11-08 00:15:23 +00001373}
1374
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001375MachineInstr *
1376ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001377 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001378 switch(Orig->getOpcode()) {
1379 case ARM::tLDRpci_pic:
1380 case ARM::t2LDRpci_pic: {
1381 unsigned CPI = Orig->getOperand(1).getIndex();
1382 unsigned PCLabelId = duplicateCPV(MF, CPI);
1383 Orig->getOperand(1).setIndex(CPI);
1384 Orig->getOperand(2).setImm(PCLabelId);
1385 break;
1386 }
1387 }
1388 return MI;
1389}
1390
Evan Chenge9c46c22010-03-03 01:44:33 +00001391bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001392 const MachineInstr *MI1,
1393 const MachineRegisterInfo *MRI) const {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001394 unsigned Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001395 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001396 Opcode == ARM::t2LDRpci_pic ||
1397 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001398 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001399 Opcode == ARM::LDRLIT_ga_pcrel ||
1400 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1401 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001402 Opcode == ARM::MOV_ga_pcrel ||
1403 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001404 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001405 if (MI1->getOpcode() != Opcode)
1406 return false;
1407 if (MI0->getNumOperands() != MI1->getNumOperands())
1408 return false;
1409
1410 const MachineOperand &MO0 = MI0->getOperand(1);
1411 const MachineOperand &MO1 = MI1->getOperand(1);
1412 if (MO0.getOffset() != MO1.getOffset())
1413 return false;
1414
Tim Northover72360d22013-12-02 10:35:41 +00001415 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1416 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1417 Opcode == ARM::tLDRLIT_ga_pcrel ||
1418 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001419 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001420 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001421 // Ignore the PC labels.
1422 return MO0.getGlobal() == MO1.getGlobal();
1423
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001424 const MachineFunction *MF = MI0->getParent()->getParent();
1425 const MachineConstantPool *MCP = MF->getConstantPool();
1426 int CPI0 = MO0.getIndex();
1427 int CPI1 = MO1.getIndex();
1428 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1429 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001430 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1431 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1432 if (isARMCP0 && isARMCP1) {
1433 ARMConstantPoolValue *ACPV0 =
1434 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1435 ARMConstantPoolValue *ACPV1 =
1436 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1437 return ACPV0->hasSameValue(ACPV1);
1438 } else if (!isARMCP0 && !isARMCP1) {
1439 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1440 }
1441 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001442 } else if (Opcode == ARM::PICLDR) {
1443 if (MI1->getOpcode() != Opcode)
1444 return false;
1445 if (MI0->getNumOperands() != MI1->getNumOperands())
1446 return false;
1447
1448 unsigned Addr0 = MI0->getOperand(1).getReg();
1449 unsigned Addr1 = MI1->getOperand(1).getReg();
1450 if (Addr0 != Addr1) {
1451 if (!MRI ||
1452 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1453 !TargetRegisterInfo::isVirtualRegister(Addr1))
1454 return false;
1455
1456 // This assumes SSA form.
1457 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1458 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1459 // Check if the loaded value, e.g. a constantpool of a global address, are
1460 // the same.
1461 if (!produceSameValue(Def0, Def1, MRI))
1462 return false;
1463 }
1464
1465 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1466 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1467 const MachineOperand &MO0 = MI0->getOperand(i);
1468 const MachineOperand &MO1 = MI1->getOperand(i);
1469 if (!MO0.isIdenticalTo(MO1))
1470 return false;
1471 }
1472 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001473 }
1474
Evan Chenge9c46c22010-03-03 01:44:33 +00001475 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001476}
1477
Bill Wendlingf4707472010-06-23 23:00:16 +00001478/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1479/// determine if two loads are loading from the same base address. It should
1480/// only return true if the base pointers are the same and the only differences
1481/// between the two addresses is the offset. It also returns the offsets by
1482/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001483///
1484/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1485/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001486bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1487 int64_t &Offset1,
1488 int64_t &Offset2) const {
1489 // Don't worry about Thumb: just ARM and Thumb2.
1490 if (Subtarget.isThumb1Only()) return false;
1491
1492 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1493 return false;
1494
1495 switch (Load1->getMachineOpcode()) {
1496 default:
1497 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001498 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001499 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001500 case ARM::LDRD:
1501 case ARM::LDRH:
1502 case ARM::LDRSB:
1503 case ARM::LDRSH:
1504 case ARM::VLDRD:
1505 case ARM::VLDRS:
1506 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001507 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001508 case ARM::t2LDRDi8:
1509 case ARM::t2LDRSHi8:
1510 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001511 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001512 case ARM::t2LDRSHi12:
1513 break;
1514 }
1515
1516 switch (Load2->getMachineOpcode()) {
1517 default:
1518 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001519 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001520 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001521 case ARM::LDRD:
1522 case ARM::LDRH:
1523 case ARM::LDRSB:
1524 case ARM::LDRSH:
1525 case ARM::VLDRD:
1526 case ARM::VLDRS:
1527 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001528 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001529 case ARM::t2LDRSHi8:
1530 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001531 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001532 case ARM::t2LDRSHi12:
1533 break;
1534 }
1535
1536 // Check if base addresses and chain operands match.
1537 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1538 Load1->getOperand(4) != Load2->getOperand(4))
1539 return false;
1540
1541 // Index should be Reg0.
1542 if (Load1->getOperand(3) != Load2->getOperand(3))
1543 return false;
1544
1545 // Determine the offsets.
1546 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1547 isa<ConstantSDNode>(Load2->getOperand(1))) {
1548 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1549 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1550 return true;
1551 }
1552
1553 return false;
1554}
1555
1556/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001557/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001558/// be scheduled togther. On some targets if two loads are loading from
1559/// addresses in the same cache line, it's better if they are scheduled
1560/// together. This function takes two integers that represent the load offsets
1561/// from the common base address. It returns true if it decides it's desirable
1562/// to schedule the two loads together. "NumLoads" is the number of loads that
1563/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001564///
1565/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1566/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001567bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1568 int64_t Offset1, int64_t Offset2,
1569 unsigned NumLoads) const {
1570 // Don't worry about Thumb: just ARM and Thumb2.
1571 if (Subtarget.isThumb1Only()) return false;
1572
1573 assert(Offset2 > Offset1);
1574
1575 if ((Offset2 - Offset1) / 8 > 64)
1576 return false;
1577
Renato Golinb184cd92013-08-14 16:35:29 +00001578 // Check if the machine opcodes are different. If they are different
1579 // then we consider them to not be of the same base address,
1580 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1581 // In this case, they are considered to be the same because they are different
1582 // encoding forms of the same basic instruction.
1583 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1584 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1585 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1586 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1587 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001588 return false; // FIXME: overly conservative?
1589
1590 // Four loads in a row should be sufficient.
1591 if (NumLoads >= 3)
1592 return false;
1593
1594 return true;
1595}
1596
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001597bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1598 const MachineBasicBlock *MBB,
1599 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001600 // Debug info is never a scheduling boundary. It's necessary to be explicit
1601 // due to the special treatment of IT instructions below, otherwise a
1602 // dbg_value followed by an IT will result in the IT instruction being
1603 // considered a scheduling hazard, which is wrong. It should be the actual
1604 // instruction preceding the dbg_value instruction(s), just like it is
1605 // when debug info is not present.
1606 if (MI->isDebugValue())
1607 return false;
1608
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001609 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001610 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001611 return true;
1612
1613 // Treat the start of the IT block as a scheduling boundary, but schedule
1614 // t2IT along with all instructions following it.
1615 // FIXME: This is a big hammer. But the alternative is to add all potential
1616 // true and anti dependencies to IT block instructions as implicit operands
1617 // to the t2IT instruction. The added compile time and complexity does not
1618 // seem worth it.
1619 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001620 // Make sure to skip any dbg_value instructions
1621 while (++I != MBB->end() && I->isDebugValue())
1622 ;
1623 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001624 return true;
1625
1626 // Don't attempt to schedule around any instruction that defines
1627 // a stack-oriented pointer, as it's unlikely to be profitable. This
1628 // saves compile time, because it doesn't require every single
1629 // stack slot reference to depend on the instruction that does the
1630 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001631 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001632 // No ARM calling conventions change the stack pointer. (X86 calling
1633 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001634 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001635 return true;
1636
1637 return false;
1638}
1639
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001640bool ARMBaseInstrInfo::
1641isProfitableToIfCvt(MachineBasicBlock &MBB,
1642 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001643 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001644 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001645 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001646
Peter Collingbourne65295232015-04-23 20:31:30 +00001647 // If we are optimizing for size, see if the branch in the predecessor can be
1648 // lowered to cbn?z by the constant island lowering pass, and return false if
1649 // so. This results in a shorter instruction sequence.
Sanjay Patel924879a2015-08-04 15:49:57 +00001650 if (MBB.getParent()->getFunction()->optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001651 MachineBasicBlock *Pred = *MBB.pred_begin();
1652 if (!Pred->empty()) {
1653 MachineInstr *LastMI = &*Pred->rbegin();
1654 if (LastMI->getOpcode() == ARM::t2Bcc) {
1655 MachineBasicBlock::iterator CmpMI = LastMI;
1656 if (CmpMI != Pred->begin()) {
1657 --CmpMI;
1658 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1659 CmpMI->getOpcode() == ARM::t2CMPri) {
1660 unsigned Reg = CmpMI->getOperand(0).getReg();
1661 unsigned PredReg = 0;
1662 ARMCC::CondCodes P = getInstrPredicate(CmpMI, PredReg);
1663 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1664 isARMLowRegister(Reg))
1665 return false;
1666 }
1667 }
1668 }
1669 }
1670 }
1671
Owen Anderson88af7d02010-09-28 18:32:13 +00001672 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001673 // Here we scale up each component of UnpredCost to avoid precision issue when
1674 // scaling NumCycles by Probability.
1675 const unsigned ScalingUpFactor = 1024;
1676 unsigned UnpredCost = Probability.scale(NumCycles * ScalingUpFactor);
1677 UnpredCost += ScalingUpFactor; // The branch itself
1678 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001679
Cong Houf9f9ffb2015-09-18 18:19:40 +00001680 return (NumCycles + ExtraPredCycles) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001681}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001682
Evan Cheng02b184d2010-06-25 22:42:03 +00001683bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001684isProfitableToIfCvt(MachineBasicBlock &TMBB,
1685 unsigned TCycles, unsigned TExtra,
1686 MachineBasicBlock &FMBB,
1687 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001688 BranchProbability Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001689 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001690 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001691
Owen Anderson88af7d02010-09-28 18:32:13 +00001692 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001693 // Here we scale up each component of UnpredCost to avoid precision issue when
1694 // scaling TCycles/FCycles by Probability.
1695 const unsigned ScalingUpFactor = 1024;
1696 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1697 unsigned FUnpredCost =
1698 Probability.getCompl().scale(FCycles * ScalingUpFactor);
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001699 unsigned UnpredCost = TUnpredCost + FUnpredCost;
Cong Houf9f9ffb2015-09-18 18:19:40 +00001700 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1701 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001702
Cong Houf9f9ffb2015-09-18 18:19:40 +00001703 return (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001704}
1705
Bob Wilsone8a549c2012-09-29 21:43:49 +00001706bool
1707ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1708 MachineBasicBlock &FMBB) const {
1709 // Reduce false anti-dependencies to let Swift's out-of-order execution
1710 // engine do its thing.
1711 return Subtarget.isSwift();
1712}
1713
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001714/// getInstrPredicate - If instruction is predicated, returns its predicate
1715/// condition, otherwise returns AL. It also returns the condition code
1716/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001717ARMCC::CondCodes
1718llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001719 int PIdx = MI->findFirstPredOperandIdx();
1720 if (PIdx == -1) {
1721 PredReg = 0;
1722 return ARMCC::AL;
1723 }
1724
1725 PredReg = MI->getOperand(PIdx+1).getReg();
1726 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1727}
1728
1729
Matthias Braunfa3872e2015-05-18 20:27:55 +00001730unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001731 if (Opc == ARM::B)
1732 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001733 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001734 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001735 if (Opc == ARM::t2B)
1736 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001737
1738 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001739}
1740
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001741MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr *MI,
1742 bool NewMI,
1743 unsigned OpIdx1,
1744 unsigned OpIdx2) const {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001745 switch (MI->getOpcode()) {
1746 case ARM::MOVCCr:
1747 case ARM::t2MOVCCr: {
1748 // MOVCC can be commuted by inverting the condition.
1749 unsigned PredReg = 0;
1750 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1751 // MOVCC AL can't be inverted. Shouldn't happen.
1752 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001753 return nullptr;
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001754 MI = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001755 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001756 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001757 // After swapping the MOVCC operands, also invert the condition.
1758 MI->getOperand(MI->findFirstPredOperandIdx())
1759 .setImm(ARMCC::getOppositeCondition(CC));
1760 return MI;
1761 }
1762 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001763 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001764}
Evan Cheng780748d2009-07-28 05:48:47 +00001765
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001766/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001767/// return the defining instruction.
1768static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1769 const MachineRegisterInfo &MRI,
1770 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001771 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001772 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001773 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001774 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001775 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001776 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001777 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001778 // MI is folded into the MOVCC by predicating it.
1779 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001780 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001781 // Check if MI has any non-dead defs or physreg uses. This also detects
1782 // predicated instructions which will be reading CPSR.
1783 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1784 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001785 // Reject frame index operands, PEI can't handle the predicated pseudos.
1786 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001787 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001788 if (!MO.isReg())
1789 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001790 // MI can't have any tied operands, that would conflict with predication.
1791 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001792 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001793 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001794 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001795 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001796 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001797 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001798 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001799 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001800 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001801 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001802}
1803
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001804bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1805 SmallVectorImpl<MachineOperand> &Cond,
1806 unsigned &TrueOp, unsigned &FalseOp,
1807 bool &Optimizable) const {
1808 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1809 "Unknown select instruction");
1810 // MOVCC operands:
1811 // 0: Def.
1812 // 1: True use.
1813 // 2: False use.
1814 // 3: Condition code.
1815 // 4: CPSR use.
1816 TrueOp = 1;
1817 FalseOp = 2;
1818 Cond.push_back(MI->getOperand(3));
1819 Cond.push_back(MI->getOperand(4));
1820 // We can always fold a def.
1821 Optimizable = true;
1822 return false;
1823}
1824
Mehdi Amini22e59742015-01-13 07:07:13 +00001825MachineInstr *
1826ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1827 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1828 bool PreferFalse) const {
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001829 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1830 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001831 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001832 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1833 bool Invert = !DefMI;
1834 if (!DefMI)
1835 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1836 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001837 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001838
Matthias Braun2f169f92013-10-04 16:52:56 +00001839 // Find new register class to use.
1840 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1841 unsigned DestReg = MI->getOperand(0).getReg();
1842 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1843 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001844 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001845
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001846 // Create a new predicated version of DefMI.
1847 // Rfalse is the first use.
1848 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001849 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001850
1851 // Copy all the DefMI operands, excluding its (null) predicate.
1852 const MCInstrDesc &DefDesc = DefMI->getDesc();
1853 for (unsigned i = 1, e = DefDesc.getNumOperands();
1854 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1855 NewMI.addOperand(DefMI->getOperand(i));
1856
1857 unsigned CondCode = MI->getOperand(3).getImm();
1858 if (Invert)
1859 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1860 else
1861 NewMI.addImm(CondCode);
1862 NewMI.addOperand(MI->getOperand(4));
1863
1864 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1865 if (NewMI->hasOptionalDef())
1866 AddDefaultCC(NewMI);
1867
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001868 // The output register value when the predicate is false is an implicit
1869 // register operand tied to the first def.
1870 // The tie makes the register allocator ensure the FalseReg is allocated the
1871 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001872 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001873 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001874 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1875
Mehdi Amini22e59742015-01-13 07:07:13 +00001876 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1877 SeenMIs.insert(NewMI);
1878 SeenMIs.erase(DefMI);
1879
Pete Cooper2127b002015-04-30 23:57:47 +00001880 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1881 // DefMI would be invalid when tranferred inside the loop. Checking for a
1882 // loop is expensive, but at least remove kill flags if they are in different
1883 // BBs.
1884 if (DefMI->getParent() != MI->getParent())
1885 NewMI->clearKillInfo();
1886
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001887 // The caller will erase MI, but not DefMI.
1888 DefMI->eraseFromParent();
1889 return NewMI;
1890}
1891
Andrew Trick924123a2011-09-21 02:20:46 +00001892/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1893/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1894/// def operand.
1895///
1896/// This will go away once we can teach tblgen how to set the optional CPSR def
1897/// operand itself.
1898struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001899 uint16_t PseudoOpc;
1900 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001901};
1902
Craig Topper2fbd1302012-05-24 03:59:11 +00001903static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001904 {ARM::ADDSri, ARM::ADDri},
1905 {ARM::ADDSrr, ARM::ADDrr},
1906 {ARM::ADDSrsi, ARM::ADDrsi},
1907 {ARM::ADDSrsr, ARM::ADDrsr},
1908
1909 {ARM::SUBSri, ARM::SUBri},
1910 {ARM::SUBSrr, ARM::SUBrr},
1911 {ARM::SUBSrsi, ARM::SUBrsi},
1912 {ARM::SUBSrsr, ARM::SUBrsr},
1913
1914 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001915 {ARM::RSBSrsi, ARM::RSBrsi},
1916 {ARM::RSBSrsr, ARM::RSBrsr},
1917
1918 {ARM::t2ADDSri, ARM::t2ADDri},
1919 {ARM::t2ADDSrr, ARM::t2ADDrr},
1920 {ARM::t2ADDSrs, ARM::t2ADDrs},
1921
1922 {ARM::t2SUBSri, ARM::t2SUBri},
1923 {ARM::t2SUBSrr, ARM::t2SUBrr},
1924 {ARM::t2SUBSrs, ARM::t2SUBrs},
1925
1926 {ARM::t2RSBSri, ARM::t2RSBri},
1927 {ARM::t2RSBSrs, ARM::t2RSBrs},
1928};
1929
1930unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001931 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1932 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1933 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001934 return 0;
1935}
1936
Evan Cheng780748d2009-07-28 05:48:47 +00001937void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1938 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1939 unsigned DestReg, unsigned BaseReg, int NumBytes,
1940 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001941 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001942 if (NumBytes == 0 && DestReg != BaseReg) {
1943 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1944 .addReg(BaseReg, RegState::Kill)
1945 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1946 .setMIFlags(MIFlags);
1947 return;
1948 }
1949
Evan Cheng780748d2009-07-28 05:48:47 +00001950 bool isSub = NumBytes < 0;
1951 if (isSub) NumBytes = -NumBytes;
1952
1953 while (NumBytes) {
1954 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1955 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1956 assert(ThisVal && "Didn't extract field correctly");
1957
1958 // We will handle these bits from offset, clear them.
1959 NumBytes &= ~ThisVal;
1960
1961 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1962
1963 // Build the new ADD / SUB.
1964 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1965 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1966 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001967 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1968 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00001969 BaseReg = DestReg;
1970 }
1971}
1972
Weiming Zhao01524852014-03-20 23:28:16 +00001973static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1974 MachineInstr *MI) {
1975 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1976 Subreg.isValid(); ++Subreg)
1977 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1978 MachineBasicBlock::LQR_Dead)
1979 return true;
1980 return false;
1981}
Tim Northoverdee86042013-12-02 14:46:26 +00001982bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1983 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00001984 unsigned NumBytes) {
1985 // This optimisation potentially adds lots of load and store
1986 // micro-operations, it's only really a great benefit to code-size.
Sanjay Patel924879a2015-08-04 15:49:57 +00001987 if (!MF.getFunction()->optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00001988 return false;
1989
1990 // If only one register is pushed/popped, LLVM can use an LDR/STR
1991 // instead. We can't modify those so make sure we're dealing with an
1992 // instruction we understand.
1993 bool IsPop = isPopOpcode(MI->getOpcode());
1994 bool IsPush = isPushOpcode(MI->getOpcode());
1995 if (!IsPush && !IsPop)
1996 return false;
1997
1998 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
1999 MI->getOpcode() == ARM::VLDMDIA_UPD;
2000 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2001 MI->getOpcode() == ARM::tPOP ||
2002 MI->getOpcode() == ARM::tPOP_RET;
2003
2004 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2005 MI->getOperand(1).getReg() == ARM::SP)) &&
2006 "trying to fold sp update into non-sp-updating push/pop");
2007
2008 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2009 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2010 // if this is violated.
2011 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2012 return false;
2013
2014 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2015 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2016 int RegListIdx = IsT1PushPop ? 2 : 4;
2017
2018 // Calculate the space we'll need in terms of registers.
2019 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2020 unsigned RD0Reg, RegsNeeded;
2021 if (IsVFPPushPop) {
2022 RD0Reg = ARM::D0;
2023 RegsNeeded = NumBytes / 8;
2024 } else {
2025 RD0Reg = ARM::R0;
2026 RegsNeeded = NumBytes / 4;
2027 }
2028
2029 // We're going to have to strip all list operands off before
2030 // re-adding them since the order matters, so save the existing ones
2031 // for later.
2032 SmallVector<MachineOperand, 4> RegList;
2033 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2034 RegList.push_back(MI->getOperand(i));
2035
Tim Northover93bcc662013-11-08 17:18:07 +00002036 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002037 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002038
2039 // Now try to find enough space in the reglist to allocate NumBytes.
2040 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002041 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002042 if (!IsPop) {
2043 // Pushing any register is completely harmless, mark the
2044 // register involved as undef since we don't care about it in
2045 // the slightest.
2046 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2047 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002048 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002049 continue;
2050 }
2051
Tim Northover45479dc2013-12-01 14:16:24 +00002052 // However, we can only pop an extra register if it's not live. For
2053 // registers live within the function we might clobber a return value
2054 // register; the other way a register can be live here is if it's
2055 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00002056 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
2057 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
2058 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00002059 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00002060 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00002061 // VFP pops don't allow holes in the register list, so any skip is fatal
2062 // for our transformation. GPR pops do, so we should just keep looking.
2063 if (IsVFPPushPop)
2064 return false;
2065 else
2066 continue;
2067 }
Tim Northover93bcc662013-11-08 17:18:07 +00002068
2069 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002070 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2071 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002072 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002073 }
2074
2075 if (RegsNeeded > 0)
2076 return false;
2077
2078 // Finally we know we can profitably perform the optimisation so go
2079 // ahead: strip all existing registers off and add them back again
2080 // in the right order.
2081 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2082 MI->RemoveOperand(i);
2083
2084 // Add the complete list back in.
2085 MachineInstrBuilder MIB(MF, &*MI);
2086 for (int i = RegList.size() - 1; i >= 0; --i)
2087 MIB.addOperand(RegList[i]);
2088
2089 return true;
2090}
2091
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002092bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2093 unsigned FrameReg, int &Offset,
2094 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002095 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002096 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002097 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2098 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002099
Evan Cheng780748d2009-07-28 05:48:47 +00002100 // Memory operands in inline assembly always use AddrMode2.
2101 if (Opcode == ARM::INLINEASM)
2102 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002103
Evan Cheng780748d2009-07-28 05:48:47 +00002104 if (Opcode == ARM::ADDri) {
2105 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2106 if (Offset == 0) {
2107 // Turn it into a move.
2108 MI.setDesc(TII.get(ARM::MOVr));
2109 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2110 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002111 Offset = 0;
2112 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002113 } else if (Offset < 0) {
2114 Offset = -Offset;
2115 isSub = true;
2116 MI.setDesc(TII.get(ARM::SUBri));
2117 }
2118
2119 // Common case: small offset, fits into instruction.
2120 if (ARM_AM::getSOImmVal(Offset) != -1) {
2121 // Replace the FrameIndex with sp / fp
2122 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2123 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002124 Offset = 0;
2125 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002126 }
2127
2128 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2129 // as possible.
2130 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2131 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2132
2133 // We will handle these bits from offset, clear them.
2134 Offset &= ~ThisImmVal;
2135
2136 // Get the properly encoded SOImmVal field.
2137 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2138 "Bit extraction didn't work?");
2139 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2140 } else {
2141 unsigned ImmIdx = 0;
2142 int InstrOffs = 0;
2143 unsigned NumBits = 0;
2144 unsigned Scale = 1;
2145 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002146 case ARMII::AddrMode_i12: {
2147 ImmIdx = FrameRegIdx + 1;
2148 InstrOffs = MI.getOperand(ImmIdx).getImm();
2149 NumBits = 12;
2150 break;
2151 }
Evan Cheng780748d2009-07-28 05:48:47 +00002152 case ARMII::AddrMode2: {
2153 ImmIdx = FrameRegIdx+2;
2154 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2155 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2156 InstrOffs *= -1;
2157 NumBits = 12;
2158 break;
2159 }
2160 case ARMII::AddrMode3: {
2161 ImmIdx = FrameRegIdx+2;
2162 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2163 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2164 InstrOffs *= -1;
2165 NumBits = 8;
2166 break;
2167 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002168 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002169 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002170 // Can't fold any offset even if it's zero.
2171 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002172 case ARMII::AddrMode5: {
2173 ImmIdx = FrameRegIdx+1;
2174 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2175 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2176 InstrOffs *= -1;
2177 NumBits = 8;
2178 Scale = 4;
2179 break;
2180 }
2181 default:
2182 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002183 }
2184
2185 Offset += InstrOffs * Scale;
2186 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2187 if (Offset < 0) {
2188 Offset = -Offset;
2189 isSub = true;
2190 }
2191
2192 // Attempt to fold address comp. if opcode has offset bits
2193 if (NumBits > 0) {
2194 // Common case: small offset, fits into instruction.
2195 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2196 int ImmedOffset = Offset / Scale;
2197 unsigned Mask = (1 << NumBits) - 1;
2198 if ((unsigned)Offset <= Mask * Scale) {
2199 // Replace the FrameIndex with sp
2200 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002201 // FIXME: When addrmode2 goes away, this will simplify (like the
2202 // T2 version), as the LDR.i12 versions don't need the encoding
2203 // tricks for the offset value.
2204 if (isSub) {
2205 if (AddrMode == ARMII::AddrMode_i12)
2206 ImmedOffset = -ImmedOffset;
2207 else
2208 ImmedOffset |= 1 << NumBits;
2209 }
Evan Cheng780748d2009-07-28 05:48:47 +00002210 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002211 Offset = 0;
2212 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002213 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002214
Evan Cheng780748d2009-07-28 05:48:47 +00002215 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2216 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002217 if (isSub) {
2218 if (AddrMode == ARMII::AddrMode_i12)
2219 ImmedOffset = -ImmedOffset;
2220 else
2221 ImmedOffset |= 1 << NumBits;
2222 }
Evan Cheng780748d2009-07-28 05:48:47 +00002223 ImmOp.ChangeToImmediate(ImmedOffset);
2224 Offset &= ~(Mask*Scale);
2225 }
2226 }
2227
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002228 Offset = (isSub) ? -Offset : Offset;
2229 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002230}
Bill Wendling7de9d522010-08-06 01:32:48 +00002231
Manman Ren6fa76dc2012-06-29 21:33:59 +00002232/// analyzeCompare - For a comparison instruction, return the source registers
2233/// in SrcReg and SrcReg2 if having two register operands, and the value it
2234/// compares against in CmpValue. Return true if the comparison instruction
2235/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002236bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002237analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2238 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002239 switch (MI->getOpcode()) {
2240 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002241 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002242 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002243 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002244 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002245 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002246 CmpValue = MI->getOperand(1).getImm();
2247 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002248 case ARM::CMPrr:
2249 case ARM::t2CMPrr:
2250 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002251 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002252 CmpMask = ~0;
2253 CmpValue = 0;
2254 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002255 case ARM::TSTri:
2256 case ARM::t2TSTri:
2257 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002258 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002259 CmpMask = MI->getOperand(1).getImm();
2260 CmpValue = 0;
2261 return true;
2262 }
2263
2264 return false;
2265}
2266
Gabor Greifd36e3e82010-09-29 10:12:08 +00002267/// isSuitableForMask - Identify a suitable 'and' instruction that
2268/// operates on the given source register and applies the same mask
2269/// as a 'tst' instruction. Provide a limited look-through for copies.
2270/// When successful, MI will hold the found instruction.
2271static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002272 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002273 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002274 case ARM::ANDri:
2275 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002276 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002277 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002278 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002279 return true;
2280 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002281 }
2282
2283 return false;
2284}
2285
Manman Renb1b3db62012-06-29 22:06:19 +00002286/// getSwappedCondition - assume the flags are set by MI(a,b), return
2287/// the condition code if we modify the instructions such that flags are
2288/// set by MI(b,a).
2289inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2290 switch (CC) {
2291 default: return ARMCC::AL;
2292 case ARMCC::EQ: return ARMCC::EQ;
2293 case ARMCC::NE: return ARMCC::NE;
2294 case ARMCC::HS: return ARMCC::LS;
2295 case ARMCC::LO: return ARMCC::HI;
2296 case ARMCC::HI: return ARMCC::LO;
2297 case ARMCC::LS: return ARMCC::HS;
2298 case ARMCC::GE: return ARMCC::LE;
2299 case ARMCC::LT: return ARMCC::GT;
2300 case ARMCC::GT: return ARMCC::LT;
2301 case ARMCC::LE: return ARMCC::GE;
2302 }
2303}
2304
2305/// isRedundantFlagInstr - check whether the first instruction, whose only
2306/// purpose is to update flags, can be made redundant.
2307/// CMPrr can be made redundant by SUBrr if the operands are the same.
2308/// CMPri can be made redundant by SUBri if the operands are the same.
2309/// This function can be extended later on.
2310inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2311 unsigned SrcReg2, int ImmValue,
2312 MachineInstr *OI) {
2313 if ((CmpI->getOpcode() == ARM::CMPrr ||
2314 CmpI->getOpcode() == ARM::t2CMPrr) &&
2315 (OI->getOpcode() == ARM::SUBrr ||
2316 OI->getOpcode() == ARM::t2SUBrr) &&
2317 ((OI->getOperand(1).getReg() == SrcReg &&
2318 OI->getOperand(2).getReg() == SrcReg2) ||
2319 (OI->getOperand(1).getReg() == SrcReg2 &&
2320 OI->getOperand(2).getReg() == SrcReg)))
2321 return true;
2322
2323 if ((CmpI->getOpcode() == ARM::CMPri ||
2324 CmpI->getOpcode() == ARM::t2CMPri) &&
2325 (OI->getOpcode() == ARM::SUBri ||
2326 OI->getOpcode() == ARM::t2SUBri) &&
2327 OI->getOperand(1).getReg() == SrcReg &&
2328 OI->getOperand(2).getImm() == ImmValue)
2329 return true;
2330 return false;
2331}
2332
Manman Ren6fa76dc2012-06-29 21:33:59 +00002333/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2334/// comparison into one that sets the zero bit in the flags register;
2335/// Remove a redundant Compare instruction if an earlier instruction can set the
2336/// flags in the same way as Compare.
2337/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2338/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2339/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002340bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002341optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2342 int CmpMask, int CmpValue,
2343 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002344 // Get the unique definition of SrcReg.
2345 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2346 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002347
Gabor Greifadbbb932010-09-21 12:01:15 +00002348 // Masked compares sometimes use the same register as the corresponding 'and'.
2349 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002350 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002351 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002352 for (MachineRegisterInfo::use_instr_iterator
2353 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2354 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002355 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002356 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002357 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2358 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002359 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002360 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002361 break;
2362 }
2363 if (!MI) return false;
2364 }
2365 }
2366
Manman Rendc8ad002012-05-11 01:30:47 +00002367 // Get ready to iterate backward from CmpInstr.
2368 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2369 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002370
2371 // Early exit if CmpInstr is at the beginning of the BB.
2372 if (I == B) return false;
2373
Manman Rendc8ad002012-05-11 01:30:47 +00002374 // There are two possible candidates which can be changed to set CPSR:
2375 // One is MI, the other is a SUB instruction.
2376 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2377 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002378 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002379 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002380 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002381 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002382 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002383 // Conservatively refuse to convert an instruction which isn't in the same
2384 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002385 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2386 // Thus we cannot return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002387 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002388 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002389 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002390 else
2391 return false;
2392 }
2393
2394 // Check that CPSR isn't set between the comparison instruction and the one we
2395 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002396 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002397 --I;
2398 for (; I != E; --I) {
2399 const MachineInstr &Instr = *I;
2400
Manman Renb1b3db62012-06-29 22:06:19 +00002401 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2402 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002403 // This instruction modifies or uses CPSR after the one we want to
2404 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002405 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002406
Manman Renb1b3db62012-06-29 22:06:19 +00002407 // Check whether CmpInstr can be made redundant by the current instruction.
2408 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002409 Sub = &*I;
2410 break;
2411 }
2412
Evan Chengd757c882010-09-21 23:49:07 +00002413 if (I == B)
2414 // The 'and' is below the comparison instruction.
2415 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002416 }
2417
Manman Rendc8ad002012-05-11 01:30:47 +00002418 // Return false if no candidates exist.
2419 if (!MI && !Sub)
2420 return false;
2421
2422 // The single candidate is called MI.
2423 if (!MI) MI = Sub;
2424
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002425 // We can't use a predicated instruction - it doesn't always write the flags.
2426 if (isPredicated(MI))
2427 return false;
2428
Bill Wendling7de9d522010-08-06 01:32:48 +00002429 switch (MI->getOpcode()) {
2430 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002431 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002432 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002433 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002434 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002435 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002436 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002437 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002438 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002439 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002440 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002441 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002442 case ARM::SBCri:
2443 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002444 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002445 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002446 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002447 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002448 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002449 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002450 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002451 case ARM::t2SBCri:
2452 case ARM::ANDrr:
2453 case ARM::ANDri:
2454 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002455 case ARM::t2ANDri:
2456 case ARM::ORRrr:
2457 case ARM::ORRri:
2458 case ARM::t2ORRrr:
2459 case ARM::t2ORRri:
2460 case ARM::EORrr:
2461 case ARM::EORri:
2462 case ARM::t2EORrr:
2463 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002464 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002465 // When checking against MI: if it's a conditional code that requires
2466 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002467 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2468 // If we are done with the basic block, we need to check whether CPSR is
2469 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002470 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2471 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002472 bool isSafe = false;
2473 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002474 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002475 while (!isSafe && ++I != E) {
2476 const MachineInstr &Instr = *I;
2477 for (unsigned IO = 0, EO = Instr.getNumOperands();
2478 !isSafe && IO != EO; ++IO) {
2479 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002480 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2481 isSafe = true;
2482 break;
2483 }
Evan Cheng425489d2011-03-23 22:52:04 +00002484 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2485 continue;
2486 if (MO.isDef()) {
2487 isSafe = true;
2488 break;
2489 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002490 // Condition code is after the operand before CPSR except for VSELs.
2491 ARMCC::CondCodes CC;
2492 bool IsInstrVSel = true;
2493 switch (Instr.getOpcode()) {
2494 default:
2495 IsInstrVSel = false;
2496 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2497 break;
2498 case ARM::VSELEQD:
2499 case ARM::VSELEQS:
2500 CC = ARMCC::EQ;
2501 break;
2502 case ARM::VSELGTD:
2503 case ARM::VSELGTS:
2504 CC = ARMCC::GT;
2505 break;
2506 case ARM::VSELGED:
2507 case ARM::VSELGES:
2508 CC = ARMCC::GE;
2509 break;
2510 case ARM::VSELVSS:
2511 case ARM::VSELVSD:
2512 CC = ARMCC::VS;
2513 break;
2514 }
2515
Manman Renb1b3db62012-06-29 22:06:19 +00002516 if (Sub) {
2517 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2518 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002519 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002520 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2521 // on CMP needs to be updated to be based on SUB.
2522 // Push the condition code operands to OperandsToUpdate.
2523 // If it is safe to remove CmpInstr, the condition code of these
2524 // operands will be modified.
2525 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002526 Sub->getOperand(2).getReg() == SrcReg) {
2527 // VSel doesn't support condition code update.
2528 if (IsInstrVSel)
2529 return false;
2530 OperandsToUpdate.push_back(
2531 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2532 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002533 } else {
2534 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002535 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002536 case ARMCC::EQ: // Z
2537 case ARMCC::NE: // Z
2538 case ARMCC::MI: // N
2539 case ARMCC::PL: // N
2540 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002541 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002542 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002543 case ARMCC::HS: // C
2544 case ARMCC::LO: // C
2545 case ARMCC::VS: // V
2546 case ARMCC::VC: // V
2547 case ARMCC::HI: // C Z
2548 case ARMCC::LS: // C Z
2549 case ARMCC::GE: // N V
2550 case ARMCC::LT: // N V
2551 case ARMCC::GT: // Z N V
2552 case ARMCC::LE: // Z N V
2553 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002554 return false;
2555 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002556 }
Evan Cheng425489d2011-03-23 22:52:04 +00002557 }
2558 }
2559
Manman Ren34cb93e2012-07-11 22:51:44 +00002560 // If CPSR is not killed nor re-defined, we should check whether it is
2561 // live-out. If it is live-out, do not optimize.
2562 if (!isSafe) {
2563 MachineBasicBlock *MBB = CmpInstr->getParent();
2564 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2565 SE = MBB->succ_end(); SI != SE; ++SI)
2566 if ((*SI)->isLiveIn(ARM::CPSR))
2567 return false;
2568 }
Evan Cheng425489d2011-03-23 22:52:04 +00002569
Evan Cheng65536472010-11-17 08:06:50 +00002570 // Toggle the optional operand to CPSR.
2571 MI->getOperand(5).setReg(ARM::CPSR);
2572 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002573 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002574 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002575
2576 // Modify the condition code of operands in OperandsToUpdate.
2577 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2578 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002579 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2580 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002581 return true;
2582 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002583 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002584
2585 return false;
2586}
Evan Cheng367a5df2010-09-09 18:18:55 +00002587
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002588bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2589 MachineInstr *DefMI, unsigned Reg,
2590 MachineRegisterInfo *MRI) const {
2591 // Fold large immediates into add, sub, or, xor.
2592 unsigned DefOpc = DefMI->getOpcode();
2593 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2594 return false;
2595 if (!DefMI->getOperand(1).isImm())
2596 // Could be t2MOVi32imm <ga:xx>
2597 return false;
2598
2599 if (!MRI->hasOneNonDBGUse(Reg))
2600 return false;
2601
Evan Chenga2b48d92012-03-26 23:31:00 +00002602 const MCInstrDesc &DefMCID = DefMI->getDesc();
2603 if (DefMCID.hasOptionalDef()) {
2604 unsigned NumOps = DefMCID.getNumOperands();
2605 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2606 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2607 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2608 // to delete DefMI.
2609 return false;
2610 }
2611
2612 const MCInstrDesc &UseMCID = UseMI->getDesc();
2613 if (UseMCID.hasOptionalDef()) {
2614 unsigned NumOps = UseMCID.getNumOperands();
2615 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2616 // If the instruction sets the flag, do not attempt this optimization
2617 // since it may change the semantics of the code.
2618 return false;
2619 }
2620
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002621 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002622 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002623 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002624 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002625 bool Commute = false;
2626 switch (UseOpc) {
2627 default: return false;
2628 case ARM::SUBrr:
2629 case ARM::ADDrr:
2630 case ARM::ORRrr:
2631 case ARM::EORrr:
2632 case ARM::t2SUBrr:
2633 case ARM::t2ADDrr:
2634 case ARM::t2ORRrr:
2635 case ARM::t2EORrr: {
2636 Commute = UseMI->getOperand(2).getReg() != Reg;
2637 switch (UseOpc) {
2638 default: break;
2639 case ARM::SUBrr: {
2640 if (Commute)
2641 return false;
2642 ImmVal = -ImmVal;
2643 NewUseOpc = ARM::SUBri;
2644 // Fallthrough
2645 }
2646 case ARM::ADDrr:
2647 case ARM::ORRrr:
2648 case ARM::EORrr: {
2649 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2650 return false;
2651 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2652 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2653 switch (UseOpc) {
2654 default: break;
2655 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2656 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2657 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2658 }
2659 break;
2660 }
2661 case ARM::t2SUBrr: {
2662 if (Commute)
2663 return false;
2664 ImmVal = -ImmVal;
2665 NewUseOpc = ARM::t2SUBri;
2666 // Fallthrough
2667 }
2668 case ARM::t2ADDrr:
2669 case ARM::t2ORRrr:
2670 case ARM::t2EORrr: {
2671 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2672 return false;
2673 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2674 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2675 switch (UseOpc) {
2676 default: break;
2677 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2678 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2679 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2680 }
2681 break;
2682 }
2683 }
2684 }
2685 }
2686
2687 unsigned OpIdx = Commute ? 2 : 1;
2688 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2689 bool isKill = UseMI->getOperand(OpIdx).isKill();
2690 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2691 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002692 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002693 get(NewUseOpc), NewReg)
2694 .addReg(Reg1, getKillRegState(isKill))
2695 .addImm(SOImmValV1)));
2696 UseMI->setDesc(get(NewUseOpc));
2697 UseMI->getOperand(1).setReg(NewReg);
2698 UseMI->getOperand(1).setIsKill();
2699 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2700 DefMI->eraseFromParent();
2701 return true;
2702}
2703
Bob Wilsone8a549c2012-09-29 21:43:49 +00002704static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2705 const MachineInstr *MI) {
2706 switch (MI->getOpcode()) {
2707 default: {
2708 const MCInstrDesc &Desc = MI->getDesc();
2709 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2710 assert(UOps >= 0 && "bad # UOps");
2711 return UOps;
2712 }
2713
2714 case ARM::LDRrs:
2715 case ARM::LDRBrs:
2716 case ARM::STRrs:
2717 case ARM::STRBrs: {
2718 unsigned ShOpVal = MI->getOperand(3).getImm();
2719 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2720 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2721 if (!isSub &&
2722 (ShImm == 0 ||
2723 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2724 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2725 return 1;
2726 return 2;
2727 }
2728
2729 case ARM::LDRH:
2730 case ARM::STRH: {
2731 if (!MI->getOperand(2).getReg())
2732 return 1;
2733
2734 unsigned ShOpVal = MI->getOperand(3).getImm();
2735 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2736 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2737 if (!isSub &&
2738 (ShImm == 0 ||
2739 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2740 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2741 return 1;
2742 return 2;
2743 }
2744
2745 case ARM::LDRSB:
2746 case ARM::LDRSH:
2747 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2748
2749 case ARM::LDRSB_POST:
2750 case ARM::LDRSH_POST: {
2751 unsigned Rt = MI->getOperand(0).getReg();
2752 unsigned Rm = MI->getOperand(3).getReg();
2753 return (Rt == Rm) ? 4 : 3;
2754 }
2755
2756 case ARM::LDR_PRE_REG:
2757 case ARM::LDRB_PRE_REG: {
2758 unsigned Rt = MI->getOperand(0).getReg();
2759 unsigned Rm = MI->getOperand(3).getReg();
2760 if (Rt == Rm)
2761 return 3;
2762 unsigned ShOpVal = MI->getOperand(4).getImm();
2763 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2764 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2765 if (!isSub &&
2766 (ShImm == 0 ||
2767 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2768 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2769 return 2;
2770 return 3;
2771 }
2772
2773 case ARM::STR_PRE_REG:
2774 case ARM::STRB_PRE_REG: {
2775 unsigned ShOpVal = MI->getOperand(4).getImm();
2776 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2777 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2778 if (!isSub &&
2779 (ShImm == 0 ||
2780 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2781 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2782 return 2;
2783 return 3;
2784 }
2785
2786 case ARM::LDRH_PRE:
2787 case ARM::STRH_PRE: {
2788 unsigned Rt = MI->getOperand(0).getReg();
2789 unsigned Rm = MI->getOperand(3).getReg();
2790 if (!Rm)
2791 return 2;
2792 if (Rt == Rm)
2793 return 3;
2794 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2795 ? 3 : 2;
2796 }
2797
2798 case ARM::LDR_POST_REG:
2799 case ARM::LDRB_POST_REG:
2800 case ARM::LDRH_POST: {
2801 unsigned Rt = MI->getOperand(0).getReg();
2802 unsigned Rm = MI->getOperand(3).getReg();
2803 return (Rt == Rm) ? 3 : 2;
2804 }
2805
2806 case ARM::LDR_PRE_IMM:
2807 case ARM::LDRB_PRE_IMM:
2808 case ARM::LDR_POST_IMM:
2809 case ARM::LDRB_POST_IMM:
2810 case ARM::STRB_POST_IMM:
2811 case ARM::STRB_POST_REG:
2812 case ARM::STRB_PRE_IMM:
2813 case ARM::STRH_POST:
2814 case ARM::STR_POST_IMM:
2815 case ARM::STR_POST_REG:
2816 case ARM::STR_PRE_IMM:
2817 return 2;
2818
2819 case ARM::LDRSB_PRE:
2820 case ARM::LDRSH_PRE: {
2821 unsigned Rm = MI->getOperand(3).getReg();
2822 if (Rm == 0)
2823 return 3;
2824 unsigned Rt = MI->getOperand(0).getReg();
2825 if (Rt == Rm)
2826 return 4;
2827 unsigned ShOpVal = MI->getOperand(4).getImm();
2828 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2829 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2830 if (!isSub &&
2831 (ShImm == 0 ||
2832 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2833 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2834 return 3;
2835 return 4;
2836 }
2837
2838 case ARM::LDRD: {
2839 unsigned Rt = MI->getOperand(0).getReg();
2840 unsigned Rn = MI->getOperand(2).getReg();
2841 unsigned Rm = MI->getOperand(3).getReg();
2842 if (Rm)
2843 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2844 return (Rt == Rn) ? 3 : 2;
2845 }
2846
2847 case ARM::STRD: {
2848 unsigned Rm = MI->getOperand(3).getReg();
2849 if (Rm)
2850 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2851 return 2;
2852 }
2853
2854 case ARM::LDRD_POST:
2855 case ARM::t2LDRD_POST:
2856 return 3;
2857
2858 case ARM::STRD_POST:
2859 case ARM::t2STRD_POST:
2860 return 4;
2861
2862 case ARM::LDRD_PRE: {
2863 unsigned Rt = MI->getOperand(0).getReg();
2864 unsigned Rn = MI->getOperand(3).getReg();
2865 unsigned Rm = MI->getOperand(4).getReg();
2866 if (Rm)
2867 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2868 return (Rt == Rn) ? 4 : 3;
2869 }
2870
2871 case ARM::t2LDRD_PRE: {
2872 unsigned Rt = MI->getOperand(0).getReg();
2873 unsigned Rn = MI->getOperand(3).getReg();
2874 return (Rt == Rn) ? 4 : 3;
2875 }
2876
2877 case ARM::STRD_PRE: {
2878 unsigned Rm = MI->getOperand(4).getReg();
2879 if (Rm)
2880 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2881 return 3;
2882 }
2883
2884 case ARM::t2STRD_PRE:
2885 return 3;
2886
2887 case ARM::t2LDR_POST:
2888 case ARM::t2LDRB_POST:
2889 case ARM::t2LDRB_PRE:
2890 case ARM::t2LDRSBi12:
2891 case ARM::t2LDRSBi8:
2892 case ARM::t2LDRSBpci:
2893 case ARM::t2LDRSBs:
2894 case ARM::t2LDRH_POST:
2895 case ARM::t2LDRH_PRE:
2896 case ARM::t2LDRSBT:
2897 case ARM::t2LDRSB_POST:
2898 case ARM::t2LDRSB_PRE:
2899 case ARM::t2LDRSH_POST:
2900 case ARM::t2LDRSH_PRE:
2901 case ARM::t2LDRSHi12:
2902 case ARM::t2LDRSHi8:
2903 case ARM::t2LDRSHpci:
2904 case ARM::t2LDRSHs:
2905 return 2;
2906
2907 case ARM::t2LDRDi8: {
2908 unsigned Rt = MI->getOperand(0).getReg();
2909 unsigned Rn = MI->getOperand(2).getReg();
2910 return (Rt == Rn) ? 3 : 2;
2911 }
2912
2913 case ARM::t2STRB_POST:
2914 case ARM::t2STRB_PRE:
2915 case ARM::t2STRBs:
2916 case ARM::t2STRDi8:
2917 case ARM::t2STRH_POST:
2918 case ARM::t2STRH_PRE:
2919 case ARM::t2STRHs:
2920 case ARM::t2STR_POST:
2921 case ARM::t2STR_PRE:
2922 case ARM::t2STRs:
2923 return 2;
2924 }
2925}
2926
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002927// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2928// can't be easily determined return 0 (missing MachineMemOperand).
2929//
2930// FIXME: The current MachineInstr design does not support relying on machine
2931// mem operands to determine the width of a memory access. Instead, we expect
2932// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00002933// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002934// two reasons:
2935//
2936// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2937// operands. This is much more dangerous than using the MachineMemOperand
2938// sizes because CodeGen passes can insert/remove optional machine operands. In
2939// fact, it's totally incorrect for preRA passes and appears to be wrong for
2940// postRA passes as well.
2941//
2942// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2943// machine model that calls this should handle the unknown (zero size) case.
2944//
2945// Long term, we should require a target hook that verifies MachineMemOperand
2946// sizes during MC lowering. That target hook should be local to MC lowering
2947// because we can't ensure that it is aware of other MI forms. Doing this will
2948// ensure that MachineMemOperands are correctly propagated through all passes.
2949unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2950 unsigned Size = 0;
2951 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2952 E = MI->memoperands_end(); I != E; ++I) {
2953 Size += (*I)->getSize();
2954 }
2955 return Size / 4;
2956}
2957
Evan Cheng367a5df2010-09-09 18:18:55 +00002958unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002959ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2960 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002961 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002962 return 1;
2963
Evan Cheng6cc775f2011-06-28 19:10:37 +00002964 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002965 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002966 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002967 if (ItinUOps >= 0) {
2968 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2969 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2970
Andrew Trickf161e392012-07-02 18:10:42 +00002971 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002972 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002973
2974 unsigned Opc = MI->getOpcode();
2975 switch (Opc) {
2976 default:
2977 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002978 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002979 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002980 return 2;
2981
2982 // The number of uOps for load / store multiple are determined by the number
2983 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002984 //
Evan Chengbf407072010-09-10 01:29:16 +00002985 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2986 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002987 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002988 //
Evan Chengbf407072010-09-10 01:29:16 +00002989 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002990 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2991 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2992 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002993 case ARM::VLDMDIA_UPD:
2994 case ARM::VLDMDDB_UPD:
2995 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002996 case ARM::VLDMSIA_UPD:
2997 case ARM::VLDMSDB_UPD:
2998 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002999 case ARM::VSTMDIA_UPD:
3000 case ARM::VSTMDDB_UPD:
3001 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003002 case ARM::VSTMSIA_UPD:
3003 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003004 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3005 return (NumRegs / 2) + (NumRegs % 2) + 1;
3006 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003007
3008 case ARM::LDMIA_RET:
3009 case ARM::LDMIA:
3010 case ARM::LDMDA:
3011 case ARM::LDMDB:
3012 case ARM::LDMIB:
3013 case ARM::LDMIA_UPD:
3014 case ARM::LDMDA_UPD:
3015 case ARM::LDMDB_UPD:
3016 case ARM::LDMIB_UPD:
3017 case ARM::STMIA:
3018 case ARM::STMDA:
3019 case ARM::STMDB:
3020 case ARM::STMIB:
3021 case ARM::STMIA_UPD:
3022 case ARM::STMDA_UPD:
3023 case ARM::STMDB_UPD:
3024 case ARM::STMIB_UPD:
3025 case ARM::tLDMIA:
3026 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003027 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003028 case ARM::tPOP_RET:
3029 case ARM::tPOP:
3030 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003031 case ARM::t2LDMIA_RET:
3032 case ARM::t2LDMIA:
3033 case ARM::t2LDMDB:
3034 case ARM::t2LDMIA_UPD:
3035 case ARM::t2LDMDB_UPD:
3036 case ARM::t2STMIA:
3037 case ARM::t2STMDB:
3038 case ARM::t2STMIA_UPD:
3039 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003040 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003041 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003042 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3043 switch (Opc) {
3044 default: break;
3045 case ARM::VLDMDIA_UPD:
3046 case ARM::VLDMDDB_UPD:
3047 case ARM::VLDMSIA_UPD:
3048 case ARM::VLDMSDB_UPD:
3049 case ARM::VSTMDIA_UPD:
3050 case ARM::VSTMDDB_UPD:
3051 case ARM::VSTMSIA_UPD:
3052 case ARM::VSTMSDB_UPD:
3053 case ARM::LDMIA_UPD:
3054 case ARM::LDMDA_UPD:
3055 case ARM::LDMDB_UPD:
3056 case ARM::LDMIB_UPD:
3057 case ARM::STMIA_UPD:
3058 case ARM::STMDA_UPD:
3059 case ARM::STMDB_UPD:
3060 case ARM::STMIB_UPD:
3061 case ARM::tLDMIA_UPD:
3062 case ARM::tSTMIA_UPD:
3063 case ARM::t2LDMIA_UPD:
3064 case ARM::t2LDMDB_UPD:
3065 case ARM::t2STMIA_UPD:
3066 case ARM::t2STMDB_UPD:
3067 ++UOps; // One for base register writeback.
3068 break;
3069 case ARM::LDMIA_RET:
3070 case ARM::tPOP_RET:
3071 case ARM::t2LDMIA_RET:
3072 UOps += 2; // One for base reg wb, one for write to pc.
3073 break;
3074 }
3075 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003076 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003077 if (NumRegs < 4)
3078 return 2;
3079 // 4 registers would be issued: 2, 2.
3080 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003081 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003082 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003083 ++A8UOps;
3084 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003085 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003086 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003087 // If there are odd number of registers or if it's not 64-bit aligned,
3088 // then it takes an extra AGU (Address Generation Unit) cycle.
3089 if ((NumRegs % 2) ||
3090 !MI->hasOneMemOperand() ||
3091 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003092 ++A9UOps;
3093 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003094 } else {
3095 // Assume the worst.
3096 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003097 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003098 }
3099 }
3100}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003101
3102int
Evan Cheng412e37b2010-10-07 23:12:15 +00003103ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003104 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003105 unsigned DefClass,
3106 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003107 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003108 if (RegNo <= 0)
3109 // Def is the address writeback.
3110 return ItinData->getOperandCycle(DefClass, DefIdx);
3111
3112 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003113 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003114 // (regno / 2) + (regno % 2) + 1
3115 DefCycle = RegNo / 2 + 1;
3116 if (RegNo % 2)
3117 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003118 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003119 DefCycle = RegNo;
3120 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003121
Evan Cheng6cc775f2011-06-28 19:10:37 +00003122 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003123 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003124 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003125 case ARM::VLDMSIA_UPD:
3126 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003127 isSLoad = true;
3128 break;
3129 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003130
Evan Cheng412e37b2010-10-07 23:12:15 +00003131 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3132 // then it takes an extra cycle.
3133 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3134 ++DefCycle;
3135 } else {
3136 // Assume the worst.
3137 DefCycle = RegNo + 2;
3138 }
3139
3140 return DefCycle;
3141}
3142
3143int
3144ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003145 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003146 unsigned DefClass,
3147 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003148 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003149 if (RegNo <= 0)
3150 // Def is the address writeback.
3151 return ItinData->getOperandCycle(DefClass, DefIdx);
3152
3153 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003154 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003155 // 4 registers would be issued: 1, 2, 1.
3156 // 5 registers would be issued: 1, 2, 2.
3157 DefCycle = RegNo / 2;
3158 if (DefCycle < 1)
3159 DefCycle = 1;
3160 // Result latency is issue cycle + 2: E2.
3161 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003162 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003163 DefCycle = (RegNo / 2);
3164 // If there are odd number of registers or if it's not 64-bit aligned,
3165 // then it takes an extra AGU (Address Generation Unit) cycle.
3166 if ((RegNo % 2) || DefAlign < 8)
3167 ++DefCycle;
3168 // Result latency is AGU cycles + 2.
3169 DefCycle += 2;
3170 } else {
3171 // Assume the worst.
3172 DefCycle = RegNo + 2;
3173 }
3174
3175 return DefCycle;
3176}
3177
3178int
3179ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003180 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003181 unsigned UseClass,
3182 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003183 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003184 if (RegNo <= 0)
3185 return ItinData->getOperandCycle(UseClass, UseIdx);
3186
3187 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003188 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003189 // (regno / 2) + (regno % 2) + 1
3190 UseCycle = RegNo / 2 + 1;
3191 if (RegNo % 2)
3192 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003193 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003194 UseCycle = RegNo;
3195 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003196
Evan Cheng6cc775f2011-06-28 19:10:37 +00003197 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003198 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003199 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003200 case ARM::VSTMSIA_UPD:
3201 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003202 isSStore = true;
3203 break;
3204 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003205
Evan Cheng412e37b2010-10-07 23:12:15 +00003206 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3207 // then it takes an extra cycle.
3208 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3209 ++UseCycle;
3210 } else {
3211 // Assume the worst.
3212 UseCycle = RegNo + 2;
3213 }
3214
3215 return UseCycle;
3216}
3217
3218int
3219ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003220 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003221 unsigned UseClass,
3222 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003223 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003224 if (RegNo <= 0)
3225 return ItinData->getOperandCycle(UseClass, UseIdx);
3226
3227 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003228 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003229 UseCycle = RegNo / 2;
3230 if (UseCycle < 2)
3231 UseCycle = 2;
3232 // Read in E3.
3233 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003234 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003235 UseCycle = (RegNo / 2);
3236 // If there are odd number of registers or if it's not 64-bit aligned,
3237 // then it takes an extra AGU (Address Generation Unit) cycle.
3238 if ((RegNo % 2) || UseAlign < 8)
3239 ++UseCycle;
3240 } else {
3241 // Assume the worst.
3242 UseCycle = 1;
3243 }
3244 return UseCycle;
3245}
3246
3247int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003248ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003249 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003250 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003251 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003252 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003253 unsigned DefClass = DefMCID.getSchedClass();
3254 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003255
Evan Cheng6cc775f2011-06-28 19:10:37 +00003256 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003257 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3258
3259 // This may be a def / use of a variable_ops instruction, the operand
3260 // latency might be determinable dynamically. Let the target try to
3261 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003262 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003263 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003264 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003265 default:
3266 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3267 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003268
3269 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003270 case ARM::VLDMDIA_UPD:
3271 case ARM::VLDMDDB_UPD:
3272 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003273 case ARM::VLDMSIA_UPD:
3274 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003275 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003276 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003277
3278 case ARM::LDMIA_RET:
3279 case ARM::LDMIA:
3280 case ARM::LDMDA:
3281 case ARM::LDMDB:
3282 case ARM::LDMIB:
3283 case ARM::LDMIA_UPD:
3284 case ARM::LDMDA_UPD:
3285 case ARM::LDMDB_UPD:
3286 case ARM::LDMIB_UPD:
3287 case ARM::tLDMIA:
3288 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003289 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003290 case ARM::t2LDMIA_RET:
3291 case ARM::t2LDMIA:
3292 case ARM::t2LDMDB:
3293 case ARM::t2LDMIA_UPD:
3294 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003295 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003296 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003297 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003298 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003299
3300 if (DefCycle == -1)
3301 // We can't seem to determine the result latency of the def, assume it's 2.
3302 DefCycle = 2;
3303
3304 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003305 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003306 default:
3307 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3308 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003309
3310 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003311 case ARM::VSTMDIA_UPD:
3312 case ARM::VSTMDDB_UPD:
3313 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003314 case ARM::VSTMSIA_UPD:
3315 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003316 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003317 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003318
3319 case ARM::STMIA:
3320 case ARM::STMDA:
3321 case ARM::STMDB:
3322 case ARM::STMIB:
3323 case ARM::STMIA_UPD:
3324 case ARM::STMDA_UPD:
3325 case ARM::STMDB_UPD:
3326 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003327 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003328 case ARM::tPOP_RET:
3329 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003330 case ARM::t2STMIA:
3331 case ARM::t2STMDB:
3332 case ARM::t2STMIA_UPD:
3333 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003334 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003335 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003336 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003337
3338 if (UseCycle == -1)
3339 // Assume it's read in the first stage.
3340 UseCycle = 1;
3341
3342 UseCycle = DefCycle - UseCycle + 1;
3343 if (UseCycle > 0) {
3344 if (LdmBypass) {
3345 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3346 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003347 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003348 UseClass, UseIdx))
3349 --UseCycle;
3350 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003351 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003352 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003353 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003354 }
3355
3356 return UseCycle;
3357}
3358
Evan Cheng7fae11b2011-12-14 02:11:42 +00003359static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003360 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003361 unsigned &DefIdx, unsigned &Dist) {
3362 Dist = 0;
3363
3364 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003365 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003366 assert(II->isInsideBundle() && "Empty bundle?");
3367
3368 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003369 while (II->isInsideBundle()) {
3370 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3371 if (Idx != -1)
3372 break;
3373 --II;
3374 ++Dist;
3375 }
3376
3377 assert(Idx != -1 && "Cannot find bundled definition!");
3378 DefIdx = Idx;
3379 return II;
3380}
3381
3382static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003383 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003384 unsigned &UseIdx, unsigned &Dist) {
3385 Dist = 0;
3386
3387 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3388 assert(II->isInsideBundle() && "Empty bundle?");
3389 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3390
3391 // FIXME: This doesn't properly handle multiple uses.
3392 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003393 while (II != E && II->isInsideBundle()) {
3394 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3395 if (Idx != -1)
3396 break;
3397 if (II->getOpcode() != ARM::t2IT)
3398 ++Dist;
3399 ++II;
3400 }
3401
Evan Chengda103bf2011-12-14 20:00:08 +00003402 if (Idx == -1) {
3403 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003404 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003405 }
3406
Evan Cheng7fae11b2011-12-14 02:11:42 +00003407 UseIdx = Idx;
3408 return II;
3409}
3410
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003411/// Return the number of cycles to add to (or subtract from) the static
3412/// itinerary based on the def opcode and alignment. The caller will ensure that
3413/// adjusted latency is at least one cycle.
3414static int adjustDefLatency(const ARMSubtarget &Subtarget,
3415 const MachineInstr *DefMI,
3416 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3417 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003418 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003419 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3420 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003421 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003422 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003423 case ARM::LDRrs:
3424 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003425 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3426 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3427 if (ShImm == 0 ||
3428 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003429 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003430 break;
3431 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003432 case ARM::t2LDRs:
3433 case ARM::t2LDRBs:
3434 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003435 case ARM::t2LDRSHs: {
3436 // Thumb2 mode: lsl only.
3437 unsigned ShAmt = DefMI->getOperand(3).getImm();
3438 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003439 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003440 break;
3441 }
3442 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003443 } else if (Subtarget.isSwift()) {
3444 // FIXME: Properly handle all of the latency adjustments for address
3445 // writeback.
3446 switch (DefMCID->getOpcode()) {
3447 default: break;
3448 case ARM::LDRrs:
3449 case ARM::LDRBrs: {
3450 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3451 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3452 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3453 if (!isSub &&
3454 (ShImm == 0 ||
3455 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3456 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3457 Adjust -= 2;
3458 else if (!isSub &&
3459 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3460 --Adjust;
3461 break;
3462 }
3463 case ARM::t2LDRs:
3464 case ARM::t2LDRBs:
3465 case ARM::t2LDRHs:
3466 case ARM::t2LDRSHs: {
3467 // Thumb2 mode: lsl only.
3468 unsigned ShAmt = DefMI->getOperand(3).getImm();
3469 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3470 Adjust -= 2;
3471 break;
3472 }
3473 }
Evan Chengff310732010-10-28 06:47:08 +00003474 }
3475
Silviu Barangab47bb942012-09-13 15:05:10 +00003476 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003477 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003478 default: break;
3479 case ARM::VLD1q8:
3480 case ARM::VLD1q16:
3481 case ARM::VLD1q32:
3482 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003483 case ARM::VLD1q8wb_fixed:
3484 case ARM::VLD1q16wb_fixed:
3485 case ARM::VLD1q32wb_fixed:
3486 case ARM::VLD1q64wb_fixed:
3487 case ARM::VLD1q8wb_register:
3488 case ARM::VLD1q16wb_register:
3489 case ARM::VLD1q32wb_register:
3490 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003491 case ARM::VLD2d8:
3492 case ARM::VLD2d16:
3493 case ARM::VLD2d32:
3494 case ARM::VLD2q8:
3495 case ARM::VLD2q16:
3496 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003497 case ARM::VLD2d8wb_fixed:
3498 case ARM::VLD2d16wb_fixed:
3499 case ARM::VLD2d32wb_fixed:
3500 case ARM::VLD2q8wb_fixed:
3501 case ARM::VLD2q16wb_fixed:
3502 case ARM::VLD2q32wb_fixed:
3503 case ARM::VLD2d8wb_register:
3504 case ARM::VLD2d16wb_register:
3505 case ARM::VLD2d32wb_register:
3506 case ARM::VLD2q8wb_register:
3507 case ARM::VLD2q16wb_register:
3508 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003509 case ARM::VLD3d8:
3510 case ARM::VLD3d16:
3511 case ARM::VLD3d32:
3512 case ARM::VLD1d64T:
3513 case ARM::VLD3d8_UPD:
3514 case ARM::VLD3d16_UPD:
3515 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003516 case ARM::VLD1d64Twb_fixed:
3517 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003518 case ARM::VLD3q8_UPD:
3519 case ARM::VLD3q16_UPD:
3520 case ARM::VLD3q32_UPD:
3521 case ARM::VLD4d8:
3522 case ARM::VLD4d16:
3523 case ARM::VLD4d32:
3524 case ARM::VLD1d64Q:
3525 case ARM::VLD4d8_UPD:
3526 case ARM::VLD4d16_UPD:
3527 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003528 case ARM::VLD1d64Qwb_fixed:
3529 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003530 case ARM::VLD4q8_UPD:
3531 case ARM::VLD4q16_UPD:
3532 case ARM::VLD4q32_UPD:
3533 case ARM::VLD1DUPq8:
3534 case ARM::VLD1DUPq16:
3535 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003536 case ARM::VLD1DUPq8wb_fixed:
3537 case ARM::VLD1DUPq16wb_fixed:
3538 case ARM::VLD1DUPq32wb_fixed:
3539 case ARM::VLD1DUPq8wb_register:
3540 case ARM::VLD1DUPq16wb_register:
3541 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003542 case ARM::VLD2DUPd8:
3543 case ARM::VLD2DUPd16:
3544 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003545 case ARM::VLD2DUPd8wb_fixed:
3546 case ARM::VLD2DUPd16wb_fixed:
3547 case ARM::VLD2DUPd32wb_fixed:
3548 case ARM::VLD2DUPd8wb_register:
3549 case ARM::VLD2DUPd16wb_register:
3550 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003551 case ARM::VLD4DUPd8:
3552 case ARM::VLD4DUPd16:
3553 case ARM::VLD4DUPd32:
3554 case ARM::VLD4DUPd8_UPD:
3555 case ARM::VLD4DUPd16_UPD:
3556 case ARM::VLD4DUPd32_UPD:
3557 case ARM::VLD1LNd8:
3558 case ARM::VLD1LNd16:
3559 case ARM::VLD1LNd32:
3560 case ARM::VLD1LNd8_UPD:
3561 case ARM::VLD1LNd16_UPD:
3562 case ARM::VLD1LNd32_UPD:
3563 case ARM::VLD2LNd8:
3564 case ARM::VLD2LNd16:
3565 case ARM::VLD2LNd32:
3566 case ARM::VLD2LNq16:
3567 case ARM::VLD2LNq32:
3568 case ARM::VLD2LNd8_UPD:
3569 case ARM::VLD2LNd16_UPD:
3570 case ARM::VLD2LNd32_UPD:
3571 case ARM::VLD2LNq16_UPD:
3572 case ARM::VLD2LNq32_UPD:
3573 case ARM::VLD4LNd8:
3574 case ARM::VLD4LNd16:
3575 case ARM::VLD4LNd32:
3576 case ARM::VLD4LNq16:
3577 case ARM::VLD4LNq32:
3578 case ARM::VLD4LNd8_UPD:
3579 case ARM::VLD4LNd16_UPD:
3580 case ARM::VLD4LNd32_UPD:
3581 case ARM::VLD4LNq16_UPD:
3582 case ARM::VLD4LNq32_UPD:
3583 // If the address is not 64-bit aligned, the latencies of these
3584 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003585 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003586 break;
3587 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003588 }
3589 return Adjust;
3590}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003591
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003592
3593
3594int
3595ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3596 const MachineInstr *DefMI, unsigned DefIdx,
3597 const MachineInstr *UseMI,
3598 unsigned UseIdx) const {
3599 // No operand latency. The caller may fall back to getInstrLatency.
3600 if (!ItinData || ItinData->isEmpty())
3601 return -1;
3602
3603 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3604 unsigned Reg = DefMO.getReg();
3605 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3606 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3607
3608 unsigned DefAdj = 0;
3609 if (DefMI->isBundle()) {
3610 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3611 DefMCID = &DefMI->getDesc();
3612 }
3613 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3614 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3615 return 1;
3616 }
3617
3618 unsigned UseAdj = 0;
3619 if (UseMI->isBundle()) {
3620 unsigned NewUseIdx;
3621 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3622 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003623 if (!NewUseMI)
3624 return -1;
3625
3626 UseMI = NewUseMI;
3627 UseIdx = NewUseIdx;
3628 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003629 }
3630
3631 if (Reg == ARM::CPSR) {
3632 if (DefMI->getOpcode() == ARM::FMSTAT) {
3633 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003634 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003635 }
3636
3637 // CPSR set and branch can be paired in the same cycle.
3638 if (UseMI->isBranch())
3639 return 0;
3640
3641 // Otherwise it takes the instruction latency (generally one).
3642 unsigned Latency = getInstrLatency(ItinData, DefMI);
3643
3644 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3645 // its uses. Instructions which are otherwise scheduled between them may
3646 // incur a code size penalty (not able to use the CPSR setting 16-bit
3647 // instructions).
3648 if (Latency > 0 && Subtarget.isThumb2()) {
3649 const MachineFunction *MF = DefMI->getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003650 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003651 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003652 --Latency;
3653 }
3654 return Latency;
3655 }
3656
Andrew Trick77d0b882012-06-22 02:50:33 +00003657 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3658 return -1;
3659
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003660 unsigned DefAlign = DefMI->hasOneMemOperand()
3661 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3662 unsigned UseAlign = UseMI->hasOneMemOperand()
3663 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3664
3665 // Get the itinerary's latency if possible, and handle variable_ops.
3666 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3667 *UseMCID, UseIdx, UseAlign);
3668 // Unable to find operand latency. The caller may resort to getInstrLatency.
3669 if (Latency < 0)
3670 return Latency;
3671
3672 // Adjust for IT block position.
3673 int Adj = DefAdj + UseAdj;
3674
3675 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3676 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3677 if (Adj >= 0 || (int)Latency > -Adj) {
3678 return Latency + Adj;
3679 }
3680 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003681 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003682}
3683
3684int
3685ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3686 SDNode *DefNode, unsigned DefIdx,
3687 SDNode *UseNode, unsigned UseIdx) const {
3688 if (!DefNode->isMachineOpcode())
3689 return 1;
3690
Evan Cheng6cc775f2011-06-28 19:10:37 +00003691 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003692
Evan Cheng6cc775f2011-06-28 19:10:37 +00003693 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003694 return 0;
3695
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003696 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003697 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003698
Evan Cheng6c1414f2010-10-29 18:09:28 +00003699 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003700 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003701 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003702 return Latency <= 2 ? 1 : Latency - 1;
3703 else
3704 return Latency <= 3 ? 1 : Latency - 2;
3705 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003706
Evan Cheng6cc775f2011-06-28 19:10:37 +00003707 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003708 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3709 unsigned DefAlign = !DefMN->memoperands_empty()
3710 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3711 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3712 unsigned UseAlign = !UseMN->memoperands_empty()
3713 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003714 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3715 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003716
3717 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003718 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3719 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003720 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3721 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003722 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003723 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003724 case ARM::LDRrs:
3725 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003726 unsigned ShOpVal =
3727 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3728 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3729 if (ShImm == 0 ||
3730 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3731 --Latency;
3732 break;
3733 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003734 case ARM::t2LDRs:
3735 case ARM::t2LDRBs:
3736 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003737 case ARM::t2LDRSHs: {
3738 // Thumb2 mode: lsl only.
3739 unsigned ShAmt =
3740 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3741 if (ShAmt == 0 || ShAmt == 2)
3742 --Latency;
3743 break;
3744 }
3745 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003746 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3747 // FIXME: Properly handle all of the latency adjustments for address
3748 // writeback.
3749 switch (DefMCID.getOpcode()) {
3750 default: break;
3751 case ARM::LDRrs:
3752 case ARM::LDRBrs: {
3753 unsigned ShOpVal =
3754 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3755 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3756 if (ShImm == 0 ||
3757 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3758 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3759 Latency -= 2;
3760 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3761 --Latency;
3762 break;
3763 }
3764 case ARM::t2LDRs:
3765 case ARM::t2LDRBs:
3766 case ARM::t2LDRHs:
3767 case ARM::t2LDRSHs: {
3768 // Thumb2 mode: lsl 0-3 only.
3769 Latency -= 2;
3770 break;
3771 }
3772 }
Evan Chengff310732010-10-28 06:47:08 +00003773 }
3774
Silviu Barangab47bb942012-09-13 15:05:10 +00003775 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003776 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003777 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003778 case ARM::VLD1q8:
3779 case ARM::VLD1q16:
3780 case ARM::VLD1q32:
3781 case ARM::VLD1q64:
3782 case ARM::VLD1q8wb_register:
3783 case ARM::VLD1q16wb_register:
3784 case ARM::VLD1q32wb_register:
3785 case ARM::VLD1q64wb_register:
3786 case ARM::VLD1q8wb_fixed:
3787 case ARM::VLD1q16wb_fixed:
3788 case ARM::VLD1q32wb_fixed:
3789 case ARM::VLD1q64wb_fixed:
3790 case ARM::VLD2d8:
3791 case ARM::VLD2d16:
3792 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003793 case ARM::VLD2q8Pseudo:
3794 case ARM::VLD2q16Pseudo:
3795 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003796 case ARM::VLD2d8wb_fixed:
3797 case ARM::VLD2d16wb_fixed:
3798 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003799 case ARM::VLD2q8PseudoWB_fixed:
3800 case ARM::VLD2q16PseudoWB_fixed:
3801 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003802 case ARM::VLD2d8wb_register:
3803 case ARM::VLD2d16wb_register:
3804 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003805 case ARM::VLD2q8PseudoWB_register:
3806 case ARM::VLD2q16PseudoWB_register:
3807 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003808 case ARM::VLD3d8Pseudo:
3809 case ARM::VLD3d16Pseudo:
3810 case ARM::VLD3d32Pseudo:
3811 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003812 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003813 case ARM::VLD3d8Pseudo_UPD:
3814 case ARM::VLD3d16Pseudo_UPD:
3815 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003816 case ARM::VLD3q8Pseudo_UPD:
3817 case ARM::VLD3q16Pseudo_UPD:
3818 case ARM::VLD3q32Pseudo_UPD:
3819 case ARM::VLD3q8oddPseudo:
3820 case ARM::VLD3q16oddPseudo:
3821 case ARM::VLD3q32oddPseudo:
3822 case ARM::VLD3q8oddPseudo_UPD:
3823 case ARM::VLD3q16oddPseudo_UPD:
3824 case ARM::VLD3q32oddPseudo_UPD:
3825 case ARM::VLD4d8Pseudo:
3826 case ARM::VLD4d16Pseudo:
3827 case ARM::VLD4d32Pseudo:
3828 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003829 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003830 case ARM::VLD4d8Pseudo_UPD:
3831 case ARM::VLD4d16Pseudo_UPD:
3832 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003833 case ARM::VLD4q8Pseudo_UPD:
3834 case ARM::VLD4q16Pseudo_UPD:
3835 case ARM::VLD4q32Pseudo_UPD:
3836 case ARM::VLD4q8oddPseudo:
3837 case ARM::VLD4q16oddPseudo:
3838 case ARM::VLD4q32oddPseudo:
3839 case ARM::VLD4q8oddPseudo_UPD:
3840 case ARM::VLD4q16oddPseudo_UPD:
3841 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003842 case ARM::VLD1DUPq8:
3843 case ARM::VLD1DUPq16:
3844 case ARM::VLD1DUPq32:
3845 case ARM::VLD1DUPq8wb_fixed:
3846 case ARM::VLD1DUPq16wb_fixed:
3847 case ARM::VLD1DUPq32wb_fixed:
3848 case ARM::VLD1DUPq8wb_register:
3849 case ARM::VLD1DUPq16wb_register:
3850 case ARM::VLD1DUPq32wb_register:
3851 case ARM::VLD2DUPd8:
3852 case ARM::VLD2DUPd16:
3853 case ARM::VLD2DUPd32:
3854 case ARM::VLD2DUPd8wb_fixed:
3855 case ARM::VLD2DUPd16wb_fixed:
3856 case ARM::VLD2DUPd32wb_fixed:
3857 case ARM::VLD2DUPd8wb_register:
3858 case ARM::VLD2DUPd16wb_register:
3859 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003860 case ARM::VLD4DUPd8Pseudo:
3861 case ARM::VLD4DUPd16Pseudo:
3862 case ARM::VLD4DUPd32Pseudo:
3863 case ARM::VLD4DUPd8Pseudo_UPD:
3864 case ARM::VLD4DUPd16Pseudo_UPD:
3865 case ARM::VLD4DUPd32Pseudo_UPD:
3866 case ARM::VLD1LNq8Pseudo:
3867 case ARM::VLD1LNq16Pseudo:
3868 case ARM::VLD1LNq32Pseudo:
3869 case ARM::VLD1LNq8Pseudo_UPD:
3870 case ARM::VLD1LNq16Pseudo_UPD:
3871 case ARM::VLD1LNq32Pseudo_UPD:
3872 case ARM::VLD2LNd8Pseudo:
3873 case ARM::VLD2LNd16Pseudo:
3874 case ARM::VLD2LNd32Pseudo:
3875 case ARM::VLD2LNq16Pseudo:
3876 case ARM::VLD2LNq32Pseudo:
3877 case ARM::VLD2LNd8Pseudo_UPD:
3878 case ARM::VLD2LNd16Pseudo_UPD:
3879 case ARM::VLD2LNd32Pseudo_UPD:
3880 case ARM::VLD2LNq16Pseudo_UPD:
3881 case ARM::VLD2LNq32Pseudo_UPD:
3882 case ARM::VLD4LNd8Pseudo:
3883 case ARM::VLD4LNd16Pseudo:
3884 case ARM::VLD4LNd32Pseudo:
3885 case ARM::VLD4LNq16Pseudo:
3886 case ARM::VLD4LNq32Pseudo:
3887 case ARM::VLD4LNd8Pseudo_UPD:
3888 case ARM::VLD4LNd16Pseudo_UPD:
3889 case ARM::VLD4LNd32Pseudo_UPD:
3890 case ARM::VLD4LNq16Pseudo_UPD:
3891 case ARM::VLD4LNq32Pseudo_UPD:
3892 // If the address is not 64-bit aligned, the latencies of these
3893 // instructions increases by one.
3894 ++Latency;
3895 break;
3896 }
3897
Evan Chengff310732010-10-28 06:47:08 +00003898 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003899}
Evan Cheng63c76082010-10-19 18:58:51 +00003900
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003901unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3902 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3903 MI->isRegSequence() || MI->isImplicitDef())
3904 return 0;
3905
3906 if (MI->isBundle())
3907 return 0;
3908
3909 const MCInstrDesc &MCID = MI->getDesc();
3910
3911 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3912 // When predicated, CPSR is an additional source operand for CPSR updating
3913 // instructions, this apparently increases their latencies.
3914 return 1;
3915 }
3916 return 0;
3917}
3918
Andrew Trick45446062012-06-05 21:11:27 +00003919unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3920 const MachineInstr *MI,
3921 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003922 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3923 MI->isRegSequence() || MI->isImplicitDef())
3924 return 1;
3925
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003926 // An instruction scheduler typically runs on unbundled instructions, however
3927 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003928 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003929 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003930 MachineBasicBlock::const_instr_iterator I = MI;
3931 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3932 while (++I != E && I->isInsideBundle()) {
3933 if (I->getOpcode() != ARM::t2IT)
3934 Latency += getInstrLatency(ItinData, I, PredCost);
3935 }
3936 return Latency;
3937 }
3938
Evan Cheng6cc775f2011-06-28 19:10:37 +00003939 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003940 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003941 // When predicated, CPSR is an additional source operand for CPSR updating
3942 // instructions, this apparently increases their latencies.
3943 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003944 }
3945 // Be sure to call getStageLatency for an empty itinerary in case it has a
3946 // valid MinLatency property.
3947 if (!ItinData)
3948 return MI->mayLoad() ? 3 : 1;
3949
3950 unsigned Class = MCID.getSchedClass();
3951
3952 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003953 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003954 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003955
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003956 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003957 unsigned Latency = ItinData->getStageLatency(Class);
3958
3959 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3960 unsigned DefAlign = MI->hasOneMemOperand()
3961 ? (*MI->memoperands_begin())->getAlignment() : 0;
3962 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3963 if (Adj >= 0 || (int)Latency > -Adj) {
3964 return Latency + Adj;
3965 }
3966 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003967}
3968
3969int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3970 SDNode *Node) const {
3971 if (!Node->isMachineOpcode())
3972 return 1;
3973
3974 if (!ItinData || ItinData->isEmpty())
3975 return 1;
3976
3977 unsigned Opcode = Node->getMachineOpcode();
3978 switch (Opcode) {
3979 default:
3980 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003981 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003982 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003983 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003984 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003985}
3986
Evan Cheng63c76082010-10-19 18:58:51 +00003987bool ARMBaseInstrInfo::
Matthias Braun88e21312015-06-13 03:42:11 +00003988hasHighOperandLatency(const TargetSchedModel &SchedModel,
Evan Cheng63c76082010-10-19 18:58:51 +00003989 const MachineRegisterInfo *MRI,
3990 const MachineInstr *DefMI, unsigned DefIdx,
3991 const MachineInstr *UseMI, unsigned UseIdx) const {
3992 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3993 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3994 if (Subtarget.isCortexA8() &&
3995 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3996 // CortexA8 VFP instructions are not pipelined.
3997 return true;
3998
3999 // Hoist VFP / NEON instructions with 4 or higher latency.
Matthias Braun88e21312015-06-13 03:42:11 +00004000 unsigned Latency
4001 = SchedModel.computeOperandLatency(DefMI, DefIdx, UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004002 if (Latency <= 3)
4003 return false;
4004 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4005 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4006}
Evan Chenge96b8d72010-10-26 02:08:50 +00004007
4008bool ARMBaseInstrInfo::
Matthias Braun88e21312015-06-13 03:42:11 +00004009hasLowDefLatency(const TargetSchedModel &SchedModel,
Evan Chenge96b8d72010-10-26 02:08:50 +00004010 const MachineInstr *DefMI, unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004011 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004012 if (!ItinData || ItinData->isEmpty())
4013 return false;
4014
4015 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4016 if (DDomain == ARMII::DomainGeneral) {
4017 unsigned DefClass = DefMI->getDesc().getSchedClass();
4018 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4019 return (DefCycle != -1 && DefCycle <= 2);
4020 }
4021 return false;
4022}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004023
Andrew Trick924123a2011-09-21 02:20:46 +00004024bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4025 StringRef &ErrInfo) const {
4026 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4027 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4028 return false;
4029 }
4030 return true;
4031}
4032
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004033// LoadStackGuard has so far only been implemented for MachO. Different code
4034// sequence is needed for other targets.
4035void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4036 unsigned LoadImmOpc,
4037 unsigned LoadOpc,
4038 Reloc::Model RM) const {
4039 MachineBasicBlock &MBB = *MI->getParent();
4040 DebugLoc DL = MI->getDebugLoc();
4041 unsigned Reg = MI->getOperand(0).getReg();
4042 const GlobalValue *GV =
4043 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4044 MachineInstrBuilder MIB;
4045
4046 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4047 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4048
4049 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4050 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4051 MIB.addReg(Reg, RegState::Kill).addImm(0);
4052 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004053 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
4054 MachinePointerInfo::getGOT(*MBB.getParent()), Flag, 4, 4);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004055 MIB.addMemOperand(MMO);
4056 AddDefaultPred(MIB);
4057 }
4058
4059 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4060 MIB.addReg(Reg, RegState::Kill).addImm(0);
4061 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4062 AddDefaultPred(MIB);
4063}
4064
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004065bool
4066ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4067 unsigned &AddSubOpc,
4068 bool &NegAcc, bool &HasLane) const {
4069 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4070 if (I == MLxEntryMap.end())
4071 return false;
4072
4073 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4074 MulOpc = Entry.MulOpc;
4075 AddSubOpc = Entry.AddSubOpc;
4076 NegAcc = Entry.NegAcc;
4077 HasLane = Entry.HasLane;
4078 return true;
4079}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004080
4081//===----------------------------------------------------------------------===//
4082// Execution domains.
4083//===----------------------------------------------------------------------===//
4084//
4085// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4086// and some can go down both. The vmov instructions go down the VFP pipeline,
4087// but they can be changed to vorr equivalents that are executed by the NEON
4088// pipeline.
4089//
4090// We use the following execution domain numbering:
4091//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004092enum ARMExeDomain {
4093 ExeGeneric = 0,
4094 ExeVFP = 1,
4095 ExeNEON = 2
4096};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004097//
4098// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4099//
4100std::pair<uint16_t, uint16_t>
4101ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004102 // If we don't have access to NEON instructions then we won't be able
4103 // to swizzle anything to the NEON domain. Check to make sure.
4104 if (Subtarget.hasNEON()) {
4105 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4106 // if they are not predicated.
4107 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
4108 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004109
Eric Christopher7e70aba2015-03-07 00:12:22 +00004110 // CortexA9 is particularly picky about mixing the two and wants these
4111 // converted.
4112 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
4113 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4114 MI->getOpcode() == ARM::VMOVS))
4115 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4116 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004117 // No other instructions can be swizzled, so just determine their domain.
4118 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4119
4120 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004121 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004122
4123 // Certain instructions can go either way on Cortex-A8.
4124 // Treat them as NEON instructions.
4125 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004126 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004127
4128 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004129 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004130
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004131 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004132}
4133
Tim Northover771f1602012-08-29 16:36:07 +00004134static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4135 unsigned SReg, unsigned &Lane) {
4136 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4137 Lane = 0;
4138
4139 if (DReg != ARM::NoRegister)
4140 return DReg;
4141
4142 Lane = 1;
4143 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4144
4145 assert(DReg && "S-register with no D super-register?");
4146 return DReg;
4147}
4148
Andrew Trickd9296ec2012-10-10 05:43:01 +00004149/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004150/// set ImplicitSReg to a register number that must be marked as implicit-use or
4151/// zero if no register needs to be defined as implicit-use.
4152///
4153/// If the function cannot determine if an SPR should be marked implicit use or
4154/// not, it returns false.
4155///
4156/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004157/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004158/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4159/// lane of the DPR).
4160///
4161/// If the other SPR is defined, an implicit-use of it should be added. Else,
4162/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004163///
James Molloyea052562012-09-18 08:31:15 +00004164static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4165 MachineInstr *MI,
4166 unsigned DReg, unsigned Lane,
4167 unsigned &ImplicitSReg) {
4168 // If the DPR is defined or used already, the other SPR lane will be chained
4169 // correctly, so there is nothing to be done.
4170 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4171 ImplicitSReg = 0;
4172 return true;
4173 }
4174
4175 // Otherwise we need to go searching to see if the SPR is set explicitly.
4176 ImplicitSReg = TRI->getSubReg(DReg,
4177 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4178 MachineBasicBlock::LivenessQueryResult LQR =
4179 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4180
4181 if (LQR == MachineBasicBlock::LQR_Live)
4182 return true;
4183 else if (LQR == MachineBasicBlock::LQR_Unknown)
4184 return false;
4185
4186 // If the register is known not to be live, there is no need to add an
4187 // implicit-use.
4188 ImplicitSReg = 0;
4189 return true;
4190}
Tim Northover771f1602012-08-29 16:36:07 +00004191
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004192void
4193ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004194 unsigned DstReg, SrcReg, DReg;
4195 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004196 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004197 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004198 switch (MI->getOpcode()) {
4199 default:
4200 llvm_unreachable("cannot handle opcode!");
4201 break;
4202 case ARM::VMOVD:
4203 if (Domain != ExeNEON)
4204 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004205
Tim Northoverf6618152012-08-17 11:32:52 +00004206 // Zap the predicate operands.
4207 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004208
Eric Christopher7e70aba2015-03-07 00:12:22 +00004209 // Make sure we've got NEON instructions.
4210 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4211
Tim Northover771f1602012-08-29 16:36:07 +00004212 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4213 DstReg = MI->getOperand(0).getReg();
4214 SrcReg = MI->getOperand(1).getReg();
4215
4216 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4217 MI->RemoveOperand(i-1);
4218
4219 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004220 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004221 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4222 .addReg(SrcReg)
4223 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004224 break;
4225 case ARM::VMOVRS:
4226 if (Domain != ExeNEON)
4227 break;
4228 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4229
Tim Northover771f1602012-08-29 16:36:07 +00004230 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004231 DstReg = MI->getOperand(0).getReg();
4232 SrcReg = MI->getOperand(1).getReg();
4233
Tim Northover771f1602012-08-29 16:36:07 +00004234 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4235 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004236
Tim Northover771f1602012-08-29 16:36:07 +00004237 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004238
Tim Northover771f1602012-08-29 16:36:07 +00004239 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4240 // Note that DSrc has been widened and the other lane may be undef, which
4241 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004242 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004243 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4244 .addReg(DReg, RegState::Undef)
4245 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004246
Tim Northover771f1602012-08-29 16:36:07 +00004247 // The old source should be an implicit use, otherwise we might think it
4248 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004249 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004250 break;
James Molloyea052562012-09-18 08:31:15 +00004251 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004252 if (Domain != ExeNEON)
4253 break;
4254 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4255
Tim Northover771f1602012-08-29 16:36:07 +00004256 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004257 DstReg = MI->getOperand(0).getReg();
4258 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004259
Tim Northover771f1602012-08-29 16:36:07 +00004260 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4261
James Molloyea052562012-09-18 08:31:15 +00004262 unsigned ImplicitSReg;
4263 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4264 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004265
Tim Northoverc8d867d2012-09-05 18:37:53 +00004266 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4267 MI->RemoveOperand(i-1);
4268
Tim Northover771f1602012-08-29 16:36:07 +00004269 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4270 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004271 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004272 MIB.addReg(DReg, RegState::Define)
4273 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4274 .addReg(SrcReg)
4275 .addImm(Lane);
4276 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004277
Tim Northover726d32c2012-09-01 18:07:29 +00004278 // The narrower destination must be marked as set to keep previous chains
4279 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004280 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004281 if (ImplicitSReg != 0)
4282 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004283 break;
James Molloyea052562012-09-18 08:31:15 +00004284 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004285 case ARM::VMOVS: {
4286 if (Domain != ExeNEON)
4287 break;
4288
4289 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4290 DstReg = MI->getOperand(0).getReg();
4291 SrcReg = MI->getOperand(1).getReg();
4292
Tim Northoverca9f3842012-08-30 10:17:45 +00004293 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4294 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4295 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4296
James Molloyea052562012-09-18 08:31:15 +00004297 unsigned ImplicitSReg;
4298 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4299 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004300
Tim Northoverc8d867d2012-09-05 18:37:53 +00004301 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4302 MI->RemoveOperand(i-1);
4303
Tim Northoverca9f3842012-08-30 10:17:45 +00004304 if (DSrc == DDst) {
4305 // Destination can be:
4306 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4307 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004308 MIB.addReg(DDst, RegState::Define)
4309 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4310 .addImm(SrcLane);
4311 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004312
4313 // Neither the source or the destination are naturally represented any
4314 // more, so add them in manually.
4315 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4316 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004317 if (ImplicitSReg != 0)
4318 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004319 break;
4320 }
4321
4322 // In general there's no single instruction that can perform an S <-> S
4323 // move in NEON space, but a pair of VEXT instructions *can* do the
4324 // job. It turns out that the VEXTs needed will only use DSrc once, with
4325 // the position based purely on the combination of lane-0 and lane-1
4326 // involved. For example
4327 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4328 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4329 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4330 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4331 //
4332 // Pattern of the MachineInstrs is:
4333 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4334 MachineInstrBuilder NewMIB;
4335 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4336 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004337
4338 // On the first instruction, both DSrc and DDst may be <undef> if present.
4339 // Specifically when the original instruction didn't have them as an
4340 // <imp-use>.
4341 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4342 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4343 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4344
4345 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4346 CurUndef = !MI->readsRegister(CurReg, TRI);
4347 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4348
Tim Northoverca9f3842012-08-30 10:17:45 +00004349 NewMIB.addImm(1);
4350 AddDefaultPred(NewMIB);
4351
4352 if (SrcLane == DstLane)
4353 NewMIB.addReg(SrcReg, RegState::Implicit);
4354
4355 MI->setDesc(get(ARM::VEXTd32));
4356 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004357
4358 // On the second instruction, DDst has definitely been defined above, so
4359 // it is not <undef>. DSrc, if present, can be <undef> as above.
4360 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4361 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4362 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4363
4364 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4365 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4366 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4367
Tim Northoverca9f3842012-08-30 10:17:45 +00004368 MIB.addImm(1);
4369 AddDefaultPred(MIB);
4370
4371 if (SrcLane != DstLane)
4372 MIB.addReg(SrcReg, RegState::Implicit);
4373
4374 // As before, the original destination is no longer represented, add it
4375 // implicitly.
4376 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004377 if (ImplicitSReg != 0)
4378 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004379 break;
4380 }
Tim Northoverf6618152012-08-17 11:32:52 +00004381 }
4382
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004383}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004384
Bob Wilsone8a549c2012-09-29 21:43:49 +00004385//===----------------------------------------------------------------------===//
4386// Partial register updates
4387//===----------------------------------------------------------------------===//
4388//
4389// Swift renames NEON registers with 64-bit granularity. That means any
4390// instruction writing an S-reg implicitly reads the containing D-reg. The
4391// problem is mostly avoided by translating f32 operations to v2f32 operations
4392// on D-registers, but f32 loads are still a problem.
4393//
4394// These instructions can load an f32 into a NEON register:
4395//
4396// VLDRS - Only writes S, partial D update.
4397// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4398// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4399//
4400// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004401unsigned ARMBaseInstrInfo::
4402getPartialRegUpdateClearance(const MachineInstr *MI,
4403 unsigned OpNum,
4404 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004405 if (!SwiftPartialUpdateClearance ||
4406 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004407 return 0;
4408
4409 assert(TRI && "Need TRI instance");
4410
4411 const MachineOperand &MO = MI->getOperand(OpNum);
4412 if (MO.readsReg())
4413 return 0;
4414 unsigned Reg = MO.getReg();
4415 int UseOp = -1;
4416
4417 switch(MI->getOpcode()) {
4418 // Normal instructions writing only an S-register.
4419 case ARM::VLDRS:
4420 case ARM::FCONSTS:
4421 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004422 case ARM::VMOVv8i8:
4423 case ARM::VMOVv4i16:
4424 case ARM::VMOVv2i32:
4425 case ARM::VMOVv2f32:
4426 case ARM::VMOVv1i64:
4427 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4428 break;
4429
4430 // Explicitly reads the dependency.
4431 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004432 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004433 break;
4434 default:
4435 return 0;
4436 }
4437
4438 // If this instruction actually reads a value from Reg, there is no unwanted
4439 // dependency.
4440 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4441 return 0;
4442
4443 // We must be able to clobber the whole D-reg.
4444 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4445 // Virtual register must be a foo:ssub_0<def,undef> operand.
4446 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4447 return 0;
4448 } else if (ARM::SPRRegClass.contains(Reg)) {
4449 // Physical register: MI must define the full D-reg.
4450 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4451 &ARM::DPRRegClass);
4452 if (!DReg || !MI->definesRegister(DReg, TRI))
4453 return 0;
4454 }
4455
4456 // MI has an unwanted D-register dependency.
4457 // Avoid defs in the previous N instructrions.
4458 return SwiftPartialUpdateClearance;
4459}
4460
4461// Break a partial register dependency after getPartialRegUpdateClearance
4462// returned non-zero.
4463void ARMBaseInstrInfo::
4464breakPartialRegDependency(MachineBasicBlock::iterator MI,
4465 unsigned OpNum,
4466 const TargetRegisterInfo *TRI) const {
4467 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4468 assert(TRI && "Need TRI instance");
4469
4470 const MachineOperand &MO = MI->getOperand(OpNum);
4471 unsigned Reg = MO.getReg();
4472 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4473 "Can't break virtual register dependencies.");
4474 unsigned DReg = Reg;
4475
4476 // If MI defines an S-reg, find the corresponding D super-register.
4477 if (ARM::SPRRegClass.contains(Reg)) {
4478 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4479 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4480 }
4481
4482 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4483 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4484
4485 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4486 // the full D-register by loading the same value to both lanes. The
4487 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004488 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004489 // too big regressions.
4490
4491 // Insert the dependency-breaking FCONSTD before MI.
4492 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4493 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4494 get(ARM::FCONSTD), DReg).addImm(96));
4495 MI->addRegisterKilled(DReg, TRI, true);
4496}
4497
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004498bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004499 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004500}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004501
4502bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004503 if (MI->getNumOperands() < 4)
4504 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004505 unsigned ShOpVal = MI->getOperand(3).getImm();
4506 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4507 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4508 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4509 ((ShImm == 1 || ShImm == 2) &&
4510 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4511 return true;
4512
4513 return false;
4514}
Quentin Colombetd358e842014-08-22 18:05:22 +00004515
4516bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4517 const MachineInstr &MI, unsigned DefIdx,
4518 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4519 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4520 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4521
4522 switch (MI.getOpcode()) {
4523 case ARM::VMOVDRR:
4524 // dX = VMOVDRR rY, rZ
4525 // is the same as:
4526 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4527 // Populate the InputRegs accordingly.
4528 // rY
4529 const MachineOperand *MOReg = &MI.getOperand(1);
4530 InputRegs.push_back(
4531 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4532 // rZ
4533 MOReg = &MI.getOperand(2);
4534 InputRegs.push_back(
4535 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4536 return true;
4537 }
4538 llvm_unreachable("Target dependent opcode missing");
4539}
4540
4541bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4542 const MachineInstr &MI, unsigned DefIdx,
4543 RegSubRegPairAndIdx &InputReg) const {
4544 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4545 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4546
4547 switch (MI.getOpcode()) {
4548 case ARM::VMOVRRD:
4549 // rX, rY = VMOVRRD dZ
4550 // is the same as:
4551 // rX = EXTRACT_SUBREG dZ, ssub_0
4552 // rY = EXTRACT_SUBREG dZ, ssub_1
4553 const MachineOperand &MOReg = MI.getOperand(2);
4554 InputReg.Reg = MOReg.getReg();
4555 InputReg.SubReg = MOReg.getSubReg();
4556 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4557 return true;
4558 }
4559 llvm_unreachable("Target dependent opcode missing");
4560}
4561
4562bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4563 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4564 RegSubRegPairAndIdx &InsertedReg) const {
4565 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4566 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4567
4568 switch (MI.getOpcode()) {
4569 case ARM::VSETLNi32:
4570 // dX = VSETLNi32 dY, rZ, imm
4571 const MachineOperand &MOBaseReg = MI.getOperand(1);
4572 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4573 const MachineOperand &MOIndex = MI.getOperand(3);
4574 BaseReg.Reg = MOBaseReg.getReg();
4575 BaseReg.SubReg = MOBaseReg.getSubReg();
4576
4577 InsertedReg.Reg = MOInsertedReg.getReg();
4578 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4579 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4580 return true;
4581 }
4582 llvm_unreachable("Target dependent opcode missing");
4583}