blob: d90d1443448ff98507cbad6b837230cd76a089c4 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
David Goodwinaf7451b2009-07-08 16:09:28 +000014#include "ARM.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000015#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000018#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000020#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000023#include "llvm/CodeGen/LiveVariables.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000024#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000030#include "llvm/CodeGen/SelectionDAGNodes.h"
Matthias Braun88e21312015-06-13 03:42:11 +000031#include "llvm/CodeGen/TargetSchedule.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000036#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000037#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000038#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000041#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000042
David Goodwinaf7451b2009-07-08 16:09:28 +000043using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "arm-instrinfo"
46
Chandler Carruthd174b722014-04-22 02:03:14 +000047#define GET_INSTRINFO_CTOR_DTOR
48#include "ARMGenInstrInfo.inc"
49
David Goodwinaf7451b2009-07-08 16:09:28 +000050static cl::opt<bool>
51EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
52 cl::desc("Enable ARM 2-addr to 3-addr conv"));
53
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000054static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000055WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000056 cl::desc("Widen ARM vmovs to vmovd when possible"));
57
Bob Wilsone8a549c2012-09-29 21:43:49 +000058static cl::opt<unsigned>
59SwiftPartialUpdateClearance("swift-partial-update-clearance",
60 cl::Hidden, cl::init(12),
61 cl::desc("Clearance before partial register updates"));
62
Evan Cheng62c7b5b2010-12-05 22:04:16 +000063/// ARM_MLxEntry - Record information about MLA / MLS instructions.
64struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000065 uint16_t MLxOpc; // MLA / MLS opcode
66 uint16_t MulOpc; // Expanded multiplication opcode
67 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000068 bool NegAcc; // True if the acc is negated before the add / sub.
69 bool HasLane; // True if instruction has an extra "lane" operand.
70};
71
72static const ARM_MLxEntry ARM_MLxTable[] = {
73 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
74 // fp scalar ops
75 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
76 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
77 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
78 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000079 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
80 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
81 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
82 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
83
84 // fp SIMD ops
85 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
86 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
87 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
88 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
89 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
90 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
91 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
92 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
93};
94
Anton Korobeynikov14635da2009-11-02 00:10:38 +000095ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000096 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000097 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000098 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
99 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000100 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000101 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
102 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
103 }
104}
105
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000106// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
107// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000108ScheduleHazardRecognizer *
109ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
110 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000111 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000112 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000113 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000114 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
115 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000116 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000117}
118
119ScheduleHazardRecognizer *ARMBaseInstrInfo::
120CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
121 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000122 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000123 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000124 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000125}
126
127MachineInstr *
128ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
129 MachineBasicBlock::iterator &MBBI,
130 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000131 // FIXME: Thumb2 support.
132
David Goodwinaf7451b2009-07-08 16:09:28 +0000133 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000134 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000135
136 MachineInstr *MI = MBBI;
137 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000138 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000139 bool isPre = false;
140 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000142 case ARMII::IndexModePre:
143 isPre = true;
144 break;
145 case ARMII::IndexModePost:
146 break;
147 }
148
149 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
150 // operation.
151 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
152 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000154
Craig Topper062a2ba2014-04-25 05:30:21 +0000155 MachineInstr *UpdateMI = nullptr;
156 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000157 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000158 const MCInstrDesc &MCID = MI->getDesc();
159 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000160 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000161 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
162 const MachineOperand &Base = MI->getOperand(2);
163 const MachineOperand &Offset = MI->getOperand(NumOps-3);
164 unsigned WBReg = WB.getReg();
165 unsigned BaseReg = Base.getReg();
166 unsigned OffReg = Offset.getReg();
167 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
168 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
169 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000170 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000171 case ARMII::AddrMode2: {
172 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
173 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
174 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000175 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 // Can't encode it in a so_imm operand. This transformation will
177 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000178 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000179 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000180 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000181 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000182 .addImm(Pred).addReg(0).addReg(0);
183 } else if (Amt != 0) {
184 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
185 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
186 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000187 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000188 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
189 .addImm(Pred).addReg(0).addReg(0);
190 } else
191 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000192 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000193 .addReg(BaseReg).addReg(OffReg)
194 .addImm(Pred).addReg(0).addReg(0);
195 break;
196 }
197 case ARMII::AddrMode3 : {
198 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
199 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
200 if (OffReg == 0)
201 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
202 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000203 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000204 .addReg(BaseReg).addImm(Amt)
205 .addImm(Pred).addReg(0).addReg(0);
206 else
207 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000208 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000209 .addReg(BaseReg).addReg(OffReg)
210 .addImm(Pred).addReg(0).addReg(0);
211 break;
212 }
213 }
214
215 std::vector<MachineInstr*> NewMIs;
216 if (isPre) {
217 if (isLoad)
218 MemMI = BuildMI(MF, MI->getDebugLoc(),
219 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000220 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000221 else
222 MemMI = BuildMI(MF, MI->getDebugLoc(),
223 get(MemOpc)).addReg(MI->getOperand(1).getReg())
224 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
225 NewMIs.push_back(MemMI);
226 NewMIs.push_back(UpdateMI);
227 } else {
228 if (isLoad)
229 MemMI = BuildMI(MF, MI->getDebugLoc(),
230 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000231 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000232 else
233 MemMI = BuildMI(MF, MI->getDebugLoc(),
234 get(MemOpc)).addReg(MI->getOperand(1).getReg())
235 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
236 if (WB.isDead())
237 UpdateMI->getOperand(0).setIsDead();
238 NewMIs.push_back(UpdateMI);
239 NewMIs.push_back(MemMI);
240 }
241
242 // Transfer LiveVariables states, kill / dead info.
243 if (LV) {
244 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
245 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000246 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000247 unsigned Reg = MO.getReg();
248
249 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
250 if (MO.isDef()) {
251 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
252 if (MO.isDead())
253 LV->addVirtualRegisterDead(Reg, NewMI);
254 }
255 if (MO.isUse() && MO.isKill()) {
256 for (unsigned j = 0; j < 2; ++j) {
257 // Look at the two new MI's in reverse order.
258 MachineInstr *NewMI = NewMIs[j];
259 if (!NewMI->readsRegister(Reg))
260 continue;
261 LV->addVirtualRegisterKilled(Reg, NewMI);
262 if (VI.removeKill(MI))
263 VI.Kills.push_back(NewMI);
264 break;
265 }
266 }
267 }
268 }
269 }
270
271 MFI->insert(MBBI, NewMIs[1]);
272 MFI->insert(MBBI, NewMIs[0]);
273 return NewMIs[0];
274}
275
276// Branch analysis.
277bool
278ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
279 MachineBasicBlock *&FBB,
280 SmallVectorImpl<MachineOperand> &Cond,
281 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000282 TBB = nullptr;
283 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000284
David Goodwinaf7451b2009-07-08 16:09:28 +0000285 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000286 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000287 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000288 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000289
290 // Walk backwards from the end of the basic block until the branch is
291 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +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.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000325 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000326 } 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.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000333 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000334 (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
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000441bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
442 if (MI.isBundle()) {
443 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
444 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000445 while (++I != E && I->isInsideBundle()) {
446 int PIdx = I->findFirstPredOperandIdx();
447 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
448 return true;
449 }
450 return false;
451 }
452
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000453 int PIdx = MI.findFirstPredOperandIdx();
454 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000455}
456
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000457bool ARMBaseInstrInfo::PredicateInstruction(
458 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
459 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000460 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000461 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
462 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000463 .addImm(Pred[0].getImm())
464 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000465 return true;
466 }
467
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000468 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000469 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000470 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000471 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000472 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000473 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
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000504bool ARMBaseInstrInfo::DefinesPredicate(
505 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000506 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000507 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.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000558bool ARMBaseInstrInfo::isPredicable(MachineInstr &MI) const {
559 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000560 return false;
561
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000562 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000563 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000564
Joey Goulya5153cb2013-09-09 14:21:49 +0000565 ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000566 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000567
568 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000569 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000570 return isV8EligibleForIT(&MI);
Joey Goulya5153cb2013-09-09 14:21:49 +0000571 } else { // non-Thumb
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000572 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
Joey Goulya5153cb2013-09-09 14:21:49 +0000573 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:
Saleem Abdulrasool1632fe12016-03-10 16:26:37 +0000635 case ARM::tInt_WIN_eh_sjlj_longjmp:
David Blaikie46a9f012012-01-20 21:51:11 +0000636 return 10;
637 case ARM::Int_eh_sjlj_setjmp:
638 case ARM::Int_eh_sjlj_setjmp_nofp:
639 return 20;
640 case ARM::tInt_eh_sjlj_setjmp:
641 case ARM::t2Int_eh_sjlj_setjmp:
642 case ARM::t2Int_eh_sjlj_setjmp_nofp:
643 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000644 case ARM::SPACE:
645 return MI->getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000646 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000647}
648
Evan Cheng7fae11b2011-12-14 02:11:42 +0000649unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
650 unsigned Size = 0;
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000651 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
652 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000653 while (++I != E && I->isInsideBundle()) {
654 assert(!I->isBundle() && "No nested bundle!");
655 Size += GetInstSizeInBytes(&*I);
656 }
657 return Size;
658}
659
Tim Northover5d72c5d2014-10-01 19:21:03 +0000660void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
661 MachineBasicBlock::iterator I,
662 unsigned DestReg, bool KillSrc,
663 const ARMSubtarget &Subtarget) const {
664 unsigned Opc = Subtarget.isThumb()
665 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
666 : ARM::MRS;
667
668 MachineInstrBuilder MIB =
669 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
670
671 // There is only 1 A/R class MRS instruction, and it always refers to
672 // APSR. However, there are lots of other possibilities on M-class cores.
673 if (Subtarget.isMClass())
674 MIB.addImm(0x800);
675
676 AddDefaultPred(MIB);
677
678 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
679}
680
681void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
682 MachineBasicBlock::iterator I,
683 unsigned SrcReg, bool KillSrc,
684 const ARMSubtarget &Subtarget) const {
685 unsigned Opc = Subtarget.isThumb()
686 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
687 : ARM::MSR;
688
689 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
690
691 if (Subtarget.isMClass())
692 MIB.addImm(0x800);
693 else
694 MIB.addImm(8);
695
696 MIB.addReg(SrcReg, getKillRegState(KillSrc));
697
698 AddDefaultPred(MIB);
699
700 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
701}
702
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000703void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
704 MachineBasicBlock::iterator I, DebugLoc DL,
705 unsigned DestReg, unsigned SrcReg,
706 bool KillSrc) const {
707 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000708 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000709
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000710 if (GPRDest && GPRSrc) {
711 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000712 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000713 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000714 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000715
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000716 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000717 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000718
Chad Rosierbe762512011-08-20 00:17:25 +0000719 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000720 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000721 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000722 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000723 Opc = ARM::VMOVRS;
724 else if (SPRDest && GPRSrc)
725 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000726 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000727 Opc = ARM::VMOVD;
728 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000729 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000730
Chad Rosierbe762512011-08-20 00:17:25 +0000731 if (Opc) {
732 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000733 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000734 if (Opc == ARM::VORRq)
735 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000736 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000737 return;
738 }
739
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000740 // Handle register classes that require multiple instructions.
741 unsigned BeginIdx = 0;
742 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000743 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000744
745 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000746 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
747 Opc = ARM::VORRq;
748 BeginIdx = ARM::qsub_0;
749 SubRegs = 2;
750 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
751 Opc = ARM::VORRq;
752 BeginIdx = ARM::qsub_0;
753 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000754 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000755 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
756 Opc = ARM::VMOVD;
757 BeginIdx = ARM::dsub_0;
758 SubRegs = 2;
759 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
760 Opc = ARM::VMOVD;
761 BeginIdx = ARM::dsub_0;
762 SubRegs = 3;
763 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
764 Opc = ARM::VMOVD;
765 BeginIdx = ARM::dsub_0;
766 SubRegs = 4;
767 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000768 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000769 BeginIdx = ARM::gsub_0;
770 SubRegs = 2;
771 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
772 Opc = ARM::VMOVD;
773 BeginIdx = ARM::dsub_0;
774 SubRegs = 2;
775 Spacing = 2;
776 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
777 Opc = ARM::VMOVD;
778 BeginIdx = ARM::dsub_0;
779 SubRegs = 3;
780 Spacing = 2;
781 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
782 Opc = ARM::VMOVD;
783 BeginIdx = ARM::dsub_0;
784 SubRegs = 4;
785 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000786 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
787 Opc = ARM::VMOVS;
788 BeginIdx = ARM::ssub_0;
789 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000790 } else if (SrcReg == ARM::CPSR) {
791 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
792 return;
793 } else if (DestReg == ARM::CPSR) {
794 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
795 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000796 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000797
Andrew Trickb57e2252012-08-29 04:41:37 +0000798 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000799
Andrew Trick4cc69492012-08-29 01:58:52 +0000800 const TargetRegisterInfo *TRI = &getRegisterInfo();
801 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000802
803 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
804 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000805 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000806 Spacing = -Spacing;
807 }
808#ifndef NDEBUG
809 SmallSet<unsigned, 4> DstRegs;
810#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000811 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000812 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
813 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000814 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000815#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000816 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000817 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000818#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000819 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000820 // VORR takes two source operands.
821 if (Opc == ARM::VORRq)
822 Mov.addReg(Src);
823 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000824 // MOVr can set CC.
825 if (Opc == ARM::MOVr)
826 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000827 }
828 // Add implicit super-register defs and kills to the last instruction.
829 Mov->addRegisterDefined(DestReg, TRI);
830 if (KillSrc)
831 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000832}
833
Tim Northover798697d2013-04-21 11:57:07 +0000834const MachineInstrBuilder &
835ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
836 unsigned SubIdx, unsigned State,
837 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000838 if (!SubIdx)
839 return MIB.addReg(Reg, State);
840
841 if (TargetRegisterInfo::isPhysicalRegister(Reg))
842 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
843 return MIB.addReg(Reg, State, SubIdx);
844}
845
David Goodwinaf7451b2009-07-08 16:09:28 +0000846void ARMBaseInstrInfo::
847storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
848 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000849 const TargetRegisterClass *RC,
850 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000851 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000852 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000853 MachineFunction &MF = *MBB.getParent();
854 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000855 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000856
Alex Lorenze40c8a22015-08-11 23:09:45 +0000857 MachineMemOperand *MMO = MF.getMachineMemOperand(
858 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
859 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000860
Owen Anderson732f82c2011-08-10 17:21:20 +0000861 switch (RC->getSize()) {
862 case 4:
863 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
864 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000865 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000866 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000867 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
868 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000869 .addReg(SrcReg, getKillRegState(isKill))
870 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000871 } else
872 llvm_unreachable("Unknown reg class!");
873 break;
874 case 8:
875 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
876 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000877 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000878 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000879 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000880 if (Subtarget.hasV5TEOps()) {
881 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
882 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
883 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
884 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
885
886 AddDefaultPred(MIB);
887 } else {
888 // Fallback to STM instruction, which has existed since the dawn of
889 // time.
890 MachineInstrBuilder MIB =
891 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
892 .addFrameIndex(FI).addMemOperand(MMO));
893 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
894 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
895 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000896 } else
897 llvm_unreachable("Unknown reg class!");
898 break;
899 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000900 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000901 // Use aligned spills if the stack can be realigned.
902 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000903 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000904 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000905 .addReg(SrcReg, getKillRegState(isKill))
906 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000907 } else {
908 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000909 .addReg(SrcReg, getKillRegState(isKill))
910 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000911 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000912 }
913 } else
914 llvm_unreachable("Unknown reg class!");
915 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000916 case 24:
917 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
918 // Use aligned spills if the stack can be realigned.
919 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
920 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
921 .addFrameIndex(FI).addImm(16)
922 .addReg(SrcReg, getKillRegState(isKill))
923 .addMemOperand(MMO));
924 } else {
925 MachineInstrBuilder MIB =
926 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
927 .addFrameIndex(FI))
928 .addMemOperand(MMO);
929 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
930 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
931 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
932 }
933 } else
934 llvm_unreachable("Unknown reg class!");
935 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000936 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000937 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000938 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
939 // FIXME: It's possible to only store part of the QQ register if the
940 // spilled def has a sub-register index.
941 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000942 .addFrameIndex(FI).addImm(16)
943 .addReg(SrcReg, getKillRegState(isKill))
944 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000945 } else {
946 MachineInstrBuilder MIB =
947 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000948 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000949 .addMemOperand(MMO);
950 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
951 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
952 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
953 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
954 }
955 } else
956 llvm_unreachable("Unknown reg class!");
957 break;
958 case 64:
959 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
960 MachineInstrBuilder MIB =
961 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
962 .addFrameIndex(FI))
963 .addMemOperand(MMO);
964 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
965 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
966 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
967 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
968 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
969 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
970 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
971 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
972 } else
973 llvm_unreachable("Unknown reg class!");
974 break;
975 default:
976 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000977 }
978}
979
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000980unsigned
981ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
982 int &FrameIndex) const {
983 switch (MI->getOpcode()) {
984 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000985 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000986 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
987 if (MI->getOperand(1).isFI() &&
988 MI->getOperand(2).isReg() &&
989 MI->getOperand(3).isImm() &&
990 MI->getOperand(2).getReg() == 0 &&
991 MI->getOperand(3).getImm() == 0) {
992 FrameIndex = MI->getOperand(1).getIndex();
993 return MI->getOperand(0).getReg();
994 }
995 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000996 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000997 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000998 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000999 case ARM::VSTRD:
1000 case ARM::VSTRS:
1001 if (MI->getOperand(1).isFI() &&
1002 MI->getOperand(2).isImm() &&
1003 MI->getOperand(2).getImm() == 0) {
1004 FrameIndex = MI->getOperand(1).getIndex();
1005 return MI->getOperand(0).getReg();
1006 }
1007 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001008 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001009 case ARM::VST1d64TPseudo:
1010 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001011 if (MI->getOperand(0).isFI() &&
1012 MI->getOperand(2).getSubReg() == 0) {
1013 FrameIndex = MI->getOperand(0).getIndex();
1014 return MI->getOperand(2).getReg();
1015 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001016 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001017 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001018 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001019 MI->getOperand(0).getSubReg() == 0) {
1020 FrameIndex = MI->getOperand(1).getIndex();
1021 return MI->getOperand(0).getReg();
1022 }
1023 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001024 }
1025
1026 return 0;
1027}
1028
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001029unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
1030 int &FrameIndex) const {
1031 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001032 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001033}
1034
David Goodwinaf7451b2009-07-08 16:09:28 +00001035void ARMBaseInstrInfo::
1036loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1037 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001038 const TargetRegisterClass *RC,
1039 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001040 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001041 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001042 MachineFunction &MF = *MBB.getParent();
1043 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001044 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001045 MachineMemOperand *MMO = MF.getMachineMemOperand(
1046 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1047 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001048
Owen Anderson732f82c2011-08-10 17:21:20 +00001049 switch (RC->getSize()) {
1050 case 4:
1051 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1052 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1053 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001054
Owen Anderson732f82c2011-08-10 17:21:20 +00001055 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1056 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001057 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001058 } else
1059 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001060 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001061 case 8:
1062 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1063 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001064 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001065 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001066 MachineInstrBuilder MIB;
1067
1068 if (Subtarget.hasV5TEOps()) {
1069 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1070 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1071 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1072 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1073
1074 AddDefaultPred(MIB);
1075 } else {
1076 // Fallback to LDM instruction, which has existed since the dawn of
1077 // time.
1078 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1079 .addFrameIndex(FI).addMemOperand(MMO));
1080 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1081 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1082 }
1083
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001084 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1085 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001086 } else
1087 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001088 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001089 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001090 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001091 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001092 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001093 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001094 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001095 } else {
1096 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1097 .addFrameIndex(FI)
1098 .addMemOperand(MMO));
1099 }
1100 } else
1101 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001102 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001103 case 24:
1104 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1105 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1106 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1107 .addFrameIndex(FI).addImm(16)
1108 .addMemOperand(MMO));
1109 } else {
1110 MachineInstrBuilder MIB =
1111 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1112 .addFrameIndex(FI)
1113 .addMemOperand(MMO));
1114 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1115 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1116 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1117 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1118 MIB.addReg(DestReg, RegState::ImplicitDefine);
1119 }
1120 } else
1121 llvm_unreachable("Unknown reg class!");
1122 break;
1123 case 32:
1124 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001125 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1126 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001127 .addFrameIndex(FI).addImm(16)
1128 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001129 } else {
1130 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001131 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1132 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001133 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001134 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1135 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1136 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1137 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001138 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1139 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001140 }
1141 } else
1142 llvm_unreachable("Unknown reg class!");
1143 break;
1144 case 64:
1145 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1146 MachineInstrBuilder MIB =
1147 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1148 .addFrameIndex(FI))
1149 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001150 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1151 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1152 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1153 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1154 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1155 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1156 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1157 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001158 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1159 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001160 } else
1161 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001162 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001163 default:
1164 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001165 }
1166}
1167
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001168unsigned
1169ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1170 int &FrameIndex) const {
1171 switch (MI->getOpcode()) {
1172 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001173 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001174 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1175 if (MI->getOperand(1).isFI() &&
1176 MI->getOperand(2).isReg() &&
1177 MI->getOperand(3).isImm() &&
1178 MI->getOperand(2).getReg() == 0 &&
1179 MI->getOperand(3).getImm() == 0) {
1180 FrameIndex = MI->getOperand(1).getIndex();
1181 return MI->getOperand(0).getReg();
1182 }
1183 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001184 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001185 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001186 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001187 case ARM::VLDRD:
1188 case ARM::VLDRS:
1189 if (MI->getOperand(1).isFI() &&
1190 MI->getOperand(2).isImm() &&
1191 MI->getOperand(2).getImm() == 0) {
1192 FrameIndex = MI->getOperand(1).getIndex();
1193 return MI->getOperand(0).getReg();
1194 }
1195 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001196 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001197 case ARM::VLD1d64TPseudo:
1198 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001199 if (MI->getOperand(1).isFI() &&
1200 MI->getOperand(0).getSubReg() == 0) {
1201 FrameIndex = MI->getOperand(1).getIndex();
1202 return MI->getOperand(0).getReg();
1203 }
1204 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001205 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001206 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001207 MI->getOperand(0).getSubReg() == 0) {
1208 FrameIndex = MI->getOperand(1).getIndex();
1209 return MI->getOperand(0).getReg();
1210 }
1211 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001212 }
1213
1214 return 0;
1215}
1216
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001217unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1218 int &FrameIndex) const {
1219 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001220 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001221}
1222
Scott Douglass953f9082015-10-05 14:49:54 +00001223/// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1224/// depending on whether the result is used.
1225void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MBBI) const {
1226 bool isThumb1 = Subtarget.isThumb1Only();
1227 bool isThumb2 = Subtarget.isThumb2();
1228 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1229
1230 MachineInstr *MI = MBBI;
1231 DebugLoc dl = MI->getDebugLoc();
1232 MachineBasicBlock *BB = MI->getParent();
1233
1234 MachineInstrBuilder LDM, STM;
1235 if (isThumb1 || !MI->getOperand(1).isDead()) {
1236 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1237 : isThumb1 ? ARM::tLDMIA_UPD
1238 : ARM::LDMIA_UPD))
1239 .addOperand(MI->getOperand(1));
1240 } else {
1241 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1242 }
1243
1244 if (isThumb1 || !MI->getOperand(0).isDead()) {
1245 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1246 : isThumb1 ? ARM::tSTMIA_UPD
1247 : ARM::STMIA_UPD))
1248 .addOperand(MI->getOperand(0));
1249 } else {
1250 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1251 }
1252
1253 AddDefaultPred(LDM.addOperand(MI->getOperand(3)));
1254 AddDefaultPred(STM.addOperand(MI->getOperand(2)));
1255
1256 // Sort the scratch registers into ascending order.
1257 const TargetRegisterInfo &TRI = getRegisterInfo();
1258 llvm::SmallVector<unsigned, 6> ScratchRegs;
1259 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1260 ScratchRegs.push_back(MI->getOperand(I).getReg());
1261 std::sort(ScratchRegs.begin(), ScratchRegs.end(),
1262 [&TRI](const unsigned &Reg1,
1263 const unsigned &Reg2) -> bool {
1264 return TRI.getEncodingValue(Reg1) <
1265 TRI.getEncodingValue(Reg2);
1266 });
1267
1268 for (const auto &Reg : ScratchRegs) {
1269 LDM.addReg(Reg, RegState::Define);
1270 STM.addReg(Reg, RegState::Kill);
1271 }
1272
1273 BB->erase(MBBI);
1274}
1275
1276
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001277bool
1278ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
1279 MachineFunction &MF = *MI->getParent()->getParent();
1280 Reloc::Model RM = MF.getTarget().getRelocationModel();
1281
1282 if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001283 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001284 "LOAD_STACK_GUARD currently supported only for MachO.");
1285 expandLoadStackGuard(MI, RM);
1286 MI->getParent()->erase(MI);
1287 return true;
1288 }
1289
Scott Douglass953f9082015-10-05 14:49:54 +00001290 if (MI->getOpcode() == ARM::MEMCPY) {
1291 expandMEMCPY(MI);
1292 return true;
1293 }
1294
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001295 // This hook gets to expand COPY instructions before they become
1296 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1297 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1298 // changed into a VORR that can go down the NEON pipeline.
Oliver Stannard51b1d462014-08-21 12:50:31 +00001299 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15() ||
1300 Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001301 return false;
1302
1303 // Look for a copy between even S-registers. That is where we keep floats
1304 // when using NEON v2f32 instructions for f32 arithmetic.
1305 unsigned DstRegS = MI->getOperand(0).getReg();
1306 unsigned SrcRegS = MI->getOperand(1).getReg();
1307 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1308 return false;
1309
1310 const TargetRegisterInfo *TRI = &getRegisterInfo();
1311 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1312 &ARM::DPRRegClass);
1313 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1314 &ARM::DPRRegClass);
1315 if (!DstRegD || !SrcRegD)
1316 return false;
1317
1318 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1319 // legal if the COPY already defines the full DstRegD, and it isn't a
1320 // sub-register insertion.
1321 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1322 return false;
1323
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001324 // A dead copy shouldn't show up here, but reject it just in case.
1325 if (MI->getOperand(0).isDead())
1326 return false;
1327
1328 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001329 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001330 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001331
1332 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1333 // or some other super-register.
1334 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1335 if (ImpDefIdx != -1)
1336 MI->RemoveOperand(ImpDefIdx);
1337
1338 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001339 MI->setDesc(get(ARM::VMOVD));
1340 MI->getOperand(0).setReg(DstRegD);
1341 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001342 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001343
1344 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1345 // register scavenger and machine verifier, so we need to indicate that we
1346 // are reading an undefined value from SrcRegD, but a proper value from
1347 // SrcRegS.
1348 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001349 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001350
1351 // SrcRegD may actually contain an unrelated value in the ssub_1
1352 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1353 if (MI->getOperand(1).isKill()) {
1354 MI->getOperand(1).setIsKill(false);
1355 MI->addRegisterKilled(SrcRegS, TRI, true);
1356 }
1357
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001358 DEBUG(dbgs() << "replaced by: " << *MI);
1359 return true;
1360}
1361
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001362/// Create a copy of a const pool value. Update CPI to the new index and return
1363/// the label UID.
1364static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1365 MachineConstantPool *MCP = MF.getConstantPool();
1366 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1367
1368 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1369 assert(MCPE.isMachineConstantPoolEntry() &&
1370 "Expecting a machine constantpool entry!");
1371 ARMConstantPoolValue *ACPV =
1372 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1373
Evan Chengdfce83c2011-01-17 08:03:18 +00001374 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001375 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001376
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001377 // FIXME: The below assumes PIC relocation model and that the function
1378 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1379 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1380 // instructions, so that's probably OK, but is PIC always correct when
1381 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001382 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001383 NewCPV = ARMConstantPoolConstant::Create(
1384 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1385 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001386 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001387 NewCPV = ARMConstantPoolSymbol::
1388 Create(MF.getFunction()->getContext(),
1389 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001390 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001391 NewCPV = ARMConstantPoolConstant::
1392 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1393 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001394 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001395 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1396 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001397 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001398 NewCPV = ARMConstantPoolMBB::
1399 Create(MF.getFunction()->getContext(),
1400 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001401 else
1402 llvm_unreachable("Unexpected ARM constantpool value type!!");
1403 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1404 return PCLabelId;
1405}
1406
Evan Chengfe864422009-11-08 00:15:23 +00001407void ARMBaseInstrInfo::
1408reMaterialize(MachineBasicBlock &MBB,
1409 MachineBasicBlock::iterator I,
1410 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001411 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001412 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001413 unsigned Opcode = Orig->getOpcode();
1414 switch (Opcode) {
1415 default: {
1416 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001417 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001418 MBB.insert(I, MI);
1419 break;
1420 }
1421 case ARM::tLDRpci_pic:
1422 case ARM::t2LDRpci_pic: {
1423 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001424 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001425 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001426 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1427 DestReg)
1428 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001429 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001430 break;
1431 }
1432 }
Evan Chengfe864422009-11-08 00:15:23 +00001433}
1434
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001435MachineInstr *
1436ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001437 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001438 switch(Orig->getOpcode()) {
1439 case ARM::tLDRpci_pic:
1440 case ARM::t2LDRpci_pic: {
1441 unsigned CPI = Orig->getOperand(1).getIndex();
1442 unsigned PCLabelId = duplicateCPV(MF, CPI);
1443 Orig->getOperand(1).setIndex(CPI);
1444 Orig->getOperand(2).setImm(PCLabelId);
1445 break;
1446 }
1447 }
1448 return MI;
1449}
1450
Evan Chenge9c46c22010-03-03 01:44:33 +00001451bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001452 const MachineInstr *MI1,
1453 const MachineRegisterInfo *MRI) const {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001454 unsigned Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001455 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001456 Opcode == ARM::t2LDRpci_pic ||
1457 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001458 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001459 Opcode == ARM::LDRLIT_ga_pcrel ||
1460 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1461 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001462 Opcode == ARM::MOV_ga_pcrel ||
1463 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001464 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001465 if (MI1->getOpcode() != Opcode)
1466 return false;
1467 if (MI0->getNumOperands() != MI1->getNumOperands())
1468 return false;
1469
1470 const MachineOperand &MO0 = MI0->getOperand(1);
1471 const MachineOperand &MO1 = MI1->getOperand(1);
1472 if (MO0.getOffset() != MO1.getOffset())
1473 return false;
1474
Tim Northover72360d22013-12-02 10:35:41 +00001475 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1476 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1477 Opcode == ARM::tLDRLIT_ga_pcrel ||
1478 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001479 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001480 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001481 // Ignore the PC labels.
1482 return MO0.getGlobal() == MO1.getGlobal();
1483
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001484 const MachineFunction *MF = MI0->getParent()->getParent();
1485 const MachineConstantPool *MCP = MF->getConstantPool();
1486 int CPI0 = MO0.getIndex();
1487 int CPI1 = MO1.getIndex();
1488 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1489 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001490 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1491 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1492 if (isARMCP0 && isARMCP1) {
1493 ARMConstantPoolValue *ACPV0 =
1494 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1495 ARMConstantPoolValue *ACPV1 =
1496 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1497 return ACPV0->hasSameValue(ACPV1);
1498 } else if (!isARMCP0 && !isARMCP1) {
1499 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1500 }
1501 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001502 } else if (Opcode == ARM::PICLDR) {
1503 if (MI1->getOpcode() != Opcode)
1504 return false;
1505 if (MI0->getNumOperands() != MI1->getNumOperands())
1506 return false;
1507
1508 unsigned Addr0 = MI0->getOperand(1).getReg();
1509 unsigned Addr1 = MI1->getOperand(1).getReg();
1510 if (Addr0 != Addr1) {
1511 if (!MRI ||
1512 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1513 !TargetRegisterInfo::isVirtualRegister(Addr1))
1514 return false;
1515
1516 // This assumes SSA form.
1517 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1518 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1519 // Check if the loaded value, e.g. a constantpool of a global address, are
1520 // the same.
1521 if (!produceSameValue(Def0, Def1, MRI))
1522 return false;
1523 }
1524
1525 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1526 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1527 const MachineOperand &MO0 = MI0->getOperand(i);
1528 const MachineOperand &MO1 = MI1->getOperand(i);
1529 if (!MO0.isIdenticalTo(MO1))
1530 return false;
1531 }
1532 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001533 }
1534
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001535 return MI0->isIdenticalTo(*MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001536}
1537
Bill Wendlingf4707472010-06-23 23:00:16 +00001538/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1539/// determine if two loads are loading from the same base address. It should
1540/// only return true if the base pointers are the same and the only differences
1541/// between the two addresses is the offset. It also returns the offsets by
1542/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001543///
1544/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1545/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001546bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1547 int64_t &Offset1,
1548 int64_t &Offset2) const {
1549 // Don't worry about Thumb: just ARM and Thumb2.
1550 if (Subtarget.isThumb1Only()) return false;
1551
1552 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1553 return false;
1554
1555 switch (Load1->getMachineOpcode()) {
1556 default:
1557 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001558 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001559 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001560 case ARM::LDRD:
1561 case ARM::LDRH:
1562 case ARM::LDRSB:
1563 case ARM::LDRSH:
1564 case ARM::VLDRD:
1565 case ARM::VLDRS:
1566 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001567 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001568 case ARM::t2LDRDi8:
1569 case ARM::t2LDRSHi8:
1570 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001571 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001572 case ARM::t2LDRSHi12:
1573 break;
1574 }
1575
1576 switch (Load2->getMachineOpcode()) {
1577 default:
1578 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001579 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001580 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001581 case ARM::LDRD:
1582 case ARM::LDRH:
1583 case ARM::LDRSB:
1584 case ARM::LDRSH:
1585 case ARM::VLDRD:
1586 case ARM::VLDRS:
1587 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001588 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001589 case ARM::t2LDRSHi8:
1590 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001591 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001592 case ARM::t2LDRSHi12:
1593 break;
1594 }
1595
1596 // Check if base addresses and chain operands match.
1597 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1598 Load1->getOperand(4) != Load2->getOperand(4))
1599 return false;
1600
1601 // Index should be Reg0.
1602 if (Load1->getOperand(3) != Load2->getOperand(3))
1603 return false;
1604
1605 // Determine the offsets.
1606 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1607 isa<ConstantSDNode>(Load2->getOperand(1))) {
1608 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1609 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1610 return true;
1611 }
1612
1613 return false;
1614}
1615
1616/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001617/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001618/// be scheduled togther. On some targets if two loads are loading from
1619/// addresses in the same cache line, it's better if they are scheduled
1620/// together. This function takes two integers that represent the load offsets
1621/// from the common base address. It returns true if it decides it's desirable
1622/// to schedule the two loads together. "NumLoads" is the number of loads that
1623/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001624///
1625/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1626/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001627bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1628 int64_t Offset1, int64_t Offset2,
1629 unsigned NumLoads) const {
1630 // Don't worry about Thumb: just ARM and Thumb2.
1631 if (Subtarget.isThumb1Only()) return false;
1632
1633 assert(Offset2 > Offset1);
1634
1635 if ((Offset2 - Offset1) / 8 > 64)
1636 return false;
1637
Renato Golinb184cd92013-08-14 16:35:29 +00001638 // Check if the machine opcodes are different. If they are different
1639 // then we consider them to not be of the same base address,
1640 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1641 // In this case, they are considered to be the same because they are different
1642 // encoding forms of the same basic instruction.
1643 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1644 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1645 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1646 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1647 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001648 return false; // FIXME: overly conservative?
1649
1650 // Four loads in a row should be sufficient.
1651 if (NumLoads >= 3)
1652 return false;
1653
1654 return true;
1655}
1656
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001657bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1658 const MachineBasicBlock *MBB,
1659 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001660 // Debug info is never a scheduling boundary. It's necessary to be explicit
1661 // due to the special treatment of IT instructions below, otherwise a
1662 // dbg_value followed by an IT will result in the IT instruction being
1663 // considered a scheduling hazard, which is wrong. It should be the actual
1664 // instruction preceding the dbg_value instruction(s), just like it is
1665 // when debug info is not present.
1666 if (MI->isDebugValue())
1667 return false;
1668
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001669 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001670 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001671 return true;
1672
1673 // Treat the start of the IT block as a scheduling boundary, but schedule
1674 // t2IT along with all instructions following it.
1675 // FIXME: This is a big hammer. But the alternative is to add all potential
1676 // true and anti dependencies to IT block instructions as implicit operands
1677 // to the t2IT instruction. The added compile time and complexity does not
1678 // seem worth it.
1679 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001680 // Make sure to skip any dbg_value instructions
1681 while (++I != MBB->end() && I->isDebugValue())
1682 ;
1683 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001684 return true;
1685
1686 // Don't attempt to schedule around any instruction that defines
1687 // a stack-oriented pointer, as it's unlikely to be profitable. This
1688 // saves compile time, because it doesn't require every single
1689 // stack slot reference to depend on the instruction that does the
1690 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001691 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001692 // No ARM calling conventions change the stack pointer. (X86 calling
1693 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001694 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001695 return true;
1696
1697 return false;
1698}
1699
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001700bool ARMBaseInstrInfo::
1701isProfitableToIfCvt(MachineBasicBlock &MBB,
1702 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001703 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001704 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001705 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001706
Peter Collingbourne65295232015-04-23 20:31:30 +00001707 // If we are optimizing for size, see if the branch in the predecessor can be
1708 // lowered to cbn?z by the constant island lowering pass, and return false if
1709 // so. This results in a shorter instruction sequence.
Sanjay Patel924879a2015-08-04 15:49:57 +00001710 if (MBB.getParent()->getFunction()->optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001711 MachineBasicBlock *Pred = *MBB.pred_begin();
1712 if (!Pred->empty()) {
1713 MachineInstr *LastMI = &*Pred->rbegin();
1714 if (LastMI->getOpcode() == ARM::t2Bcc) {
1715 MachineBasicBlock::iterator CmpMI = LastMI;
1716 if (CmpMI != Pred->begin()) {
1717 --CmpMI;
1718 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1719 CmpMI->getOpcode() == ARM::t2CMPri) {
1720 unsigned Reg = CmpMI->getOperand(0).getReg();
1721 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001722 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
Peter Collingbourne65295232015-04-23 20:31:30 +00001723 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1724 isARMLowRegister(Reg))
1725 return false;
1726 }
1727 }
1728 }
1729 }
1730 }
1731
Owen Anderson88af7d02010-09-28 18:32:13 +00001732 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001733 // Here we scale up each component of UnpredCost to avoid precision issue when
1734 // scaling NumCycles by Probability.
1735 const unsigned ScalingUpFactor = 1024;
1736 unsigned UnpredCost = Probability.scale(NumCycles * ScalingUpFactor);
1737 UnpredCost += ScalingUpFactor; // The branch itself
1738 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001739
Cong Houf9f9ffb2015-09-18 18:19:40 +00001740 return (NumCycles + ExtraPredCycles) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001741}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001742
Evan Cheng02b184d2010-06-25 22:42:03 +00001743bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001744isProfitableToIfCvt(MachineBasicBlock &TMBB,
1745 unsigned TCycles, unsigned TExtra,
1746 MachineBasicBlock &FMBB,
1747 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001748 BranchProbability Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001749 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001750 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001751
Owen Anderson88af7d02010-09-28 18:32:13 +00001752 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001753 // Here we scale up each component of UnpredCost to avoid precision issue when
1754 // scaling TCycles/FCycles by Probability.
1755 const unsigned ScalingUpFactor = 1024;
1756 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1757 unsigned FUnpredCost =
1758 Probability.getCompl().scale(FCycles * ScalingUpFactor);
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001759 unsigned UnpredCost = TUnpredCost + FUnpredCost;
Cong Houf9f9ffb2015-09-18 18:19:40 +00001760 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1761 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001762
Cong Houf9f9ffb2015-09-18 18:19:40 +00001763 return (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001764}
1765
Bob Wilsone8a549c2012-09-29 21:43:49 +00001766bool
1767ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1768 MachineBasicBlock &FMBB) const {
1769 // Reduce false anti-dependencies to let Swift's out-of-order execution
1770 // engine do its thing.
1771 return Subtarget.isSwift();
1772}
1773
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001774/// getInstrPredicate - If instruction is predicated, returns its predicate
1775/// condition, otherwise returns AL. It also returns the condition code
1776/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001777ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1778 unsigned &PredReg) {
1779 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001780 if (PIdx == -1) {
1781 PredReg = 0;
1782 return ARMCC::AL;
1783 }
1784
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001785 PredReg = MI.getOperand(PIdx+1).getReg();
1786 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001787}
1788
1789
Matthias Braunfa3872e2015-05-18 20:27:55 +00001790unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001791 if (Opc == ARM::B)
1792 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001793 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001794 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001795 if (Opc == ARM::t2B)
1796 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001797
1798 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001799}
1800
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001801MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr *MI,
1802 bool NewMI,
1803 unsigned OpIdx1,
1804 unsigned OpIdx2) const {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001805 switch (MI->getOpcode()) {
1806 case ARM::MOVCCr:
1807 case ARM::t2MOVCCr: {
1808 // MOVCC can be commuted by inverting the condition.
1809 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001810 ARMCC::CondCodes CC = getInstrPredicate(*MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001811 // MOVCC AL can't be inverted. Shouldn't happen.
1812 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001813 return nullptr;
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001814 MI = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001815 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001816 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001817 // After swapping the MOVCC operands, also invert the condition.
1818 MI->getOperand(MI->findFirstPredOperandIdx())
1819 .setImm(ARMCC::getOppositeCondition(CC));
1820 return MI;
1821 }
1822 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001823 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001824}
Evan Cheng780748d2009-07-28 05:48:47 +00001825
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001826/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001827/// return the defining instruction.
1828static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1829 const MachineRegisterInfo &MRI,
1830 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001831 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001832 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001833 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001834 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001835 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001836 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001837 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001838 // MI is folded into the MOVCC by predicating it.
1839 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001840 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001841 // Check if MI has any non-dead defs or physreg uses. This also detects
1842 // predicated instructions which will be reading CPSR.
1843 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1844 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001845 // Reject frame index operands, PEI can't handle the predicated pseudos.
1846 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001847 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001848 if (!MO.isReg())
1849 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001850 // MI can't have any tied operands, that would conflict with predication.
1851 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001852 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001853 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001854 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001855 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001856 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001857 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001858 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001859 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001860 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001861 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001862}
1863
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001864bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1865 SmallVectorImpl<MachineOperand> &Cond,
1866 unsigned &TrueOp, unsigned &FalseOp,
1867 bool &Optimizable) const {
1868 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1869 "Unknown select instruction");
1870 // MOVCC operands:
1871 // 0: Def.
1872 // 1: True use.
1873 // 2: False use.
1874 // 3: Condition code.
1875 // 4: CPSR use.
1876 TrueOp = 1;
1877 FalseOp = 2;
1878 Cond.push_back(MI->getOperand(3));
1879 Cond.push_back(MI->getOperand(4));
1880 // We can always fold a def.
1881 Optimizable = true;
1882 return false;
1883}
1884
Mehdi Amini22e59742015-01-13 07:07:13 +00001885MachineInstr *
1886ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1887 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1888 bool PreferFalse) const {
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001889 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1890 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001891 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001892 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1893 bool Invert = !DefMI;
1894 if (!DefMI)
1895 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1896 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001897 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001898
Matthias Braun2f169f92013-10-04 16:52:56 +00001899 // Find new register class to use.
1900 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1901 unsigned DestReg = MI->getOperand(0).getReg();
1902 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1903 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001904 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001905
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001906 // Create a new predicated version of DefMI.
1907 // Rfalse is the first use.
1908 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001909 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001910
1911 // Copy all the DefMI operands, excluding its (null) predicate.
1912 const MCInstrDesc &DefDesc = DefMI->getDesc();
1913 for (unsigned i = 1, e = DefDesc.getNumOperands();
1914 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1915 NewMI.addOperand(DefMI->getOperand(i));
1916
1917 unsigned CondCode = MI->getOperand(3).getImm();
1918 if (Invert)
1919 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1920 else
1921 NewMI.addImm(CondCode);
1922 NewMI.addOperand(MI->getOperand(4));
1923
1924 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1925 if (NewMI->hasOptionalDef())
1926 AddDefaultCC(NewMI);
1927
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001928 // The output register value when the predicate is false is an implicit
1929 // register operand tied to the first def.
1930 // The tie makes the register allocator ensure the FalseReg is allocated the
1931 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001932 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001933 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001934 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1935
Mehdi Amini22e59742015-01-13 07:07:13 +00001936 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1937 SeenMIs.insert(NewMI);
1938 SeenMIs.erase(DefMI);
1939
Pete Cooper2127b002015-04-30 23:57:47 +00001940 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1941 // DefMI would be invalid when tranferred inside the loop. Checking for a
1942 // loop is expensive, but at least remove kill flags if they are in different
1943 // BBs.
1944 if (DefMI->getParent() != MI->getParent())
1945 NewMI->clearKillInfo();
1946
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001947 // The caller will erase MI, but not DefMI.
1948 DefMI->eraseFromParent();
1949 return NewMI;
1950}
1951
Andrew Trick924123a2011-09-21 02:20:46 +00001952/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1953/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1954/// def operand.
1955///
1956/// This will go away once we can teach tblgen how to set the optional CPSR def
1957/// operand itself.
1958struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001959 uint16_t PseudoOpc;
1960 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001961};
1962
Craig Topper2fbd1302012-05-24 03:59:11 +00001963static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001964 {ARM::ADDSri, ARM::ADDri},
1965 {ARM::ADDSrr, ARM::ADDrr},
1966 {ARM::ADDSrsi, ARM::ADDrsi},
1967 {ARM::ADDSrsr, ARM::ADDrsr},
1968
1969 {ARM::SUBSri, ARM::SUBri},
1970 {ARM::SUBSrr, ARM::SUBrr},
1971 {ARM::SUBSrsi, ARM::SUBrsi},
1972 {ARM::SUBSrsr, ARM::SUBrsr},
1973
1974 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001975 {ARM::RSBSrsi, ARM::RSBrsi},
1976 {ARM::RSBSrsr, ARM::RSBrsr},
1977
1978 {ARM::t2ADDSri, ARM::t2ADDri},
1979 {ARM::t2ADDSrr, ARM::t2ADDrr},
1980 {ARM::t2ADDSrs, ARM::t2ADDrs},
1981
1982 {ARM::t2SUBSri, ARM::t2SUBri},
1983 {ARM::t2SUBSrr, ARM::t2SUBrr},
1984 {ARM::t2SUBSrs, ARM::t2SUBrs},
1985
1986 {ARM::t2RSBSri, ARM::t2RSBri},
1987 {ARM::t2RSBSrs, ARM::t2RSBrs},
1988};
1989
1990unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001991 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1992 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1993 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001994 return 0;
1995}
1996
Evan Cheng780748d2009-07-28 05:48:47 +00001997void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1998 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1999 unsigned DestReg, unsigned BaseReg, int NumBytes,
2000 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002001 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002002 if (NumBytes == 0 && DestReg != BaseReg) {
2003 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
2004 .addReg(BaseReg, RegState::Kill)
2005 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2006 .setMIFlags(MIFlags);
2007 return;
2008 }
2009
Evan Cheng780748d2009-07-28 05:48:47 +00002010 bool isSub = NumBytes < 0;
2011 if (isSub) NumBytes = -NumBytes;
2012
2013 while (NumBytes) {
2014 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2015 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2016 assert(ThisVal && "Didn't extract field correctly");
2017
2018 // We will handle these bits from offset, clear them.
2019 NumBytes &= ~ThisVal;
2020
2021 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2022
2023 // Build the new ADD / SUB.
2024 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2025 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2026 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002027 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2028 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002029 BaseReg = DestReg;
2030 }
2031}
2032
Tim Northoverdee86042013-12-02 14:46:26 +00002033bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2034 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002035 unsigned NumBytes) {
2036 // This optimisation potentially adds lots of load and store
2037 // micro-operations, it's only really a great benefit to code-size.
Sanjay Patel924879a2015-08-04 15:49:57 +00002038 if (!MF.getFunction()->optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002039 return false;
2040
2041 // If only one register is pushed/popped, LLVM can use an LDR/STR
2042 // instead. We can't modify those so make sure we're dealing with an
2043 // instruction we understand.
2044 bool IsPop = isPopOpcode(MI->getOpcode());
2045 bool IsPush = isPushOpcode(MI->getOpcode());
2046 if (!IsPush && !IsPop)
2047 return false;
2048
2049 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2050 MI->getOpcode() == ARM::VLDMDIA_UPD;
2051 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2052 MI->getOpcode() == ARM::tPOP ||
2053 MI->getOpcode() == ARM::tPOP_RET;
2054
2055 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2056 MI->getOperand(1).getReg() == ARM::SP)) &&
2057 "trying to fold sp update into non-sp-updating push/pop");
2058
2059 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2060 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2061 // if this is violated.
2062 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2063 return false;
2064
2065 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2066 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2067 int RegListIdx = IsT1PushPop ? 2 : 4;
2068
2069 // Calculate the space we'll need in terms of registers.
2070 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2071 unsigned RD0Reg, RegsNeeded;
2072 if (IsVFPPushPop) {
2073 RD0Reg = ARM::D0;
2074 RegsNeeded = NumBytes / 8;
2075 } else {
2076 RD0Reg = ARM::R0;
2077 RegsNeeded = NumBytes / 4;
2078 }
2079
2080 // We're going to have to strip all list operands off before
2081 // re-adding them since the order matters, so save the existing ones
2082 // for later.
2083 SmallVector<MachineOperand, 4> RegList;
2084 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2085 RegList.push_back(MI->getOperand(i));
2086
Tim Northover93bcc662013-11-08 17:18:07 +00002087 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002088 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002089
2090 // Now try to find enough space in the reglist to allocate NumBytes.
2091 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002092 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002093 if (!IsPop) {
2094 // Pushing any register is completely harmless, mark the
2095 // register involved as undef since we don't care about it in
2096 // the slightest.
2097 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2098 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002099 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002100 continue;
2101 }
2102
Tim Northover45479dc2013-12-01 14:16:24 +00002103 // However, we can only pop an extra register if it's not live. For
2104 // registers live within the function we might clobber a return value
2105 // register; the other way a register can be live here is if it's
2106 // callee-saved.
2107 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002108 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2109 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002110 // VFP pops don't allow holes in the register list, so any skip is fatal
2111 // for our transformation. GPR pops do, so we should just keep looking.
2112 if (IsVFPPushPop)
2113 return false;
2114 else
2115 continue;
2116 }
Tim Northover93bcc662013-11-08 17:18:07 +00002117
2118 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002119 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2120 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002121 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002122 }
2123
2124 if (RegsNeeded > 0)
2125 return false;
2126
2127 // Finally we know we can profitably perform the optimisation so go
2128 // ahead: strip all existing registers off and add them back again
2129 // in the right order.
2130 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2131 MI->RemoveOperand(i);
2132
2133 // Add the complete list back in.
2134 MachineInstrBuilder MIB(MF, &*MI);
2135 for (int i = RegList.size() - 1; i >= 0; --i)
2136 MIB.addOperand(RegList[i]);
2137
2138 return true;
2139}
2140
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002141bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2142 unsigned FrameReg, int &Offset,
2143 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002144 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002145 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002146 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2147 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002148
Evan Cheng780748d2009-07-28 05:48:47 +00002149 // Memory operands in inline assembly always use AddrMode2.
2150 if (Opcode == ARM::INLINEASM)
2151 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002152
Evan Cheng780748d2009-07-28 05:48:47 +00002153 if (Opcode == ARM::ADDri) {
2154 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2155 if (Offset == 0) {
2156 // Turn it into a move.
2157 MI.setDesc(TII.get(ARM::MOVr));
2158 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2159 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002160 Offset = 0;
2161 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002162 } else if (Offset < 0) {
2163 Offset = -Offset;
2164 isSub = true;
2165 MI.setDesc(TII.get(ARM::SUBri));
2166 }
2167
2168 // Common case: small offset, fits into instruction.
2169 if (ARM_AM::getSOImmVal(Offset) != -1) {
2170 // Replace the FrameIndex with sp / fp
2171 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2172 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002173 Offset = 0;
2174 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002175 }
2176
2177 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2178 // as possible.
2179 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2180 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2181
2182 // We will handle these bits from offset, clear them.
2183 Offset &= ~ThisImmVal;
2184
2185 // Get the properly encoded SOImmVal field.
2186 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2187 "Bit extraction didn't work?");
2188 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2189 } else {
2190 unsigned ImmIdx = 0;
2191 int InstrOffs = 0;
2192 unsigned NumBits = 0;
2193 unsigned Scale = 1;
2194 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002195 case ARMII::AddrMode_i12: {
2196 ImmIdx = FrameRegIdx + 1;
2197 InstrOffs = MI.getOperand(ImmIdx).getImm();
2198 NumBits = 12;
2199 break;
2200 }
Evan Cheng780748d2009-07-28 05:48:47 +00002201 case ARMII::AddrMode2: {
2202 ImmIdx = FrameRegIdx+2;
2203 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2204 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2205 InstrOffs *= -1;
2206 NumBits = 12;
2207 break;
2208 }
2209 case ARMII::AddrMode3: {
2210 ImmIdx = FrameRegIdx+2;
2211 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2212 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2213 InstrOffs *= -1;
2214 NumBits = 8;
2215 break;
2216 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002217 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002218 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002219 // Can't fold any offset even if it's zero.
2220 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002221 case ARMII::AddrMode5: {
2222 ImmIdx = FrameRegIdx+1;
2223 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2224 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2225 InstrOffs *= -1;
2226 NumBits = 8;
2227 Scale = 4;
2228 break;
2229 }
2230 default:
2231 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002232 }
2233
2234 Offset += InstrOffs * Scale;
2235 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2236 if (Offset < 0) {
2237 Offset = -Offset;
2238 isSub = true;
2239 }
2240
2241 // Attempt to fold address comp. if opcode has offset bits
2242 if (NumBits > 0) {
2243 // Common case: small offset, fits into instruction.
2244 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2245 int ImmedOffset = Offset / Scale;
2246 unsigned Mask = (1 << NumBits) - 1;
2247 if ((unsigned)Offset <= Mask * Scale) {
2248 // Replace the FrameIndex with sp
2249 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002250 // FIXME: When addrmode2 goes away, this will simplify (like the
2251 // T2 version), as the LDR.i12 versions don't need the encoding
2252 // tricks for the offset value.
2253 if (isSub) {
2254 if (AddrMode == ARMII::AddrMode_i12)
2255 ImmedOffset = -ImmedOffset;
2256 else
2257 ImmedOffset |= 1 << NumBits;
2258 }
Evan Cheng780748d2009-07-28 05:48:47 +00002259 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002260 Offset = 0;
2261 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002262 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002263
Evan Cheng780748d2009-07-28 05:48:47 +00002264 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2265 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002266 if (isSub) {
2267 if (AddrMode == ARMII::AddrMode_i12)
2268 ImmedOffset = -ImmedOffset;
2269 else
2270 ImmedOffset |= 1 << NumBits;
2271 }
Evan Cheng780748d2009-07-28 05:48:47 +00002272 ImmOp.ChangeToImmediate(ImmedOffset);
2273 Offset &= ~(Mask*Scale);
2274 }
2275 }
2276
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002277 Offset = (isSub) ? -Offset : Offset;
2278 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002279}
Bill Wendling7de9d522010-08-06 01:32:48 +00002280
Manman Ren6fa76dc2012-06-29 21:33:59 +00002281/// analyzeCompare - For a comparison instruction, return the source registers
2282/// in SrcReg and SrcReg2 if having two register operands, and the value it
2283/// compares against in CmpValue. Return true if the comparison instruction
2284/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002285bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002286analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2287 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002288 switch (MI->getOpcode()) {
2289 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002290 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002291 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002292 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002293 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002294 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002295 CmpValue = MI->getOperand(1).getImm();
2296 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002297 case ARM::CMPrr:
2298 case ARM::t2CMPrr:
2299 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002300 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002301 CmpMask = ~0;
2302 CmpValue = 0;
2303 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002304 case ARM::TSTri:
2305 case ARM::t2TSTri:
2306 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002307 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002308 CmpMask = MI->getOperand(1).getImm();
2309 CmpValue = 0;
2310 return true;
2311 }
2312
2313 return false;
2314}
2315
Gabor Greifd36e3e82010-09-29 10:12:08 +00002316/// isSuitableForMask - Identify a suitable 'and' instruction that
2317/// operates on the given source register and applies the same mask
2318/// as a 'tst' instruction. Provide a limited look-through for copies.
2319/// When successful, MI will hold the found instruction.
2320static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002321 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002322 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002323 case ARM::ANDri:
2324 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002325 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002326 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002327 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002328 return true;
2329 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002330 }
2331
2332 return false;
2333}
2334
Manman Renb1b3db62012-06-29 22:06:19 +00002335/// getSwappedCondition - assume the flags are set by MI(a,b), return
2336/// the condition code if we modify the instructions such that flags are
2337/// set by MI(b,a).
2338inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2339 switch (CC) {
2340 default: return ARMCC::AL;
2341 case ARMCC::EQ: return ARMCC::EQ;
2342 case ARMCC::NE: return ARMCC::NE;
2343 case ARMCC::HS: return ARMCC::LS;
2344 case ARMCC::LO: return ARMCC::HI;
2345 case ARMCC::HI: return ARMCC::LO;
2346 case ARMCC::LS: return ARMCC::HS;
2347 case ARMCC::GE: return ARMCC::LE;
2348 case ARMCC::LT: return ARMCC::GT;
2349 case ARMCC::GT: return ARMCC::LT;
2350 case ARMCC::LE: return ARMCC::GE;
2351 }
2352}
2353
2354/// isRedundantFlagInstr - check whether the first instruction, whose only
2355/// purpose is to update flags, can be made redundant.
2356/// CMPrr can be made redundant by SUBrr if the operands are the same.
2357/// CMPri can be made redundant by SUBri if the operands are the same.
2358/// This function can be extended later on.
2359inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2360 unsigned SrcReg2, int ImmValue,
2361 MachineInstr *OI) {
2362 if ((CmpI->getOpcode() == ARM::CMPrr ||
2363 CmpI->getOpcode() == ARM::t2CMPrr) &&
2364 (OI->getOpcode() == ARM::SUBrr ||
2365 OI->getOpcode() == ARM::t2SUBrr) &&
2366 ((OI->getOperand(1).getReg() == SrcReg &&
2367 OI->getOperand(2).getReg() == SrcReg2) ||
2368 (OI->getOperand(1).getReg() == SrcReg2 &&
2369 OI->getOperand(2).getReg() == SrcReg)))
2370 return true;
2371
2372 if ((CmpI->getOpcode() == ARM::CMPri ||
2373 CmpI->getOpcode() == ARM::t2CMPri) &&
2374 (OI->getOpcode() == ARM::SUBri ||
2375 OI->getOpcode() == ARM::t2SUBri) &&
2376 OI->getOperand(1).getReg() == SrcReg &&
2377 OI->getOperand(2).getImm() == ImmValue)
2378 return true;
2379 return false;
2380}
2381
Manman Ren6fa76dc2012-06-29 21:33:59 +00002382/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2383/// comparison into one that sets the zero bit in the flags register;
2384/// Remove a redundant Compare instruction if an earlier instruction can set the
2385/// flags in the same way as Compare.
2386/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2387/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2388/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002389bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002390optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2391 int CmpMask, int CmpValue,
2392 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002393 // Get the unique definition of SrcReg.
2394 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2395 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002396
Gabor Greifadbbb932010-09-21 12:01:15 +00002397 // Masked compares sometimes use the same register as the corresponding 'and'.
2398 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002399 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002400 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002401 for (MachineRegisterInfo::use_instr_iterator
2402 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2403 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002404 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002405 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002406 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002407 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002408 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002409 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002410 break;
2411 }
2412 if (!MI) return false;
2413 }
2414 }
2415
Manman Rendc8ad002012-05-11 01:30:47 +00002416 // Get ready to iterate backward from CmpInstr.
2417 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2418 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002419
2420 // Early exit if CmpInstr is at the beginning of the BB.
2421 if (I == B) return false;
2422
Manman Rendc8ad002012-05-11 01:30:47 +00002423 // There are two possible candidates which can be changed to set CPSR:
2424 // One is MI, the other is a SUB instruction.
2425 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2426 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002427 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002428 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002429 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002430 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002431 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002432 // Conservatively refuse to convert an instruction which isn't in the same
2433 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002434 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2435 // Thus we cannot return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002436 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002437 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002438 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002439 else
2440 return false;
2441 }
2442
2443 // Check that CPSR isn't set between the comparison instruction and the one we
2444 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002445 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002446 --I;
2447 for (; I != E; --I) {
2448 const MachineInstr &Instr = *I;
2449
Manman Renb1b3db62012-06-29 22:06:19 +00002450 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2451 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002452 // This instruction modifies or uses CPSR after the one we want to
2453 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002454 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002455
Manman Renb1b3db62012-06-29 22:06:19 +00002456 // Check whether CmpInstr can be made redundant by the current instruction.
2457 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002458 Sub = &*I;
2459 break;
2460 }
2461
Evan Chengd757c882010-09-21 23:49:07 +00002462 if (I == B)
2463 // The 'and' is below the comparison instruction.
2464 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002465 }
2466
Manman Rendc8ad002012-05-11 01:30:47 +00002467 // Return false if no candidates exist.
2468 if (!MI && !Sub)
2469 return false;
2470
2471 // The single candidate is called MI.
2472 if (!MI) MI = Sub;
2473
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002474 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002475 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002476 return false;
2477
Bill Wendling7de9d522010-08-06 01:32:48 +00002478 switch (MI->getOpcode()) {
2479 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002480 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002481 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002482 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002483 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002484 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002485 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002486 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002487 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002488 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002489 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002490 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002491 case ARM::SBCri:
2492 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002493 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002494 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002495 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002496 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002497 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002498 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002499 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002500 case ARM::t2SBCri:
2501 case ARM::ANDrr:
2502 case ARM::ANDri:
2503 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002504 case ARM::t2ANDri:
2505 case ARM::ORRrr:
2506 case ARM::ORRri:
2507 case ARM::t2ORRrr:
2508 case ARM::t2ORRri:
2509 case ARM::EORrr:
2510 case ARM::EORri:
2511 case ARM::t2EORrr:
2512 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002513 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002514 // When checking against MI: if it's a conditional code that requires
2515 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002516 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2517 // If we are done with the basic block, we need to check whether CPSR is
2518 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002519 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2520 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002521 bool isSafe = false;
2522 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002523 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002524 while (!isSafe && ++I != E) {
2525 const MachineInstr &Instr = *I;
2526 for (unsigned IO = 0, EO = Instr.getNumOperands();
2527 !isSafe && IO != EO; ++IO) {
2528 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002529 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2530 isSafe = true;
2531 break;
2532 }
Evan Cheng425489d2011-03-23 22:52:04 +00002533 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2534 continue;
2535 if (MO.isDef()) {
2536 isSafe = true;
2537 break;
2538 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002539 // Condition code is after the operand before CPSR except for VSELs.
2540 ARMCC::CondCodes CC;
2541 bool IsInstrVSel = true;
2542 switch (Instr.getOpcode()) {
2543 default:
2544 IsInstrVSel = false;
2545 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2546 break;
2547 case ARM::VSELEQD:
2548 case ARM::VSELEQS:
2549 CC = ARMCC::EQ;
2550 break;
2551 case ARM::VSELGTD:
2552 case ARM::VSELGTS:
2553 CC = ARMCC::GT;
2554 break;
2555 case ARM::VSELGED:
2556 case ARM::VSELGES:
2557 CC = ARMCC::GE;
2558 break;
2559 case ARM::VSELVSS:
2560 case ARM::VSELVSD:
2561 CC = ARMCC::VS;
2562 break;
2563 }
2564
Manman Renb1b3db62012-06-29 22:06:19 +00002565 if (Sub) {
2566 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2567 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002568 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002569 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2570 // on CMP needs to be updated to be based on SUB.
2571 // Push the condition code operands to OperandsToUpdate.
2572 // If it is safe to remove CmpInstr, the condition code of these
2573 // operands will be modified.
2574 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002575 Sub->getOperand(2).getReg() == SrcReg) {
2576 // VSel doesn't support condition code update.
2577 if (IsInstrVSel)
2578 return false;
2579 OperandsToUpdate.push_back(
2580 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2581 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002582 } else {
2583 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002584 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002585 case ARMCC::EQ: // Z
2586 case ARMCC::NE: // Z
2587 case ARMCC::MI: // N
2588 case ARMCC::PL: // N
2589 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002590 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002591 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002592 case ARMCC::HS: // C
2593 case ARMCC::LO: // C
2594 case ARMCC::VS: // V
2595 case ARMCC::VC: // V
2596 case ARMCC::HI: // C Z
2597 case ARMCC::LS: // C Z
2598 case ARMCC::GE: // N V
2599 case ARMCC::LT: // N V
2600 case ARMCC::GT: // Z N V
2601 case ARMCC::LE: // Z N V
2602 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002603 return false;
2604 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002605 }
Evan Cheng425489d2011-03-23 22:52:04 +00002606 }
2607 }
2608
Manman Ren34cb93e2012-07-11 22:51:44 +00002609 // If CPSR is not killed nor re-defined, we should check whether it is
2610 // live-out. If it is live-out, do not optimize.
2611 if (!isSafe) {
2612 MachineBasicBlock *MBB = CmpInstr->getParent();
2613 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2614 SE = MBB->succ_end(); SI != SE; ++SI)
2615 if ((*SI)->isLiveIn(ARM::CPSR))
2616 return false;
2617 }
Evan Cheng425489d2011-03-23 22:52:04 +00002618
Evan Cheng65536472010-11-17 08:06:50 +00002619 // Toggle the optional operand to CPSR.
2620 MI->getOperand(5).setReg(ARM::CPSR);
2621 MI->getOperand(5).setIsDef(true);
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002622 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002623 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002624
2625 // Modify the condition code of operands in OperandsToUpdate.
2626 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2627 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002628 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2629 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002630 return true;
2631 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002632 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002633
2634 return false;
2635}
Evan Cheng367a5df2010-09-09 18:18:55 +00002636
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002637bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2638 MachineInstr *DefMI, unsigned Reg,
2639 MachineRegisterInfo *MRI) const {
2640 // Fold large immediates into add, sub, or, xor.
2641 unsigned DefOpc = DefMI->getOpcode();
2642 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2643 return false;
2644 if (!DefMI->getOperand(1).isImm())
2645 // Could be t2MOVi32imm <ga:xx>
2646 return false;
2647
2648 if (!MRI->hasOneNonDBGUse(Reg))
2649 return false;
2650
Evan Chenga2b48d92012-03-26 23:31:00 +00002651 const MCInstrDesc &DefMCID = DefMI->getDesc();
2652 if (DefMCID.hasOptionalDef()) {
2653 unsigned NumOps = DefMCID.getNumOperands();
2654 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2655 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2656 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2657 // to delete DefMI.
2658 return false;
2659 }
2660
2661 const MCInstrDesc &UseMCID = UseMI->getDesc();
2662 if (UseMCID.hasOptionalDef()) {
2663 unsigned NumOps = UseMCID.getNumOperands();
2664 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2665 // If the instruction sets the flag, do not attempt this optimization
2666 // since it may change the semantics of the code.
2667 return false;
2668 }
2669
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002670 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002671 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002672 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002673 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002674 bool Commute = false;
2675 switch (UseOpc) {
2676 default: return false;
2677 case ARM::SUBrr:
2678 case ARM::ADDrr:
2679 case ARM::ORRrr:
2680 case ARM::EORrr:
2681 case ARM::t2SUBrr:
2682 case ARM::t2ADDrr:
2683 case ARM::t2ORRrr:
2684 case ARM::t2EORrr: {
2685 Commute = UseMI->getOperand(2).getReg() != Reg;
2686 switch (UseOpc) {
2687 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002688 case ARM::ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002689 case ARM::SUBrr: {
2690 if (UseOpc == ARM::SUBrr && Commute)
2691 return false;
2692
2693 // ADD/SUB are special because they're essentially the same operation, so
2694 // we can handle a larger range of immediates.
2695 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
2696 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
2697 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
2698 ImmVal = -ImmVal;
2699 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
2700 } else
2701 return false;
2702 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2703 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2704 break;
2705 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002706 case ARM::ORRrr:
2707 case ARM::EORrr: {
2708 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2709 return false;
2710 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2711 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2712 switch (UseOpc) {
2713 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002714 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2715 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2716 }
2717 break;
2718 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002719 case ARM::t2ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002720 case ARM::t2SUBrr: {
2721 if (UseOpc == ARM::t2SUBrr && Commute)
2722 return false;
2723
2724 // ADD/SUB are special because they're essentially the same operation, so
2725 // we can handle a larger range of immediates.
2726 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2727 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
2728 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
2729 ImmVal = -ImmVal;
2730 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
2731 } else
2732 return false;
2733 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2734 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2735 break;
2736 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002737 case ARM::t2ORRrr:
2738 case ARM::t2EORrr: {
2739 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2740 return false;
2741 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2742 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2743 switch (UseOpc) {
2744 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002745 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2746 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2747 }
2748 break;
2749 }
2750 }
2751 }
2752 }
2753
2754 unsigned OpIdx = Commute ? 2 : 1;
2755 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2756 bool isKill = UseMI->getOperand(OpIdx).isKill();
2757 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2758 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002759 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002760 get(NewUseOpc), NewReg)
2761 .addReg(Reg1, getKillRegState(isKill))
2762 .addImm(SOImmValV1)));
2763 UseMI->setDesc(get(NewUseOpc));
2764 UseMI->getOperand(1).setReg(NewReg);
2765 UseMI->getOperand(1).setIsKill();
2766 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2767 DefMI->eraseFromParent();
2768 return true;
2769}
2770
Bob Wilsone8a549c2012-09-29 21:43:49 +00002771static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2772 const MachineInstr *MI) {
2773 switch (MI->getOpcode()) {
2774 default: {
2775 const MCInstrDesc &Desc = MI->getDesc();
2776 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2777 assert(UOps >= 0 && "bad # UOps");
2778 return UOps;
2779 }
2780
2781 case ARM::LDRrs:
2782 case ARM::LDRBrs:
2783 case ARM::STRrs:
2784 case ARM::STRBrs: {
2785 unsigned ShOpVal = MI->getOperand(3).getImm();
2786 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2787 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2788 if (!isSub &&
2789 (ShImm == 0 ||
2790 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2791 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2792 return 1;
2793 return 2;
2794 }
2795
2796 case ARM::LDRH:
2797 case ARM::STRH: {
2798 if (!MI->getOperand(2).getReg())
2799 return 1;
2800
2801 unsigned ShOpVal = MI->getOperand(3).getImm();
2802 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2803 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2804 if (!isSub &&
2805 (ShImm == 0 ||
2806 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2807 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2808 return 1;
2809 return 2;
2810 }
2811
2812 case ARM::LDRSB:
2813 case ARM::LDRSH:
2814 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2815
2816 case ARM::LDRSB_POST:
2817 case ARM::LDRSH_POST: {
2818 unsigned Rt = MI->getOperand(0).getReg();
2819 unsigned Rm = MI->getOperand(3).getReg();
2820 return (Rt == Rm) ? 4 : 3;
2821 }
2822
2823 case ARM::LDR_PRE_REG:
2824 case ARM::LDRB_PRE_REG: {
2825 unsigned Rt = MI->getOperand(0).getReg();
2826 unsigned Rm = MI->getOperand(3).getReg();
2827 if (Rt == Rm)
2828 return 3;
2829 unsigned ShOpVal = MI->getOperand(4).getImm();
2830 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2831 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2832 if (!isSub &&
2833 (ShImm == 0 ||
2834 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2835 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2836 return 2;
2837 return 3;
2838 }
2839
2840 case ARM::STR_PRE_REG:
2841 case ARM::STRB_PRE_REG: {
2842 unsigned ShOpVal = MI->getOperand(4).getImm();
2843 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2844 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2845 if (!isSub &&
2846 (ShImm == 0 ||
2847 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2848 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2849 return 2;
2850 return 3;
2851 }
2852
2853 case ARM::LDRH_PRE:
2854 case ARM::STRH_PRE: {
2855 unsigned Rt = MI->getOperand(0).getReg();
2856 unsigned Rm = MI->getOperand(3).getReg();
2857 if (!Rm)
2858 return 2;
2859 if (Rt == Rm)
2860 return 3;
2861 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2862 ? 3 : 2;
2863 }
2864
2865 case ARM::LDR_POST_REG:
2866 case ARM::LDRB_POST_REG:
2867 case ARM::LDRH_POST: {
2868 unsigned Rt = MI->getOperand(0).getReg();
2869 unsigned Rm = MI->getOperand(3).getReg();
2870 return (Rt == Rm) ? 3 : 2;
2871 }
2872
2873 case ARM::LDR_PRE_IMM:
2874 case ARM::LDRB_PRE_IMM:
2875 case ARM::LDR_POST_IMM:
2876 case ARM::LDRB_POST_IMM:
2877 case ARM::STRB_POST_IMM:
2878 case ARM::STRB_POST_REG:
2879 case ARM::STRB_PRE_IMM:
2880 case ARM::STRH_POST:
2881 case ARM::STR_POST_IMM:
2882 case ARM::STR_POST_REG:
2883 case ARM::STR_PRE_IMM:
2884 return 2;
2885
2886 case ARM::LDRSB_PRE:
2887 case ARM::LDRSH_PRE: {
2888 unsigned Rm = MI->getOperand(3).getReg();
2889 if (Rm == 0)
2890 return 3;
2891 unsigned Rt = MI->getOperand(0).getReg();
2892 if (Rt == Rm)
2893 return 4;
2894 unsigned ShOpVal = MI->getOperand(4).getImm();
2895 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2896 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2897 if (!isSub &&
2898 (ShImm == 0 ||
2899 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2900 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2901 return 3;
2902 return 4;
2903 }
2904
2905 case ARM::LDRD: {
2906 unsigned Rt = MI->getOperand(0).getReg();
2907 unsigned Rn = MI->getOperand(2).getReg();
2908 unsigned Rm = MI->getOperand(3).getReg();
2909 if (Rm)
2910 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2911 return (Rt == Rn) ? 3 : 2;
2912 }
2913
2914 case ARM::STRD: {
2915 unsigned Rm = MI->getOperand(3).getReg();
2916 if (Rm)
2917 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2918 return 2;
2919 }
2920
2921 case ARM::LDRD_POST:
2922 case ARM::t2LDRD_POST:
2923 return 3;
2924
2925 case ARM::STRD_POST:
2926 case ARM::t2STRD_POST:
2927 return 4;
2928
2929 case ARM::LDRD_PRE: {
2930 unsigned Rt = MI->getOperand(0).getReg();
2931 unsigned Rn = MI->getOperand(3).getReg();
2932 unsigned Rm = MI->getOperand(4).getReg();
2933 if (Rm)
2934 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2935 return (Rt == Rn) ? 4 : 3;
2936 }
2937
2938 case ARM::t2LDRD_PRE: {
2939 unsigned Rt = MI->getOperand(0).getReg();
2940 unsigned Rn = MI->getOperand(3).getReg();
2941 return (Rt == Rn) ? 4 : 3;
2942 }
2943
2944 case ARM::STRD_PRE: {
2945 unsigned Rm = MI->getOperand(4).getReg();
2946 if (Rm)
2947 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2948 return 3;
2949 }
2950
2951 case ARM::t2STRD_PRE:
2952 return 3;
2953
2954 case ARM::t2LDR_POST:
2955 case ARM::t2LDRB_POST:
2956 case ARM::t2LDRB_PRE:
2957 case ARM::t2LDRSBi12:
2958 case ARM::t2LDRSBi8:
2959 case ARM::t2LDRSBpci:
2960 case ARM::t2LDRSBs:
2961 case ARM::t2LDRH_POST:
2962 case ARM::t2LDRH_PRE:
2963 case ARM::t2LDRSBT:
2964 case ARM::t2LDRSB_POST:
2965 case ARM::t2LDRSB_PRE:
2966 case ARM::t2LDRSH_POST:
2967 case ARM::t2LDRSH_PRE:
2968 case ARM::t2LDRSHi12:
2969 case ARM::t2LDRSHi8:
2970 case ARM::t2LDRSHpci:
2971 case ARM::t2LDRSHs:
2972 return 2;
2973
2974 case ARM::t2LDRDi8: {
2975 unsigned Rt = MI->getOperand(0).getReg();
2976 unsigned Rn = MI->getOperand(2).getReg();
2977 return (Rt == Rn) ? 3 : 2;
2978 }
2979
2980 case ARM::t2STRB_POST:
2981 case ARM::t2STRB_PRE:
2982 case ARM::t2STRBs:
2983 case ARM::t2STRDi8:
2984 case ARM::t2STRH_POST:
2985 case ARM::t2STRH_PRE:
2986 case ARM::t2STRHs:
2987 case ARM::t2STR_POST:
2988 case ARM::t2STR_PRE:
2989 case ARM::t2STRs:
2990 return 2;
2991 }
2992}
2993
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002994// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2995// can't be easily determined return 0 (missing MachineMemOperand).
2996//
2997// FIXME: The current MachineInstr design does not support relying on machine
2998// mem operands to determine the width of a memory access. Instead, we expect
2999// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003000// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003001// two reasons:
3002//
3003// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3004// operands. This is much more dangerous than using the MachineMemOperand
3005// sizes because CodeGen passes can insert/remove optional machine operands. In
3006// fact, it's totally incorrect for preRA passes and appears to be wrong for
3007// postRA passes as well.
3008//
3009// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3010// machine model that calls this should handle the unknown (zero size) case.
3011//
3012// Long term, we should require a target hook that verifies MachineMemOperand
3013// sizes during MC lowering. That target hook should be local to MC lowering
3014// because we can't ensure that it is aware of other MI forms. Doing this will
3015// ensure that MachineMemOperands are correctly propagated through all passes.
3016unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
3017 unsigned Size = 0;
3018 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
3019 E = MI->memoperands_end(); I != E; ++I) {
3020 Size += (*I)->getSize();
3021 }
3022 return Size / 4;
3023}
3024
Evan Cheng367a5df2010-09-09 18:18:55 +00003025unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00003026ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3027 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003028 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003029 return 1;
3030
Evan Cheng6cc775f2011-06-28 19:10:37 +00003031 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003032 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003033 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003034 if (ItinUOps >= 0) {
3035 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3036 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3037
Andrew Trickf161e392012-07-02 18:10:42 +00003038 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003039 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003040
3041 unsigned Opc = MI->getOpcode();
3042 switch (Opc) {
3043 default:
3044 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003045 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003046 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003047 return 2;
3048
3049 // The number of uOps for load / store multiple are determined by the number
3050 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003051 //
Evan Chengbf407072010-09-10 01:29:16 +00003052 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3053 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003054 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003055 //
Evan Chengbf407072010-09-10 01:29:16 +00003056 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003057 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3058 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3059 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003060 case ARM::VLDMDIA_UPD:
3061 case ARM::VLDMDDB_UPD:
3062 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003063 case ARM::VLDMSIA_UPD:
3064 case ARM::VLDMSDB_UPD:
3065 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003066 case ARM::VSTMDIA_UPD:
3067 case ARM::VSTMDDB_UPD:
3068 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003069 case ARM::VSTMSIA_UPD:
3070 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00003071 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
3072 return (NumRegs / 2) + (NumRegs % 2) + 1;
3073 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003074
3075 case ARM::LDMIA_RET:
3076 case ARM::LDMIA:
3077 case ARM::LDMDA:
3078 case ARM::LDMDB:
3079 case ARM::LDMIB:
3080 case ARM::LDMIA_UPD:
3081 case ARM::LDMDA_UPD:
3082 case ARM::LDMDB_UPD:
3083 case ARM::LDMIB_UPD:
3084 case ARM::STMIA:
3085 case ARM::STMDA:
3086 case ARM::STMDB:
3087 case ARM::STMIB:
3088 case ARM::STMIA_UPD:
3089 case ARM::STMDA_UPD:
3090 case ARM::STMDB_UPD:
3091 case ARM::STMIB_UPD:
3092 case ARM::tLDMIA:
3093 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003094 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003095 case ARM::tPOP_RET:
3096 case ARM::tPOP:
3097 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003098 case ARM::t2LDMIA_RET:
3099 case ARM::t2LDMIA:
3100 case ARM::t2LDMDB:
3101 case ARM::t2LDMIA_UPD:
3102 case ARM::t2LDMDB_UPD:
3103 case ARM::t2STMIA:
3104 case ARM::t2STMDB:
3105 case ARM::t2STMIA_UPD:
3106 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00003107 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003108 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003109 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
3110 switch (Opc) {
3111 default: break;
3112 case ARM::VLDMDIA_UPD:
3113 case ARM::VLDMDDB_UPD:
3114 case ARM::VLDMSIA_UPD:
3115 case ARM::VLDMSDB_UPD:
3116 case ARM::VSTMDIA_UPD:
3117 case ARM::VSTMDDB_UPD:
3118 case ARM::VSTMSIA_UPD:
3119 case ARM::VSTMSDB_UPD:
3120 case ARM::LDMIA_UPD:
3121 case ARM::LDMDA_UPD:
3122 case ARM::LDMDB_UPD:
3123 case ARM::LDMIB_UPD:
3124 case ARM::STMIA_UPD:
3125 case ARM::STMDA_UPD:
3126 case ARM::STMDB_UPD:
3127 case ARM::STMIB_UPD:
3128 case ARM::tLDMIA_UPD:
3129 case ARM::tSTMIA_UPD:
3130 case ARM::t2LDMIA_UPD:
3131 case ARM::t2LDMDB_UPD:
3132 case ARM::t2STMIA_UPD:
3133 case ARM::t2STMDB_UPD:
3134 ++UOps; // One for base register writeback.
3135 break;
3136 case ARM::LDMIA_RET:
3137 case ARM::tPOP_RET:
3138 case ARM::t2LDMIA_RET:
3139 UOps += 2; // One for base reg wb, one for write to pc.
3140 break;
3141 }
3142 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00003143 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003144 if (NumRegs < 4)
3145 return 2;
3146 // 4 registers would be issued: 2, 2.
3147 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00003148 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003149 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00003150 ++A8UOps;
3151 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003152 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00003153 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003154 // If there are odd number of registers or if it's not 64-bit aligned,
3155 // then it takes an extra AGU (Address Generation Unit) cycle.
3156 if ((NumRegs % 2) ||
3157 !MI->hasOneMemOperand() ||
3158 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00003159 ++A9UOps;
3160 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00003161 } else {
3162 // Assume the worst.
3163 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003164 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003165 }
3166 }
3167}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003168
3169int
Evan Cheng412e37b2010-10-07 23:12:15 +00003170ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003171 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003172 unsigned DefClass,
3173 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003174 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003175 if (RegNo <= 0)
3176 // Def is the address writeback.
3177 return ItinData->getOperandCycle(DefClass, DefIdx);
3178
3179 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003180 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003181 // (regno / 2) + (regno % 2) + 1
3182 DefCycle = RegNo / 2 + 1;
3183 if (RegNo % 2)
3184 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003185 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003186 DefCycle = RegNo;
3187 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003188
Evan Cheng6cc775f2011-06-28 19:10:37 +00003189 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003190 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003191 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003192 case ARM::VLDMSIA_UPD:
3193 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003194 isSLoad = true;
3195 break;
3196 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003197
Evan Cheng412e37b2010-10-07 23:12:15 +00003198 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3199 // then it takes an extra cycle.
3200 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3201 ++DefCycle;
3202 } else {
3203 // Assume the worst.
3204 DefCycle = RegNo + 2;
3205 }
3206
3207 return DefCycle;
3208}
3209
3210int
3211ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003212 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003213 unsigned DefClass,
3214 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003215 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003216 if (RegNo <= 0)
3217 // Def is the address writeback.
3218 return ItinData->getOperandCycle(DefClass, DefIdx);
3219
3220 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003221 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003222 // 4 registers would be issued: 1, 2, 1.
3223 // 5 registers would be issued: 1, 2, 2.
3224 DefCycle = RegNo / 2;
3225 if (DefCycle < 1)
3226 DefCycle = 1;
3227 // Result latency is issue cycle + 2: E2.
3228 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003229 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003230 DefCycle = (RegNo / 2);
3231 // If there are odd number of registers or if it's not 64-bit aligned,
3232 // then it takes an extra AGU (Address Generation Unit) cycle.
3233 if ((RegNo % 2) || DefAlign < 8)
3234 ++DefCycle;
3235 // Result latency is AGU cycles + 2.
3236 DefCycle += 2;
3237 } else {
3238 // Assume the worst.
3239 DefCycle = RegNo + 2;
3240 }
3241
3242 return DefCycle;
3243}
3244
3245int
3246ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003247 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003248 unsigned UseClass,
3249 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003250 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003251 if (RegNo <= 0)
3252 return ItinData->getOperandCycle(UseClass, UseIdx);
3253
3254 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003255 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003256 // (regno / 2) + (regno % 2) + 1
3257 UseCycle = RegNo / 2 + 1;
3258 if (RegNo % 2)
3259 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003260 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003261 UseCycle = RegNo;
3262 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003263
Evan Cheng6cc775f2011-06-28 19:10:37 +00003264 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003265 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003266 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003267 case ARM::VSTMSIA_UPD:
3268 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003269 isSStore = true;
3270 break;
3271 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003272
Evan Cheng412e37b2010-10-07 23:12:15 +00003273 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3274 // then it takes an extra cycle.
3275 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3276 ++UseCycle;
3277 } else {
3278 // Assume the worst.
3279 UseCycle = RegNo + 2;
3280 }
3281
3282 return UseCycle;
3283}
3284
3285int
3286ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003287 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003288 unsigned UseClass,
3289 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003290 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003291 if (RegNo <= 0)
3292 return ItinData->getOperandCycle(UseClass, UseIdx);
3293
3294 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003295 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003296 UseCycle = RegNo / 2;
3297 if (UseCycle < 2)
3298 UseCycle = 2;
3299 // Read in E3.
3300 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003301 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003302 UseCycle = (RegNo / 2);
3303 // If there are odd number of registers or if it's not 64-bit aligned,
3304 // then it takes an extra AGU (Address Generation Unit) cycle.
3305 if ((RegNo % 2) || UseAlign < 8)
3306 ++UseCycle;
3307 } else {
3308 // Assume the worst.
3309 UseCycle = 1;
3310 }
3311 return UseCycle;
3312}
3313
3314int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003315ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003316 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003317 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003318 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003319 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003320 unsigned DefClass = DefMCID.getSchedClass();
3321 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003322
Evan Cheng6cc775f2011-06-28 19:10:37 +00003323 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003324 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3325
3326 // This may be a def / use of a variable_ops instruction, the operand
3327 // latency might be determinable dynamically. Let the target try to
3328 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003329 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003330 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003331 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003332 default:
3333 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3334 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003335
3336 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003337 case ARM::VLDMDIA_UPD:
3338 case ARM::VLDMDDB_UPD:
3339 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003340 case ARM::VLDMSIA_UPD:
3341 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003342 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003343 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003344
3345 case ARM::LDMIA_RET:
3346 case ARM::LDMIA:
3347 case ARM::LDMDA:
3348 case ARM::LDMDB:
3349 case ARM::LDMIB:
3350 case ARM::LDMIA_UPD:
3351 case ARM::LDMDA_UPD:
3352 case ARM::LDMDB_UPD:
3353 case ARM::LDMIB_UPD:
3354 case ARM::tLDMIA:
3355 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003356 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003357 case ARM::t2LDMIA_RET:
3358 case ARM::t2LDMIA:
3359 case ARM::t2LDMDB:
3360 case ARM::t2LDMIA_UPD:
3361 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003362 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003363 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003364 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003365 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003366
3367 if (DefCycle == -1)
3368 // We can't seem to determine the result latency of the def, assume it's 2.
3369 DefCycle = 2;
3370
3371 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003372 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003373 default:
3374 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3375 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003376
3377 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003378 case ARM::VSTMDIA_UPD:
3379 case ARM::VSTMDDB_UPD:
3380 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003381 case ARM::VSTMSIA_UPD:
3382 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003383 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003384 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003385
3386 case ARM::STMIA:
3387 case ARM::STMDA:
3388 case ARM::STMDB:
3389 case ARM::STMIB:
3390 case ARM::STMIA_UPD:
3391 case ARM::STMDA_UPD:
3392 case ARM::STMDB_UPD:
3393 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003394 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003395 case ARM::tPOP_RET:
3396 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003397 case ARM::t2STMIA:
3398 case ARM::t2STMDB:
3399 case ARM::t2STMIA_UPD:
3400 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003401 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003402 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003403 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003404
3405 if (UseCycle == -1)
3406 // Assume it's read in the first stage.
3407 UseCycle = 1;
3408
3409 UseCycle = DefCycle - UseCycle + 1;
3410 if (UseCycle > 0) {
3411 if (LdmBypass) {
3412 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3413 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003414 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003415 UseClass, UseIdx))
3416 --UseCycle;
3417 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003418 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003419 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003420 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003421 }
3422
3423 return UseCycle;
3424}
3425
Evan Cheng7fae11b2011-12-14 02:11:42 +00003426static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003427 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003428 unsigned &DefIdx, unsigned &Dist) {
3429 Dist = 0;
3430
3431 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003432 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003433 assert(II->isInsideBundle() && "Empty bundle?");
3434
3435 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003436 while (II->isInsideBundle()) {
3437 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3438 if (Idx != -1)
3439 break;
3440 --II;
3441 ++Dist;
3442 }
3443
3444 assert(Idx != -1 && "Cannot find bundled definition!");
3445 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003446 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003447}
3448
3449static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003450 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003451 unsigned &UseIdx, unsigned &Dist) {
3452 Dist = 0;
3453
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00003454 MachineBasicBlock::const_instr_iterator II = ++MI->getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003455 assert(II->isInsideBundle() && "Empty bundle?");
3456 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3457
3458 // FIXME: This doesn't properly handle multiple uses.
3459 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003460 while (II != E && II->isInsideBundle()) {
3461 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3462 if (Idx != -1)
3463 break;
3464 if (II->getOpcode() != ARM::t2IT)
3465 ++Dist;
3466 ++II;
3467 }
3468
Evan Chengda103bf2011-12-14 20:00:08 +00003469 if (Idx == -1) {
3470 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003471 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003472 }
3473
Evan Cheng7fae11b2011-12-14 02:11:42 +00003474 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003475 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003476}
3477
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003478/// Return the number of cycles to add to (or subtract from) the static
3479/// itinerary based on the def opcode and alignment. The caller will ensure that
3480/// adjusted latency is at least one cycle.
3481static int adjustDefLatency(const ARMSubtarget &Subtarget,
3482 const MachineInstr *DefMI,
3483 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3484 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003485 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003486 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3487 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003488 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003489 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003490 case ARM::LDRrs:
3491 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003492 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3493 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3494 if (ShImm == 0 ||
3495 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003496 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003497 break;
3498 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003499 case ARM::t2LDRs:
3500 case ARM::t2LDRBs:
3501 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003502 case ARM::t2LDRSHs: {
3503 // Thumb2 mode: lsl only.
3504 unsigned ShAmt = DefMI->getOperand(3).getImm();
3505 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003506 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003507 break;
3508 }
3509 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003510 } else if (Subtarget.isSwift()) {
3511 // FIXME: Properly handle all of the latency adjustments for address
3512 // writeback.
3513 switch (DefMCID->getOpcode()) {
3514 default: break;
3515 case ARM::LDRrs:
3516 case ARM::LDRBrs: {
3517 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3518 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3519 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3520 if (!isSub &&
3521 (ShImm == 0 ||
3522 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3523 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3524 Adjust -= 2;
3525 else if (!isSub &&
3526 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3527 --Adjust;
3528 break;
3529 }
3530 case ARM::t2LDRs:
3531 case ARM::t2LDRBs:
3532 case ARM::t2LDRHs:
3533 case ARM::t2LDRSHs: {
3534 // Thumb2 mode: lsl only.
3535 unsigned ShAmt = DefMI->getOperand(3).getImm();
3536 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3537 Adjust -= 2;
3538 break;
3539 }
3540 }
Evan Chengff310732010-10-28 06:47:08 +00003541 }
3542
Silviu Barangab47bb942012-09-13 15:05:10 +00003543 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003544 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003545 default: break;
3546 case ARM::VLD1q8:
3547 case ARM::VLD1q16:
3548 case ARM::VLD1q32:
3549 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003550 case ARM::VLD1q8wb_fixed:
3551 case ARM::VLD1q16wb_fixed:
3552 case ARM::VLD1q32wb_fixed:
3553 case ARM::VLD1q64wb_fixed:
3554 case ARM::VLD1q8wb_register:
3555 case ARM::VLD1q16wb_register:
3556 case ARM::VLD1q32wb_register:
3557 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003558 case ARM::VLD2d8:
3559 case ARM::VLD2d16:
3560 case ARM::VLD2d32:
3561 case ARM::VLD2q8:
3562 case ARM::VLD2q16:
3563 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003564 case ARM::VLD2d8wb_fixed:
3565 case ARM::VLD2d16wb_fixed:
3566 case ARM::VLD2d32wb_fixed:
3567 case ARM::VLD2q8wb_fixed:
3568 case ARM::VLD2q16wb_fixed:
3569 case ARM::VLD2q32wb_fixed:
3570 case ARM::VLD2d8wb_register:
3571 case ARM::VLD2d16wb_register:
3572 case ARM::VLD2d32wb_register:
3573 case ARM::VLD2q8wb_register:
3574 case ARM::VLD2q16wb_register:
3575 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003576 case ARM::VLD3d8:
3577 case ARM::VLD3d16:
3578 case ARM::VLD3d32:
3579 case ARM::VLD1d64T:
3580 case ARM::VLD3d8_UPD:
3581 case ARM::VLD3d16_UPD:
3582 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003583 case ARM::VLD1d64Twb_fixed:
3584 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003585 case ARM::VLD3q8_UPD:
3586 case ARM::VLD3q16_UPD:
3587 case ARM::VLD3q32_UPD:
3588 case ARM::VLD4d8:
3589 case ARM::VLD4d16:
3590 case ARM::VLD4d32:
3591 case ARM::VLD1d64Q:
3592 case ARM::VLD4d8_UPD:
3593 case ARM::VLD4d16_UPD:
3594 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003595 case ARM::VLD1d64Qwb_fixed:
3596 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003597 case ARM::VLD4q8_UPD:
3598 case ARM::VLD4q16_UPD:
3599 case ARM::VLD4q32_UPD:
3600 case ARM::VLD1DUPq8:
3601 case ARM::VLD1DUPq16:
3602 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003603 case ARM::VLD1DUPq8wb_fixed:
3604 case ARM::VLD1DUPq16wb_fixed:
3605 case ARM::VLD1DUPq32wb_fixed:
3606 case ARM::VLD1DUPq8wb_register:
3607 case ARM::VLD1DUPq16wb_register:
3608 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003609 case ARM::VLD2DUPd8:
3610 case ARM::VLD2DUPd16:
3611 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003612 case ARM::VLD2DUPd8wb_fixed:
3613 case ARM::VLD2DUPd16wb_fixed:
3614 case ARM::VLD2DUPd32wb_fixed:
3615 case ARM::VLD2DUPd8wb_register:
3616 case ARM::VLD2DUPd16wb_register:
3617 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003618 case ARM::VLD4DUPd8:
3619 case ARM::VLD4DUPd16:
3620 case ARM::VLD4DUPd32:
3621 case ARM::VLD4DUPd8_UPD:
3622 case ARM::VLD4DUPd16_UPD:
3623 case ARM::VLD4DUPd32_UPD:
3624 case ARM::VLD1LNd8:
3625 case ARM::VLD1LNd16:
3626 case ARM::VLD1LNd32:
3627 case ARM::VLD1LNd8_UPD:
3628 case ARM::VLD1LNd16_UPD:
3629 case ARM::VLD1LNd32_UPD:
3630 case ARM::VLD2LNd8:
3631 case ARM::VLD2LNd16:
3632 case ARM::VLD2LNd32:
3633 case ARM::VLD2LNq16:
3634 case ARM::VLD2LNq32:
3635 case ARM::VLD2LNd8_UPD:
3636 case ARM::VLD2LNd16_UPD:
3637 case ARM::VLD2LNd32_UPD:
3638 case ARM::VLD2LNq16_UPD:
3639 case ARM::VLD2LNq32_UPD:
3640 case ARM::VLD4LNd8:
3641 case ARM::VLD4LNd16:
3642 case ARM::VLD4LNd32:
3643 case ARM::VLD4LNq16:
3644 case ARM::VLD4LNq32:
3645 case ARM::VLD4LNd8_UPD:
3646 case ARM::VLD4LNd16_UPD:
3647 case ARM::VLD4LNd32_UPD:
3648 case ARM::VLD4LNq16_UPD:
3649 case ARM::VLD4LNq32_UPD:
3650 // If the address is not 64-bit aligned, the latencies of these
3651 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003652 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003653 break;
3654 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003655 }
3656 return Adjust;
3657}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003658
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003659
3660
3661int
3662ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3663 const MachineInstr *DefMI, unsigned DefIdx,
3664 const MachineInstr *UseMI,
3665 unsigned UseIdx) const {
3666 // No operand latency. The caller may fall back to getInstrLatency.
3667 if (!ItinData || ItinData->isEmpty())
3668 return -1;
3669
3670 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3671 unsigned Reg = DefMO.getReg();
3672 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3673 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3674
3675 unsigned DefAdj = 0;
3676 if (DefMI->isBundle()) {
3677 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3678 DefMCID = &DefMI->getDesc();
3679 }
3680 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3681 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3682 return 1;
3683 }
3684
3685 unsigned UseAdj = 0;
3686 if (UseMI->isBundle()) {
3687 unsigned NewUseIdx;
3688 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3689 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003690 if (!NewUseMI)
3691 return -1;
3692
3693 UseMI = NewUseMI;
3694 UseIdx = NewUseIdx;
3695 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003696 }
3697
3698 if (Reg == ARM::CPSR) {
3699 if (DefMI->getOpcode() == ARM::FMSTAT) {
3700 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003701 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003702 }
3703
3704 // CPSR set and branch can be paired in the same cycle.
3705 if (UseMI->isBranch())
3706 return 0;
3707
3708 // Otherwise it takes the instruction latency (generally one).
3709 unsigned Latency = getInstrLatency(ItinData, DefMI);
3710
3711 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3712 // its uses. Instructions which are otherwise scheduled between them may
3713 // incur a code size penalty (not able to use the CPSR setting 16-bit
3714 // instructions).
3715 if (Latency > 0 && Subtarget.isThumb2()) {
3716 const MachineFunction *MF = DefMI->getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003717 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003718 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003719 --Latency;
3720 }
3721 return Latency;
3722 }
3723
Andrew Trick77d0b882012-06-22 02:50:33 +00003724 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3725 return -1;
3726
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003727 unsigned DefAlign = DefMI->hasOneMemOperand()
3728 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3729 unsigned UseAlign = UseMI->hasOneMemOperand()
3730 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3731
3732 // Get the itinerary's latency if possible, and handle variable_ops.
3733 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3734 *UseMCID, UseIdx, UseAlign);
3735 // Unable to find operand latency. The caller may resort to getInstrLatency.
3736 if (Latency < 0)
3737 return Latency;
3738
3739 // Adjust for IT block position.
3740 int Adj = DefAdj + UseAdj;
3741
3742 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3743 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3744 if (Adj >= 0 || (int)Latency > -Adj) {
3745 return Latency + Adj;
3746 }
3747 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003748 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003749}
3750
3751int
3752ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3753 SDNode *DefNode, unsigned DefIdx,
3754 SDNode *UseNode, unsigned UseIdx) const {
3755 if (!DefNode->isMachineOpcode())
3756 return 1;
3757
Evan Cheng6cc775f2011-06-28 19:10:37 +00003758 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003759
Evan Cheng6cc775f2011-06-28 19:10:37 +00003760 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003761 return 0;
3762
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003763 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003764 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003765
Evan Cheng6c1414f2010-10-29 18:09:28 +00003766 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003767 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003768 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003769 return Latency <= 2 ? 1 : Latency - 1;
3770 else
3771 return Latency <= 3 ? 1 : Latency - 2;
3772 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003773
Evan Cheng6cc775f2011-06-28 19:10:37 +00003774 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003775 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3776 unsigned DefAlign = !DefMN->memoperands_empty()
3777 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3778 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3779 unsigned UseAlign = !UseMN->memoperands_empty()
3780 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003781 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3782 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003783
3784 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003785 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3786 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003787 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3788 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003789 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003790 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003791 case ARM::LDRrs:
3792 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003793 unsigned ShOpVal =
3794 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3795 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3796 if (ShImm == 0 ||
3797 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3798 --Latency;
3799 break;
3800 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003801 case ARM::t2LDRs:
3802 case ARM::t2LDRBs:
3803 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003804 case ARM::t2LDRSHs: {
3805 // Thumb2 mode: lsl only.
3806 unsigned ShAmt =
3807 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3808 if (ShAmt == 0 || ShAmt == 2)
3809 --Latency;
3810 break;
3811 }
3812 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003813 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3814 // FIXME: Properly handle all of the latency adjustments for address
3815 // writeback.
3816 switch (DefMCID.getOpcode()) {
3817 default: break;
3818 case ARM::LDRrs:
3819 case ARM::LDRBrs: {
3820 unsigned ShOpVal =
3821 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3822 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3823 if (ShImm == 0 ||
3824 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3825 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3826 Latency -= 2;
3827 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3828 --Latency;
3829 break;
3830 }
3831 case ARM::t2LDRs:
3832 case ARM::t2LDRBs:
3833 case ARM::t2LDRHs:
3834 case ARM::t2LDRSHs: {
3835 // Thumb2 mode: lsl 0-3 only.
3836 Latency -= 2;
3837 break;
3838 }
3839 }
Evan Chengff310732010-10-28 06:47:08 +00003840 }
3841
Silviu Barangab47bb942012-09-13 15:05:10 +00003842 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003843 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003844 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003845 case ARM::VLD1q8:
3846 case ARM::VLD1q16:
3847 case ARM::VLD1q32:
3848 case ARM::VLD1q64:
3849 case ARM::VLD1q8wb_register:
3850 case ARM::VLD1q16wb_register:
3851 case ARM::VLD1q32wb_register:
3852 case ARM::VLD1q64wb_register:
3853 case ARM::VLD1q8wb_fixed:
3854 case ARM::VLD1q16wb_fixed:
3855 case ARM::VLD1q32wb_fixed:
3856 case ARM::VLD1q64wb_fixed:
3857 case ARM::VLD2d8:
3858 case ARM::VLD2d16:
3859 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003860 case ARM::VLD2q8Pseudo:
3861 case ARM::VLD2q16Pseudo:
3862 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003863 case ARM::VLD2d8wb_fixed:
3864 case ARM::VLD2d16wb_fixed:
3865 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003866 case ARM::VLD2q8PseudoWB_fixed:
3867 case ARM::VLD2q16PseudoWB_fixed:
3868 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003869 case ARM::VLD2d8wb_register:
3870 case ARM::VLD2d16wb_register:
3871 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003872 case ARM::VLD2q8PseudoWB_register:
3873 case ARM::VLD2q16PseudoWB_register:
3874 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003875 case ARM::VLD3d8Pseudo:
3876 case ARM::VLD3d16Pseudo:
3877 case ARM::VLD3d32Pseudo:
3878 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003879 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003880 case ARM::VLD3d8Pseudo_UPD:
3881 case ARM::VLD3d16Pseudo_UPD:
3882 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003883 case ARM::VLD3q8Pseudo_UPD:
3884 case ARM::VLD3q16Pseudo_UPD:
3885 case ARM::VLD3q32Pseudo_UPD:
3886 case ARM::VLD3q8oddPseudo:
3887 case ARM::VLD3q16oddPseudo:
3888 case ARM::VLD3q32oddPseudo:
3889 case ARM::VLD3q8oddPseudo_UPD:
3890 case ARM::VLD3q16oddPseudo_UPD:
3891 case ARM::VLD3q32oddPseudo_UPD:
3892 case ARM::VLD4d8Pseudo:
3893 case ARM::VLD4d16Pseudo:
3894 case ARM::VLD4d32Pseudo:
3895 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003896 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003897 case ARM::VLD4d8Pseudo_UPD:
3898 case ARM::VLD4d16Pseudo_UPD:
3899 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003900 case ARM::VLD4q8Pseudo_UPD:
3901 case ARM::VLD4q16Pseudo_UPD:
3902 case ARM::VLD4q32Pseudo_UPD:
3903 case ARM::VLD4q8oddPseudo:
3904 case ARM::VLD4q16oddPseudo:
3905 case ARM::VLD4q32oddPseudo:
3906 case ARM::VLD4q8oddPseudo_UPD:
3907 case ARM::VLD4q16oddPseudo_UPD:
3908 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003909 case ARM::VLD1DUPq8:
3910 case ARM::VLD1DUPq16:
3911 case ARM::VLD1DUPq32:
3912 case ARM::VLD1DUPq8wb_fixed:
3913 case ARM::VLD1DUPq16wb_fixed:
3914 case ARM::VLD1DUPq32wb_fixed:
3915 case ARM::VLD1DUPq8wb_register:
3916 case ARM::VLD1DUPq16wb_register:
3917 case ARM::VLD1DUPq32wb_register:
3918 case ARM::VLD2DUPd8:
3919 case ARM::VLD2DUPd16:
3920 case ARM::VLD2DUPd32:
3921 case ARM::VLD2DUPd8wb_fixed:
3922 case ARM::VLD2DUPd16wb_fixed:
3923 case ARM::VLD2DUPd32wb_fixed:
3924 case ARM::VLD2DUPd8wb_register:
3925 case ARM::VLD2DUPd16wb_register:
3926 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003927 case ARM::VLD4DUPd8Pseudo:
3928 case ARM::VLD4DUPd16Pseudo:
3929 case ARM::VLD4DUPd32Pseudo:
3930 case ARM::VLD4DUPd8Pseudo_UPD:
3931 case ARM::VLD4DUPd16Pseudo_UPD:
3932 case ARM::VLD4DUPd32Pseudo_UPD:
3933 case ARM::VLD1LNq8Pseudo:
3934 case ARM::VLD1LNq16Pseudo:
3935 case ARM::VLD1LNq32Pseudo:
3936 case ARM::VLD1LNq8Pseudo_UPD:
3937 case ARM::VLD1LNq16Pseudo_UPD:
3938 case ARM::VLD1LNq32Pseudo_UPD:
3939 case ARM::VLD2LNd8Pseudo:
3940 case ARM::VLD2LNd16Pseudo:
3941 case ARM::VLD2LNd32Pseudo:
3942 case ARM::VLD2LNq16Pseudo:
3943 case ARM::VLD2LNq32Pseudo:
3944 case ARM::VLD2LNd8Pseudo_UPD:
3945 case ARM::VLD2LNd16Pseudo_UPD:
3946 case ARM::VLD2LNd32Pseudo_UPD:
3947 case ARM::VLD2LNq16Pseudo_UPD:
3948 case ARM::VLD2LNq32Pseudo_UPD:
3949 case ARM::VLD4LNd8Pseudo:
3950 case ARM::VLD4LNd16Pseudo:
3951 case ARM::VLD4LNd32Pseudo:
3952 case ARM::VLD4LNq16Pseudo:
3953 case ARM::VLD4LNq32Pseudo:
3954 case ARM::VLD4LNd8Pseudo_UPD:
3955 case ARM::VLD4LNd16Pseudo_UPD:
3956 case ARM::VLD4LNd32Pseudo_UPD:
3957 case ARM::VLD4LNq16Pseudo_UPD:
3958 case ARM::VLD4LNq32Pseudo_UPD:
3959 // If the address is not 64-bit aligned, the latencies of these
3960 // instructions increases by one.
3961 ++Latency;
3962 break;
3963 }
3964
Evan Chengff310732010-10-28 06:47:08 +00003965 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003966}
Evan Cheng63c76082010-10-19 18:58:51 +00003967
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003968unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
3969 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
3970 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003971 return 0;
3972
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003973 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003974 return 0;
3975
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003976 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003977
3978 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3979 // When predicated, CPSR is an additional source operand for CPSR updating
3980 // instructions, this apparently increases their latencies.
3981 return 1;
3982 }
3983 return 0;
3984}
3985
Andrew Trick45446062012-06-05 21:11:27 +00003986unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3987 const MachineInstr *MI,
3988 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003989 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3990 MI->isRegSequence() || MI->isImplicitDef())
3991 return 1;
3992
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003993 // An instruction scheduler typically runs on unbundled instructions, however
3994 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003995 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003996 unsigned Latency = 0;
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00003997 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
3998 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003999 while (++I != E && I->isInsideBundle()) {
4000 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00004001 Latency += getInstrLatency(ItinData, &*I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004002 }
4003 return Latency;
4004 }
4005
Evan Cheng6cc775f2011-06-28 19:10:37 +00004006 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004007 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004008 // When predicated, CPSR is an additional source operand for CPSR updating
4009 // instructions, this apparently increases their latencies.
4010 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004011 }
4012 // Be sure to call getStageLatency for an empty itinerary in case it has a
4013 // valid MinLatency property.
4014 if (!ItinData)
4015 return MI->mayLoad() ? 3 : 1;
4016
4017 unsigned Class = MCID.getSchedClass();
4018
4019 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004020 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004021 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004022
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004023 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004024 unsigned Latency = ItinData->getStageLatency(Class);
4025
4026 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4027 unsigned DefAlign = MI->hasOneMemOperand()
4028 ? (*MI->memoperands_begin())->getAlignment() : 0;
4029 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
4030 if (Adj >= 0 || (int)Latency > -Adj) {
4031 return Latency + Adj;
4032 }
4033 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004034}
4035
4036int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4037 SDNode *Node) const {
4038 if (!Node->isMachineOpcode())
4039 return 1;
4040
4041 if (!ItinData || ItinData->isEmpty())
4042 return 1;
4043
4044 unsigned Opcode = Node->getMachineOpcode();
4045 switch (Opcode) {
4046 default:
4047 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004048 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004049 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004050 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004051 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004052}
4053
Evan Cheng63c76082010-10-19 18:58:51 +00004054bool ARMBaseInstrInfo::
Matthias Braun88e21312015-06-13 03:42:11 +00004055hasHighOperandLatency(const TargetSchedModel &SchedModel,
Evan Cheng63c76082010-10-19 18:58:51 +00004056 const MachineRegisterInfo *MRI,
4057 const MachineInstr *DefMI, unsigned DefIdx,
4058 const MachineInstr *UseMI, unsigned UseIdx) const {
4059 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4060 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
4061 if (Subtarget.isCortexA8() &&
4062 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
4063 // CortexA8 VFP instructions are not pipelined.
4064 return true;
4065
4066 // Hoist VFP / NEON instructions with 4 or higher latency.
Matthias Braun88e21312015-06-13 03:42:11 +00004067 unsigned Latency
4068 = SchedModel.computeOperandLatency(DefMI, DefIdx, UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004069 if (Latency <= 3)
4070 return false;
4071 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4072 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4073}
Evan Chenge96b8d72010-10-26 02:08:50 +00004074
4075bool ARMBaseInstrInfo::
Matthias Braun88e21312015-06-13 03:42:11 +00004076hasLowDefLatency(const TargetSchedModel &SchedModel,
Evan Chenge96b8d72010-10-26 02:08:50 +00004077 const MachineInstr *DefMI, unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004078 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004079 if (!ItinData || ItinData->isEmpty())
4080 return false;
4081
4082 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
4083 if (DDomain == ARMII::DomainGeneral) {
4084 unsigned DefClass = DefMI->getDesc().getSchedClass();
4085 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4086 return (DefCycle != -1 && DefCycle <= 2);
4087 }
4088 return false;
4089}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004090
Andrew Trick924123a2011-09-21 02:20:46 +00004091bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
4092 StringRef &ErrInfo) const {
4093 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
4094 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4095 return false;
4096 }
4097 return true;
4098}
4099
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004100// LoadStackGuard has so far only been implemented for MachO. Different code
4101// sequence is needed for other targets.
4102void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4103 unsigned LoadImmOpc,
4104 unsigned LoadOpc,
4105 Reloc::Model RM) const {
4106 MachineBasicBlock &MBB = *MI->getParent();
4107 DebugLoc DL = MI->getDebugLoc();
4108 unsigned Reg = MI->getOperand(0).getReg();
4109 const GlobalValue *GV =
4110 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4111 MachineInstrBuilder MIB;
4112
4113 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4114 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4115
4116 if (Subtarget.GVIsIndirectSymbol(GV, RM)) {
4117 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4118 MIB.addReg(Reg, RegState::Kill).addImm(0);
4119 unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004120 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
4121 MachinePointerInfo::getGOT(*MBB.getParent()), Flag, 4, 4);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004122 MIB.addMemOperand(MMO);
4123 AddDefaultPred(MIB);
4124 }
4125
4126 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4127 MIB.addReg(Reg, RegState::Kill).addImm(0);
4128 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4129 AddDefaultPred(MIB);
4130}
4131
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004132bool
4133ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4134 unsigned &AddSubOpc,
4135 bool &NegAcc, bool &HasLane) const {
4136 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4137 if (I == MLxEntryMap.end())
4138 return false;
4139
4140 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4141 MulOpc = Entry.MulOpc;
4142 AddSubOpc = Entry.AddSubOpc;
4143 NegAcc = Entry.NegAcc;
4144 HasLane = Entry.HasLane;
4145 return true;
4146}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004147
4148//===----------------------------------------------------------------------===//
4149// Execution domains.
4150//===----------------------------------------------------------------------===//
4151//
4152// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4153// and some can go down both. The vmov instructions go down the VFP pipeline,
4154// but they can be changed to vorr equivalents that are executed by the NEON
4155// pipeline.
4156//
4157// We use the following execution domain numbering:
4158//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004159enum ARMExeDomain {
4160 ExeGeneric = 0,
4161 ExeVFP = 1,
4162 ExeNEON = 2
4163};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004164//
4165// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4166//
4167std::pair<uint16_t, uint16_t>
4168ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004169 // If we don't have access to NEON instructions then we won't be able
4170 // to swizzle anything to the NEON domain. Check to make sure.
4171 if (Subtarget.hasNEON()) {
4172 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4173 // if they are not predicated.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004174 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(*MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004175 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004176
Eric Christopher7e70aba2015-03-07 00:12:22 +00004177 // CortexA9 is particularly picky about mixing the two and wants these
4178 // converted.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004179 if (Subtarget.isCortexA9() && !isPredicated(*MI) &&
Eric Christopher7e70aba2015-03-07 00:12:22 +00004180 (MI->getOpcode() == ARM::VMOVRS || MI->getOpcode() == ARM::VMOVSR ||
4181 MI->getOpcode() == ARM::VMOVS))
4182 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4183 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004184 // No other instructions can be swizzled, so just determine their domain.
4185 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
4186
4187 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004188 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004189
4190 // Certain instructions can go either way on Cortex-A8.
4191 // Treat them as NEON instructions.
4192 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004193 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004194
4195 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004196 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004197
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004198 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004199}
4200
Tim Northover771f1602012-08-29 16:36:07 +00004201static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4202 unsigned SReg, unsigned &Lane) {
4203 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4204 Lane = 0;
4205
4206 if (DReg != ARM::NoRegister)
4207 return DReg;
4208
4209 Lane = 1;
4210 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4211
4212 assert(DReg && "S-register with no D super-register?");
4213 return DReg;
4214}
4215
Andrew Trickd9296ec2012-10-10 05:43:01 +00004216/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004217/// set ImplicitSReg to a register number that must be marked as implicit-use or
4218/// zero if no register needs to be defined as implicit-use.
4219///
4220/// If the function cannot determine if an SPR should be marked implicit use or
4221/// not, it returns false.
4222///
4223/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004224/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004225/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4226/// lane of the DPR).
4227///
4228/// If the other SPR is defined, an implicit-use of it should be added. Else,
4229/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004230///
James Molloyea052562012-09-18 08:31:15 +00004231static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4232 MachineInstr *MI,
4233 unsigned DReg, unsigned Lane,
4234 unsigned &ImplicitSReg) {
4235 // If the DPR is defined or used already, the other SPR lane will be chained
4236 // correctly, so there is nothing to be done.
4237 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4238 ImplicitSReg = 0;
4239 return true;
4240 }
4241
4242 // Otherwise we need to go searching to see if the SPR is set explicitly.
4243 ImplicitSReg = TRI->getSubReg(DReg,
4244 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4245 MachineBasicBlock::LivenessQueryResult LQR =
4246 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4247
4248 if (LQR == MachineBasicBlock::LQR_Live)
4249 return true;
4250 else if (LQR == MachineBasicBlock::LQR_Unknown)
4251 return false;
4252
4253 // If the register is known not to be live, there is no need to add an
4254 // implicit-use.
4255 ImplicitSReg = 0;
4256 return true;
4257}
Tim Northover771f1602012-08-29 16:36:07 +00004258
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004259void
4260ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004261 unsigned DstReg, SrcReg, DReg;
4262 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004263 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004264 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004265 switch (MI->getOpcode()) {
4266 default:
4267 llvm_unreachable("cannot handle opcode!");
4268 break;
4269 case ARM::VMOVD:
4270 if (Domain != ExeNEON)
4271 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004272
Tim Northoverf6618152012-08-17 11:32:52 +00004273 // Zap the predicate operands.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004274 assert(!isPredicated(*MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004275
Eric Christopher7e70aba2015-03-07 00:12:22 +00004276 // Make sure we've got NEON instructions.
4277 assert(Subtarget.hasNEON() && "VORRd requires NEON");
4278
Tim Northover771f1602012-08-29 16:36:07 +00004279 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4280 DstReg = MI->getOperand(0).getReg();
4281 SrcReg = MI->getOperand(1).getReg();
4282
4283 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4284 MI->RemoveOperand(i-1);
4285
4286 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004287 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004288 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4289 .addReg(SrcReg)
4290 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004291 break;
4292 case ARM::VMOVRS:
4293 if (Domain != ExeNEON)
4294 break;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004295 assert(!isPredicated(*MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004296
Tim Northover771f1602012-08-29 16:36:07 +00004297 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004298 DstReg = MI->getOperand(0).getReg();
4299 SrcReg = MI->getOperand(1).getReg();
4300
Tim Northover771f1602012-08-29 16:36:07 +00004301 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4302 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004303
Tim Northover771f1602012-08-29 16:36:07 +00004304 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004305
Tim Northover771f1602012-08-29 16:36:07 +00004306 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4307 // Note that DSrc has been widened and the other lane may be undef, which
4308 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004309 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004310 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4311 .addReg(DReg, RegState::Undef)
4312 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004313
Tim Northover771f1602012-08-29 16:36:07 +00004314 // The old source should be an implicit use, otherwise we might think it
4315 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004316 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004317 break;
James Molloyea052562012-09-18 08:31:15 +00004318 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004319 if (Domain != ExeNEON)
4320 break;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004321 assert(!isPredicated(*MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004322
Tim Northover771f1602012-08-29 16:36:07 +00004323 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004324 DstReg = MI->getOperand(0).getReg();
4325 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004326
Tim Northover771f1602012-08-29 16:36:07 +00004327 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4328
James Molloyea052562012-09-18 08:31:15 +00004329 unsigned ImplicitSReg;
4330 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4331 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004332
Tim Northoverc8d867d2012-09-05 18:37:53 +00004333 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4334 MI->RemoveOperand(i-1);
4335
Tim Northover771f1602012-08-29 16:36:07 +00004336 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4337 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004338 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004339 MIB.addReg(DReg, RegState::Define)
4340 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4341 .addReg(SrcReg)
4342 .addImm(Lane);
4343 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004344
Tim Northover726d32c2012-09-01 18:07:29 +00004345 // The narrower destination must be marked as set to keep previous chains
4346 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004347 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004348 if (ImplicitSReg != 0)
4349 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004350 break;
James Molloyea052562012-09-18 08:31:15 +00004351 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004352 case ARM::VMOVS: {
4353 if (Domain != ExeNEON)
4354 break;
4355
4356 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4357 DstReg = MI->getOperand(0).getReg();
4358 SrcReg = MI->getOperand(1).getReg();
4359
Tim Northoverca9f3842012-08-30 10:17:45 +00004360 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4361 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4362 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4363
James Molloyea052562012-09-18 08:31:15 +00004364 unsigned ImplicitSReg;
4365 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4366 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004367
Tim Northoverc8d867d2012-09-05 18:37:53 +00004368 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4369 MI->RemoveOperand(i-1);
4370
Tim Northoverca9f3842012-08-30 10:17:45 +00004371 if (DSrc == DDst) {
4372 // Destination can be:
4373 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4374 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004375 MIB.addReg(DDst, RegState::Define)
4376 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4377 .addImm(SrcLane);
4378 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004379
4380 // Neither the source or the destination are naturally represented any
4381 // more, so add them in manually.
4382 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4383 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004384 if (ImplicitSReg != 0)
4385 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004386 break;
4387 }
4388
4389 // In general there's no single instruction that can perform an S <-> S
4390 // move in NEON space, but a pair of VEXT instructions *can* do the
4391 // job. It turns out that the VEXTs needed will only use DSrc once, with
4392 // the position based purely on the combination of lane-0 and lane-1
4393 // involved. For example
4394 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4395 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4396 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4397 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4398 //
4399 // Pattern of the MachineInstrs is:
4400 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4401 MachineInstrBuilder NewMIB;
4402 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4403 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004404
4405 // On the first instruction, both DSrc and DDst may be <undef> if present.
4406 // Specifically when the original instruction didn't have them as an
4407 // <imp-use>.
4408 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4409 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4410 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4411
4412 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4413 CurUndef = !MI->readsRegister(CurReg, TRI);
4414 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4415
Tim Northoverca9f3842012-08-30 10:17:45 +00004416 NewMIB.addImm(1);
4417 AddDefaultPred(NewMIB);
4418
4419 if (SrcLane == DstLane)
4420 NewMIB.addReg(SrcReg, RegState::Implicit);
4421
4422 MI->setDesc(get(ARM::VEXTd32));
4423 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004424
4425 // On the second instruction, DDst has definitely been defined above, so
4426 // it is not <undef>. DSrc, if present, can be <undef> as above.
4427 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4428 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4429 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4430
4431 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4432 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4433 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4434
Tim Northoverca9f3842012-08-30 10:17:45 +00004435 MIB.addImm(1);
4436 AddDefaultPred(MIB);
4437
4438 if (SrcLane != DstLane)
4439 MIB.addReg(SrcReg, RegState::Implicit);
4440
4441 // As before, the original destination is no longer represented, add it
4442 // implicitly.
4443 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004444 if (ImplicitSReg != 0)
4445 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004446 break;
4447 }
Tim Northoverf6618152012-08-17 11:32:52 +00004448 }
4449
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004450}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004451
Bob Wilsone8a549c2012-09-29 21:43:49 +00004452//===----------------------------------------------------------------------===//
4453// Partial register updates
4454//===----------------------------------------------------------------------===//
4455//
4456// Swift renames NEON registers with 64-bit granularity. That means any
4457// instruction writing an S-reg implicitly reads the containing D-reg. The
4458// problem is mostly avoided by translating f32 operations to v2f32 operations
4459// on D-registers, but f32 loads are still a problem.
4460//
4461// These instructions can load an f32 into a NEON register:
4462//
4463// VLDRS - Only writes S, partial D update.
4464// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4465// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4466//
4467// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004468unsigned ARMBaseInstrInfo::
4469getPartialRegUpdateClearance(const MachineInstr *MI,
4470 unsigned OpNum,
4471 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004472 if (!SwiftPartialUpdateClearance ||
4473 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004474 return 0;
4475
4476 assert(TRI && "Need TRI instance");
4477
4478 const MachineOperand &MO = MI->getOperand(OpNum);
4479 if (MO.readsReg())
4480 return 0;
4481 unsigned Reg = MO.getReg();
4482 int UseOp = -1;
4483
4484 switch(MI->getOpcode()) {
4485 // Normal instructions writing only an S-register.
4486 case ARM::VLDRS:
4487 case ARM::FCONSTS:
4488 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004489 case ARM::VMOVv8i8:
4490 case ARM::VMOVv4i16:
4491 case ARM::VMOVv2i32:
4492 case ARM::VMOVv2f32:
4493 case ARM::VMOVv1i64:
4494 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4495 break;
4496
4497 // Explicitly reads the dependency.
4498 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004499 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004500 break;
4501 default:
4502 return 0;
4503 }
4504
4505 // If this instruction actually reads a value from Reg, there is no unwanted
4506 // dependency.
4507 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4508 return 0;
4509
4510 // We must be able to clobber the whole D-reg.
4511 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4512 // Virtual register must be a foo:ssub_0<def,undef> operand.
4513 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4514 return 0;
4515 } else if (ARM::SPRRegClass.contains(Reg)) {
4516 // Physical register: MI must define the full D-reg.
4517 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4518 &ARM::DPRRegClass);
4519 if (!DReg || !MI->definesRegister(DReg, TRI))
4520 return 0;
4521 }
4522
4523 // MI has an unwanted D-register dependency.
4524 // Avoid defs in the previous N instructrions.
4525 return SwiftPartialUpdateClearance;
4526}
4527
4528// Break a partial register dependency after getPartialRegUpdateClearance
4529// returned non-zero.
4530void ARMBaseInstrInfo::
4531breakPartialRegDependency(MachineBasicBlock::iterator MI,
4532 unsigned OpNum,
4533 const TargetRegisterInfo *TRI) const {
4534 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4535 assert(TRI && "Need TRI instance");
4536
4537 const MachineOperand &MO = MI->getOperand(OpNum);
4538 unsigned Reg = MO.getReg();
4539 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4540 "Can't break virtual register dependencies.");
4541 unsigned DReg = Reg;
4542
4543 // If MI defines an S-reg, find the corresponding D super-register.
4544 if (ARM::SPRRegClass.contains(Reg)) {
4545 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4546 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4547 }
4548
4549 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4550 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4551
4552 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4553 // the full D-register by loading the same value to both lanes. The
4554 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004555 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004556 // too big regressions.
4557
4558 // Insert the dependency-breaking FCONSTD before MI.
4559 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4560 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4561 get(ARM::FCONSTD), DReg).addImm(96));
4562 MI->addRegisterKilled(DReg, TRI, true);
4563}
4564
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004565bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004566 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004567}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004568
4569bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004570 if (MI->getNumOperands() < 4)
4571 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004572 unsigned ShOpVal = MI->getOperand(3).getImm();
4573 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4574 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4575 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4576 ((ShImm == 1 || ShImm == 2) &&
4577 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4578 return true;
4579
4580 return false;
4581}
Quentin Colombetd358e842014-08-22 18:05:22 +00004582
4583bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4584 const MachineInstr &MI, unsigned DefIdx,
4585 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4586 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4587 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4588
4589 switch (MI.getOpcode()) {
4590 case ARM::VMOVDRR:
4591 // dX = VMOVDRR rY, rZ
4592 // is the same as:
4593 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4594 // Populate the InputRegs accordingly.
4595 // rY
4596 const MachineOperand *MOReg = &MI.getOperand(1);
4597 InputRegs.push_back(
4598 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4599 // rZ
4600 MOReg = &MI.getOperand(2);
4601 InputRegs.push_back(
4602 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4603 return true;
4604 }
4605 llvm_unreachable("Target dependent opcode missing");
4606}
4607
4608bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4609 const MachineInstr &MI, unsigned DefIdx,
4610 RegSubRegPairAndIdx &InputReg) const {
4611 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4612 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4613
4614 switch (MI.getOpcode()) {
4615 case ARM::VMOVRRD:
4616 // rX, rY = VMOVRRD dZ
4617 // is the same as:
4618 // rX = EXTRACT_SUBREG dZ, ssub_0
4619 // rY = EXTRACT_SUBREG dZ, ssub_1
4620 const MachineOperand &MOReg = MI.getOperand(2);
4621 InputReg.Reg = MOReg.getReg();
4622 InputReg.SubReg = MOReg.getSubReg();
4623 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4624 return true;
4625 }
4626 llvm_unreachable("Target dependent opcode missing");
4627}
4628
4629bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4630 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4631 RegSubRegPairAndIdx &InsertedReg) const {
4632 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4633 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4634
4635 switch (MI.getOpcode()) {
4636 case ARM::VSETLNi32:
4637 // dX = VSETLNi32 dY, rZ, imm
4638 const MachineOperand &MOBaseReg = MI.getOperand(1);
4639 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4640 const MachineOperand &MOIndex = MI.getOperand(3);
4641 BaseReg.Reg = MOBaseReg.getReg();
4642 BaseReg.SubReg = MOBaseReg.getSubReg();
4643
4644 InsertedReg.Reg = MOInsertedReg.getReg();
4645 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4646 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4647 return true;
4648 }
4649 llvm_unreachable("Target dependent opcode missing");
4650}