blob: 8c7de5fc7c119acd8ec93e0be835762d0b11d05b [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
Amara Emerson52cfb6a2013-10-03 09:31:51 +000014#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000015#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000016#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000017#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000018#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000019#include "ARMMachineFunctionInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000020#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000022#include "MCTargetDesc/ARMBaseInfo.h"
23#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000025#include "llvm/ADT/SmallSet.h"
26#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000027#include "llvm/ADT/Triple.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000028#include "llvm/CodeGen/LiveVariables.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000030#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstr.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000035#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000036#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000038#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Chenga20cde32011-07-20 23:34:39 +000039#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000040#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000041#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun88e21312015-06-13 03:42:11 +000042#include "llvm/CodeGen/TargetSchedule.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000043#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/Constants.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000045#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Function.h"
47#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000048#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000049#include "llvm/MC/MCInstrDesc.h"
50#include "llvm/MC/MCInstrItineraries.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000051#include "llvm/Support/BranchProbability.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000052#include "llvm/Support/Casting.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000053#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000054#include "llvm/Support/Compiler.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000055#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000056#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000057#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000058#include "llvm/Target/TargetMachine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000059#include <algorithm>
60#include <cassert>
61#include <cstdint>
62#include <iterator>
63#include <new>
64#include <utility>
65#include <vector>
Evan Cheng1e210d02011-06-28 20:07:07 +000066
David Goodwinaf7451b2009-07-08 16:09:28 +000067using namespace llvm;
68
Chandler Carruthe96dd892014-04-21 22:55:11 +000069#define DEBUG_TYPE "arm-instrinfo"
70
Chandler Carruthd174b722014-04-22 02:03:14 +000071#define GET_INSTRINFO_CTOR_DTOR
72#include "ARMGenInstrInfo.inc"
73
David Goodwinaf7451b2009-07-08 16:09:28 +000074static cl::opt<bool>
75EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
76 cl::desc("Enable ARM 2-addr to 3-addr conv"));
77
Evan Cheng62c7b5b2010-12-05 22:04:16 +000078/// ARM_MLxEntry - Record information about MLA / MLS instructions.
79struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000080 uint16_t MLxOpc; // MLA / MLS opcode
81 uint16_t MulOpc; // Expanded multiplication opcode
82 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000083 bool NegAcc; // True if the acc is negated before the add / sub.
84 bool HasLane; // True if instruction has an extra "lane" operand.
85};
86
87static const ARM_MLxEntry ARM_MLxTable[] = {
88 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
89 // fp scalar ops
90 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
91 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
92 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
93 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000094 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
95 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
96 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
97 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
98
99 // fp SIMD ops
100 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
101 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
102 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
103 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
104 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
105 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
106 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
107 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
108};
109
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000110ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +0000111 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000112 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000113 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
114 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000115 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000116 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
117 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
118 }
119}
120
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000121// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
122// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000123ScheduleHazardRecognizer *
124ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
125 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000126 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000127 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000128 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000129 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
130 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000131 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000132}
133
134ScheduleHazardRecognizer *ARMBaseInstrInfo::
135CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
136 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000137 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000138 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000139 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000140}
141
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000142MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
143 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000144 // FIXME: Thumb2 support.
145
David Goodwinaf7451b2009-07-08 16:09:28 +0000146 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000147 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000148
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000149 MachineFunction &MF = *MI.getParent()->getParent();
150 uint64_t TSFlags = MI.getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000151 bool isPre = false;
152 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000154 case ARMII::IndexModePre:
155 isPre = true;
156 break;
157 case ARMII::IndexModePost:
158 break;
159 }
160
161 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
162 // operation.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000163 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
David Goodwinaf7451b2009-07-08 16:09:28 +0000164 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000165 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000166
Craig Topper062a2ba2014-04-25 05:30:21 +0000167 MachineInstr *UpdateMI = nullptr;
168 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000169 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000170 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000171 unsigned NumOps = MCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000172 bool isLoad = !MI.mayStore();
173 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
174 const MachineOperand &Base = MI.getOperand(2);
175 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 unsigned WBReg = WB.getReg();
177 unsigned BaseReg = Base.getReg();
178 unsigned OffReg = Offset.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000179 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
180 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
David Goodwinaf7451b2009-07-08 16:09:28 +0000181 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000182 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000183 case ARMII::AddrMode2: {
184 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
185 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
186 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000187 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000188 // Can't encode it in a so_imm operand. This transformation will
189 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000190 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000191 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000192 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000193 .addReg(BaseReg)
194 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000195 .add(predOps(Pred))
196 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000197 } else if (Amt != 0) {
198 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
199 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000200 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000201 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000202 .addReg(BaseReg)
203 .addReg(OffReg)
204 .addReg(0)
205 .addImm(SOOpc)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000206 .add(predOps(Pred))
207 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 } else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000209 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000210 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000211 .addReg(BaseReg)
212 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000213 .add(predOps(Pred))
214 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000215 break;
216 }
217 case ARMII::AddrMode3 : {
218 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
219 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
220 if (OffReg == 0)
221 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000222 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000223 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000224 .addReg(BaseReg)
225 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000226 .add(predOps(Pred))
227 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000228 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000229 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000230 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000231 .addReg(BaseReg)
232 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000233 .add(predOps(Pred))
234 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000235 break;
236 }
237 }
238
239 std::vector<MachineInstr*> NewMIs;
240 if (isPre) {
241 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000242 MemMI =
243 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
244 .addReg(WBReg)
245 .addImm(0)
246 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000247 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
249 .addReg(MI.getOperand(1).getReg())
250 .addReg(WBReg)
251 .addReg(0)
252 .addImm(0)
253 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000254 NewMIs.push_back(MemMI);
255 NewMIs.push_back(UpdateMI);
256 } else {
257 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000258 MemMI =
259 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
260 .addReg(BaseReg)
261 .addImm(0)
262 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000263 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000264 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
265 .addReg(MI.getOperand(1).getReg())
266 .addReg(BaseReg)
267 .addReg(0)
268 .addImm(0)
269 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000270 if (WB.isDead())
271 UpdateMI->getOperand(0).setIsDead();
272 NewMIs.push_back(UpdateMI);
273 NewMIs.push_back(MemMI);
274 }
275
276 // Transfer LiveVariables states, kill / dead info.
277 if (LV) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000278 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
279 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000280 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000281 unsigned Reg = MO.getReg();
282
283 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
284 if (MO.isDef()) {
285 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
286 if (MO.isDead())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000287 LV->addVirtualRegisterDead(Reg, *NewMI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000288 }
289 if (MO.isUse() && MO.isKill()) {
290 for (unsigned j = 0; j < 2; ++j) {
291 // Look at the two new MI's in reverse order.
292 MachineInstr *NewMI = NewMIs[j];
293 if (!NewMI->readsRegister(Reg))
294 continue;
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000295 LV->addVirtualRegisterKilled(Reg, *NewMI);
296 if (VI.removeKill(MI))
David Goodwinaf7451b2009-07-08 16:09:28 +0000297 VI.Kills.push_back(NewMI);
298 break;
299 }
300 }
301 }
302 }
303 }
304
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000305 MachineBasicBlock::iterator MBBI = MI.getIterator();
David Goodwinaf7451b2009-07-08 16:09:28 +0000306 MFI->insert(MBBI, NewMIs[1]);
307 MFI->insert(MBBI, NewMIs[0]);
308 return NewMIs[0];
309}
310
311// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000312bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
313 MachineBasicBlock *&TBB,
314 MachineBasicBlock *&FBB,
315 SmallVectorImpl<MachineOperand> &Cond,
316 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000317 TBB = nullptr;
318 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000319
David Goodwinaf7451b2009-07-08 16:09:28 +0000320 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000321 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000322 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000323 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000324
325 // Walk backwards from the end of the basic block until the branch is
326 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000327 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000328 // Flag to be raised on unanalyzeable instructions. This is useful in cases
329 // where we want to clean up on the end of the basic block before we bail
330 // out.
331 bool CantAnalyze = false;
332
333 // Skip over DEBUG values and predicated nonterminators.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000334 while (I->isDebugInstr() || !I->isTerminator()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000335 if (I == MBB.begin())
336 return false;
337 --I;
338 }
339
340 if (isIndirectBranchOpcode(I->getOpcode()) ||
341 isJumpTableBranchOpcode(I->getOpcode())) {
342 // Indirect branches and jump tables can't be analyzed, but we still want
343 // to clean up any instructions at the tail of the basic block.
344 CantAnalyze = true;
345 } else if (isUncondBranchOpcode(I->getOpcode())) {
346 TBB = I->getOperand(0).getMBB();
347 } else if (isCondBranchOpcode(I->getOpcode())) {
348 // Bail out if we encounter multiple conditional branches.
349 if (!Cond.empty())
350 return true;
351
352 assert(!FBB && "FBB should have been null.");
353 FBB = TBB;
354 TBB = I->getOperand(0).getMBB();
355 Cond.push_back(I->getOperand(1));
356 Cond.push_back(I->getOperand(2));
357 } else if (I->isReturn()) {
358 // Returns can't be analyzed, but we should run cleanup.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000359 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000360 } else {
361 // We encountered other unrecognized terminator. Bail out immediately.
362 return true;
363 }
364
365 // Cleanup code - to be run for unpredicated unconditional branches and
366 // returns.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000367 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000368 (isUncondBranchOpcode(I->getOpcode()) ||
369 isIndirectBranchOpcode(I->getOpcode()) ||
370 isJumpTableBranchOpcode(I->getOpcode()) ||
371 I->isReturn())) {
372 // Forget any previous condition branch information - it no longer applies.
373 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000374 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000375
376 // If we can modify the function, delete everything below this
377 // unconditional branch.
378 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000379 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000380 while (DI != MBB.end()) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000381 MachineInstr &InstToDelete = *DI;
Lang Hames24864fe2013-07-19 23:52:47 +0000382 ++DI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000383 InstToDelete.eraseFromParent();
Lang Hames24864fe2013-07-19 23:52:47 +0000384 }
385 }
386 }
387
388 if (CantAnalyze)
389 return true;
390
Dale Johannesen4244d122010-04-02 01:38:09 +0000391 if (I == MBB.begin())
392 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000393
Dale Johannesen4244d122010-04-02 01:38:09 +0000394 --I;
395 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000396
Lang Hames24864fe2013-07-19 23:52:47 +0000397 // We made it past the terminators without bailing out - we must have
398 // analyzed this branch successfully.
399 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000400}
401
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000402unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000403 int *BytesRemoved) const {
404 assert(!BytesRemoved && "code size not handled");
405
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000406 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
407 if (I == MBB.end())
408 return 0;
409
Evan Cheng056c6692009-07-27 18:20:05 +0000410 if (!isUncondBranchOpcode(I->getOpcode()) &&
411 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000412 return 0;
413
414 // Remove the branch.
415 I->eraseFromParent();
416
417 I = MBB.end();
418
419 if (I == MBB.begin()) return 1;
420 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000421 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000422 return 1;
423
424 // Remove the branch.
425 I->eraseFromParent();
426 return 2;
427}
428
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000429unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000430 MachineBasicBlock *TBB,
431 MachineBasicBlock *FBB,
432 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000433 const DebugLoc &DL,
434 int *BytesAdded) const {
435 assert(!BytesAdded && "code size not handled");
Evan Cheng780748d2009-07-28 05:48:47 +0000436 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
437 int BOpc = !AFI->isThumbFunction()
438 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
439 int BccOpc = !AFI->isThumbFunction()
440 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000441 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000442
David Goodwinaf7451b2009-07-08 16:09:28 +0000443 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000444 assert(TBB && "insertBranch must not be told to insert a fallthrough");
David Goodwinaf7451b2009-07-08 16:09:28 +0000445 assert((Cond.size() == 2 || Cond.size() == 0) &&
446 "ARM branch conditions have two components!");
447
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000448 // For conditional branches, we use addOperand to preserve CPSR flags.
449
Craig Topper062a2ba2014-04-25 05:30:21 +0000450 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000451 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000452 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000453 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000454 else
455 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000456 } else
Diana Picus116bbab2017-01-13 09:58:52 +0000457 BuildMI(&MBB, DL, get(BccOpc))
458 .addMBB(TBB)
459 .addImm(Cond[0].getImm())
460 .add(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000461 return 1;
462 }
463
464 // Two-way conditional branch.
Diana Picus116bbab2017-01-13 09:58:52 +0000465 BuildMI(&MBB, DL, get(BccOpc))
466 .addMBB(TBB)
467 .addImm(Cond[0].getImm())
468 .add(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000469 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000470 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000471 else
472 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000473 return 2;
474}
475
476bool ARMBaseInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000477reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000478 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
479 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
480 return false;
481}
482
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000483bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
484 if (MI.isBundle()) {
485 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
486 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000487 while (++I != E && I->isInsideBundle()) {
488 int PIdx = I->findFirstPredOperandIdx();
489 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
490 return true;
491 }
492 return false;
493 }
494
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000495 int PIdx = MI.findFirstPredOperandIdx();
496 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000497}
498
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000499bool ARMBaseInstrInfo::PredicateInstruction(
500 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
501 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000502 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000503 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
504 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000505 .addImm(Pred[0].getImm())
506 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000507 return true;
508 }
509
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000510 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000511 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000512 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000513 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000514 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000515 return true;
516 }
517 return false;
518}
519
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000520bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
521 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000522 if (Pred1.size() > 2 || Pred2.size() > 2)
523 return false;
524
525 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
526 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
527 if (CC1 == CC2)
528 return true;
529
530 switch (CC1) {
531 default:
532 return false;
533 case ARMCC::AL:
534 return true;
535 case ARMCC::HS:
536 return CC2 == ARMCC::HI;
537 case ARMCC::LS:
538 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
539 case ARMCC::GE:
540 return CC2 == ARMCC::GT;
541 case ARMCC::LE:
542 return CC2 == ARMCC::LT;
543 }
544}
545
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000546bool ARMBaseInstrInfo::DefinesPredicate(
547 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000548 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000549 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
550 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000551 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
552 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000553 Pred.push_back(MO);
554 Found = true;
555 }
556 }
557
558 return Found;
559}
560
Javed Absar4ae7e8122017-06-02 08:53:19 +0000561bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
562 for (const auto &MO : MI.operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000563 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000564 return true;
565 return false;
566}
567
Javed Absar4ae7e8122017-06-02 08:53:19 +0000568bool ARMBaseInstrInfo::isAddrMode3OpImm(const MachineInstr &MI,
569 unsigned Op) const {
570 const MachineOperand &Offset = MI.getOperand(Op + 1);
571 return Offset.getReg() != 0;
572}
573
574// Load with negative register offset requires additional 1cyc and +I unit
575// for Cortex A57
576bool ARMBaseInstrInfo::isAddrMode3OpMinusReg(const MachineInstr &MI,
577 unsigned Op) const {
578 const MachineOperand &Offset = MI.getOperand(Op + 1);
579 const MachineOperand &Opc = MI.getOperand(Op + 2);
580 assert(Opc.isImm());
581 assert(Offset.isReg());
582 int64_t OpcImm = Opc.getImm();
583
584 bool isSub = ARM_AM::getAM3Op(OpcImm) == ARM_AM::sub;
585 return (isSub && Offset.getReg() != 0);
586}
587
588bool ARMBaseInstrInfo::isLdstScaledReg(const MachineInstr &MI,
589 unsigned Op) const {
590 const MachineOperand &Opc = MI.getOperand(Op + 2);
591 unsigned OffImm = Opc.getImm();
592 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
593}
594
595// Load, scaled register offset, not plus LSL2
596bool ARMBaseInstrInfo::isLdstScaledRegNotPlusLsl2(const MachineInstr &MI,
597 unsigned Op) const {
598 const MachineOperand &Opc = MI.getOperand(Op + 2);
599 unsigned OffImm = Opc.getImm();
600
601 bool isAdd = ARM_AM::getAM2Op(OffImm) == ARM_AM::add;
602 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
603 ARM_AM::ShiftOpc ShiftOpc = ARM_AM::getAM2ShiftOpc(OffImm);
604 if (ShiftOpc == ARM_AM::no_shift) return false; // not scaled
605 bool SimpleScaled = (isAdd && ShiftOpc == ARM_AM::lsl && Amt == 2);
606 return !SimpleScaled;
607}
608
609// Minus reg for ldstso addr mode
610bool ARMBaseInstrInfo::isLdstSoMinusReg(const MachineInstr &MI,
611 unsigned Op) const {
612 unsigned OffImm = MI.getOperand(Op + 2).getImm();
613 return ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
614}
615
616// Load, scaled register offset
617bool ARMBaseInstrInfo::isAm2ScaledReg(const MachineInstr &MI,
618 unsigned Op) const {
619 unsigned OffImm = MI.getOperand(Op + 2).getImm();
620 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
621}
622
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000623static bool isEligibleForITBlock(const MachineInstr *MI) {
624 switch (MI->getOpcode()) {
625 default: return true;
626 case ARM::tADC: // ADC (register) T1
627 case ARM::tADDi3: // ADD (immediate) T1
628 case ARM::tADDi8: // ADD (immediate) T2
629 case ARM::tADDrr: // ADD (register) T1
630 case ARM::tAND: // AND (register) T1
631 case ARM::tASRri: // ASR (immediate) T1
632 case ARM::tASRrr: // ASR (register) T1
633 case ARM::tBIC: // BIC (register) T1
634 case ARM::tEOR: // EOR (register) T1
635 case ARM::tLSLri: // LSL (immediate) T1
636 case ARM::tLSLrr: // LSL (register) T1
637 case ARM::tLSRri: // LSR (immediate) T1
638 case ARM::tLSRrr: // LSR (register) T1
639 case ARM::tMUL: // MUL T1
640 case ARM::tMVN: // MVN (register) T1
641 case ARM::tORR: // ORR (register) T1
642 case ARM::tROR: // ROR (register) T1
643 case ARM::tRSB: // RSB (immediate) T1
644 case ARM::tSBC: // SBC (register) T1
645 case ARM::tSUBi3: // SUB (immediate) T1
646 case ARM::tSUBi8: // SUB (immediate) T2
647 case ARM::tSUBrr: // SUB (register) T1
Javed Absar4ae7e8122017-06-02 08:53:19 +0000648 return !ARMBaseInstrInfo::isCPSRDefined(*MI);
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000649 }
650}
651
Evan Chenga33fc862009-11-21 06:21:52 +0000652/// isPredicable - Return true if the specified instruction can be predicated.
653/// By default, this returns true for every instruction with a
654/// PredicateOperand.
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000655bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000656 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000657 return false;
658
Saleem Abdulrasoolbfa25bd2016-09-06 04:00:12 +0000659 if (MI.isBundle())
660 return false;
661
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000662 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000663 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000664
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000665 const ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000666 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000667
Kristof Beyls96652492017-06-22 12:11:38 +0000668 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
669 // In their ARM encoding, they can't be encoded in a conditional form.
670 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
671 return false;
672
Joey Goulya5153cb2013-09-09 14:21:49 +0000673 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000674 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000675 return isV8EligibleForIT(&MI);
Evan Chenga33fc862009-11-21 06:21:52 +0000676 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000677
Evan Chenga33fc862009-11-21 06:21:52 +0000678 return true;
679}
David Goodwinaf7451b2009-07-08 16:09:28 +0000680
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000681namespace llvm {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000682
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000683template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000684 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
685 const MachineOperand &MO = MI->getOperand(i);
686 if (!MO.isReg() || MO.isUndef() || MO.isUse())
687 continue;
688 if (MO.getReg() != ARM::CPSR)
689 continue;
690 if (!MO.isDead())
691 return false;
692 }
693 // all definitions of CPSR are dead
694 return true;
695}
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000696
697} // end namespace llvm
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000698
David Goodwinaf7451b2009-07-08 16:09:28 +0000699/// GetInstSize - Return the size of the specified MachineInstr.
700///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000701unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000702 const MachineBasicBlock &MBB = *MI.getParent();
David Goodwinaf7451b2009-07-08 16:09:28 +0000703 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000704 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000705
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000706 const MCInstrDesc &MCID = MI.getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000707 if (MCID.getSize())
708 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000709
David Blaikie46a9f012012-01-20 21:51:11 +0000710 // If this machine instr is an inline asm, measure it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000711 if (MI.getOpcode() == ARM::INLINEASM)
712 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
713 unsigned Opc = MI.getOpcode();
David Blaikie46a9f012012-01-20 21:51:11 +0000714 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000715 default:
716 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000717 return 0;
718 case TargetOpcode::BUNDLE:
719 return getInstBundleLength(MI);
720 case ARM::MOVi16_ga_pcrel:
721 case ARM::MOVTi16_ga_pcrel:
722 case ARM::t2MOVi16_ga_pcrel:
723 case ARM::t2MOVTi16_ga_pcrel:
724 return 4;
725 case ARM::MOVi32imm:
726 case ARM::t2MOVi32imm:
727 return 8;
728 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000729 case ARM::JUMPTABLE_INSTS:
730 case ARM::JUMPTABLE_ADDRS:
731 case ARM::JUMPTABLE_TBB:
732 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000733 // If this machine instr is a constant pool entry, its size is recorded as
734 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000735 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000736 case ARM::Int_eh_sjlj_longjmp:
737 return 16;
738 case ARM::tInt_eh_sjlj_longjmp:
739 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000740 case ARM::tInt_WIN_eh_sjlj_longjmp:
741 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000742 case ARM::Int_eh_sjlj_setjmp:
743 case ARM::Int_eh_sjlj_setjmp_nofp:
744 return 20;
745 case ARM::tInt_eh_sjlj_setjmp:
746 case ARM::t2Int_eh_sjlj_setjmp:
747 case ARM::t2Int_eh_sjlj_setjmp_nofp:
748 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000749 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000750 return MI.getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000751 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000752}
753
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000754unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000755 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000756 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
757 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000758 while (++I != E && I->isInsideBundle()) {
759 assert(!I->isBundle() && "No nested bundle!");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000760 Size += getInstSizeInBytes(*I);
Evan Cheng7fae11b2011-12-14 02:11:42 +0000761 }
762 return Size;
763}
764
Tim Northover5d72c5d2014-10-01 19:21:03 +0000765void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
766 MachineBasicBlock::iterator I,
767 unsigned DestReg, bool KillSrc,
768 const ARMSubtarget &Subtarget) const {
769 unsigned Opc = Subtarget.isThumb()
770 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
771 : ARM::MRS;
772
773 MachineInstrBuilder MIB =
774 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
775
776 // There is only 1 A/R class MRS instruction, and it always refers to
777 // APSR. However, there are lots of other possibilities on M-class cores.
778 if (Subtarget.isMClass())
779 MIB.addImm(0x800);
780
Diana Picus4f8c3e12017-01-13 09:37:56 +0000781 MIB.add(predOps(ARMCC::AL))
782 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
Tim Northover5d72c5d2014-10-01 19:21:03 +0000783}
784
785void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
786 MachineBasicBlock::iterator I,
787 unsigned SrcReg, bool KillSrc,
788 const ARMSubtarget &Subtarget) const {
789 unsigned Opc = Subtarget.isThumb()
790 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
791 : ARM::MSR;
792
793 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
794
795 if (Subtarget.isMClass())
796 MIB.addImm(0x800);
797 else
798 MIB.addImm(8);
799
Diana Picus4f8c3e12017-01-13 09:37:56 +0000800 MIB.addReg(SrcReg, getKillRegState(KillSrc))
801 .add(predOps(ARMCC::AL))
802 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
Tim Northover5d72c5d2014-10-01 19:21:03 +0000803}
804
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000805void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000806 MachineBasicBlock::iterator I,
807 const DebugLoc &DL, unsigned DestReg,
808 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000809 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000810 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000811
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000812 if (GPRDest && GPRSrc) {
Diana Picus8a73f552017-01-13 10:18:01 +0000813 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
814 .addReg(SrcReg, getKillRegState(KillSrc))
815 .add(predOps(ARMCC::AL))
816 .add(condCodeOp());
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000817 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000818 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000819
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000820 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000821 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000822
Chad Rosierbe762512011-08-20 00:17:25 +0000823 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000824 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000825 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000826 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000827 Opc = ARM::VMOVRS;
828 else if (SPRDest && GPRSrc)
829 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000830 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000831 Opc = ARM::VMOVD;
832 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000833 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000834
Chad Rosierbe762512011-08-20 00:17:25 +0000835 if (Opc) {
836 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000837 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000838 if (Opc == ARM::VORRq)
839 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Diana Picus4f8c3e12017-01-13 09:37:56 +0000840 MIB.add(predOps(ARMCC::AL));
Chad Rosierbe762512011-08-20 00:17:25 +0000841 return;
842 }
843
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000844 // Handle register classes that require multiple instructions.
845 unsigned BeginIdx = 0;
846 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000847 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000848
849 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000850 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
851 Opc = ARM::VORRq;
852 BeginIdx = ARM::qsub_0;
853 SubRegs = 2;
854 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
855 Opc = ARM::VORRq;
856 BeginIdx = ARM::qsub_0;
857 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000858 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000859 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
860 Opc = ARM::VMOVD;
861 BeginIdx = ARM::dsub_0;
862 SubRegs = 2;
863 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
864 Opc = ARM::VMOVD;
865 BeginIdx = ARM::dsub_0;
866 SubRegs = 3;
867 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
868 Opc = ARM::VMOVD;
869 BeginIdx = ARM::dsub_0;
870 SubRegs = 4;
871 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000872 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000873 BeginIdx = ARM::gsub_0;
874 SubRegs = 2;
875 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
876 Opc = ARM::VMOVD;
877 BeginIdx = ARM::dsub_0;
878 SubRegs = 2;
879 Spacing = 2;
880 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
881 Opc = ARM::VMOVD;
882 BeginIdx = ARM::dsub_0;
883 SubRegs = 3;
884 Spacing = 2;
885 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
886 Opc = ARM::VMOVD;
887 BeginIdx = ARM::dsub_0;
888 SubRegs = 4;
889 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000890 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
891 Opc = ARM::VMOVS;
892 BeginIdx = ARM::ssub_0;
893 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000894 } else if (SrcReg == ARM::CPSR) {
895 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
896 return;
897 } else if (DestReg == ARM::CPSR) {
898 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
899 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000900 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000901
Andrew Trickb57e2252012-08-29 04:41:37 +0000902 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000903
Andrew Trick4cc69492012-08-29 01:58:52 +0000904 const TargetRegisterInfo *TRI = &getRegisterInfo();
905 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000906
907 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
908 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000909 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000910 Spacing = -Spacing;
911 }
912#ifndef NDEBUG
913 SmallSet<unsigned, 4> DstRegs;
914#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000915 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000916 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
917 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000918 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000919#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000920 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000921 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000922#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000923 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000924 // VORR takes two source operands.
925 if (Opc == ARM::VORRq)
926 Mov.addReg(Src);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000927 Mov = Mov.add(predOps(ARMCC::AL));
JF Bastien583db652013-07-12 23:33:03 +0000928 // MOVr can set CC.
929 if (Opc == ARM::MOVr)
Diana Picus8a73f552017-01-13 10:18:01 +0000930 Mov = Mov.add(condCodeOp());
Andrew Trick4cc69492012-08-29 01:58:52 +0000931 }
932 // Add implicit super-register defs and kills to the last instruction.
933 Mov->addRegisterDefined(DestReg, TRI);
934 if (KillSrc)
935 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000936}
937
Petar Jovanovicc0510002018-05-23 15:28:28 +0000938bool ARMBaseInstrInfo::isCopyInstr(const MachineInstr &MI, MachineOperand &Src,
939 MachineOperand &Dest) const {
940 // VMOVRRD is also a copy instruction but it requires
941 // special way of handling. It is more complex copy version
942 // and since that we are not considering it. For recognition
943 // of such instruction isExtractSubregLike MI interface fuction
944 // could be used.
945 // VORRq is considered as a move only if two inputs are
946 // the same register.
947 if (!MI.isMoveReg() ||
948 (MI.getOpcode() == ARM::VORRq &&
949 MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
950 return false;
951 Dest = MI.getOperand(0);
952 Src = MI.getOperand(1);
953 return true;
954}
955
Tim Northover798697d2013-04-21 11:57:07 +0000956const MachineInstrBuilder &
957ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
958 unsigned SubIdx, unsigned State,
959 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000960 if (!SubIdx)
961 return MIB.addReg(Reg, State);
962
963 if (TargetRegisterInfo::isPhysicalRegister(Reg))
964 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
965 return MIB.addReg(Reg, State, SubIdx);
966}
967
David Goodwinaf7451b2009-07-08 16:09:28 +0000968void ARMBaseInstrInfo::
969storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
970 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000971 const TargetRegisterClass *RC,
972 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000973 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000974 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000975 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000976 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000977 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000978
Alex Lorenze40c8a22015-08-11 23:09:45 +0000979 MachineMemOperand *MMO = MF.getMachineMemOperand(
980 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
981 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000982
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000983 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000984 case 2:
985 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
986 BuildMI(MBB, I, DL, get(ARM::VSTRH))
987 .addReg(SrcReg, getKillRegState(isKill))
988 .addFrameIndex(FI)
989 .addImm(0)
990 .addMemOperand(MMO)
991 .add(predOps(ARMCC::AL));
992 } else
993 llvm_unreachable("Unknown reg class!");
994 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000995 case 4:
996 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000997 BuildMI(MBB, I, DL, get(ARM::STRi12))
998 .addReg(SrcReg, getKillRegState(isKill))
999 .addFrameIndex(FI)
1000 .addImm(0)
1001 .addMemOperand(MMO)
1002 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001003 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001004 BuildMI(MBB, I, DL, get(ARM::VSTRS))
1005 .addReg(SrcReg, getKillRegState(isKill))
1006 .addFrameIndex(FI)
1007 .addImm(0)
1008 .addMemOperand(MMO)
1009 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001010 } else
1011 llvm_unreachable("Unknown reg class!");
1012 break;
1013 case 8:
1014 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001015 BuildMI(MBB, I, DL, get(ARM::VSTRD))
1016 .addReg(SrcReg, getKillRegState(isKill))
1017 .addFrameIndex(FI)
1018 .addImm(0)
1019 .addMemOperand(MMO)
1020 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001021 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001022 if (Subtarget.hasV5TEOps()) {
1023 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
1024 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1025 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001026 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1027 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001028 } else {
1029 // Fallback to STM instruction, which has existed since the dawn of
1030 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001031 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STMIA))
1032 .addFrameIndex(FI)
1033 .addMemOperand(MMO)
1034 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001035 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1036 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1037 }
Owen Anderson732f82c2011-08-10 17:21:20 +00001038 } else
1039 llvm_unreachable("Unknown reg class!");
1040 break;
1041 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001042 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001043 // Use aligned spills if the stack can be realigned.
1044 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001045 BuildMI(MBB, I, DL, get(ARM::VST1q64))
1046 .addFrameIndex(FI)
1047 .addImm(16)
1048 .addReg(SrcReg, getKillRegState(isKill))
1049 .addMemOperand(MMO)
1050 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001051 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001052 BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
1053 .addReg(SrcReg, getKillRegState(isKill))
1054 .addFrameIndex(FI)
1055 .addMemOperand(MMO)
1056 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001057 }
1058 } else
1059 llvm_unreachable("Unknown reg class!");
1060 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001061 case 24:
1062 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1063 // Use aligned spills if the stack can be realigned.
1064 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001065 BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
1066 .addFrameIndex(FI)
1067 .addImm(16)
1068 .addReg(SrcReg, getKillRegState(isKill))
1069 .addMemOperand(MMO)
1070 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001071 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001072 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1073 .addFrameIndex(FI)
1074 .add(predOps(ARMCC::AL))
1075 .addMemOperand(MMO);
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001076 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1077 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1078 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1079 }
1080 } else
1081 llvm_unreachable("Unknown reg class!");
1082 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001083 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001084 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001085 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1086 // FIXME: It's possible to only store part of the QQ register if the
1087 // spilled def has a sub-register index.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001088 BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
1089 .addFrameIndex(FI)
1090 .addImm(16)
1091 .addReg(SrcReg, getKillRegState(isKill))
1092 .addMemOperand(MMO)
1093 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001094 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001095 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1096 .addFrameIndex(FI)
1097 .add(predOps(ARMCC::AL))
1098 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001099 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1100 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1101 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1102 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1103 }
1104 } else
1105 llvm_unreachable("Unknown reg class!");
1106 break;
1107 case 64:
1108 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001109 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1110 .addFrameIndex(FI)
1111 .add(predOps(ARMCC::AL))
1112 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001113 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1114 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1115 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1116 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1117 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1118 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1119 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1120 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1121 } else
1122 llvm_unreachable("Unknown reg class!");
1123 break;
1124 default:
1125 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001126 }
1127}
1128
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001129unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1130 int &FrameIndex) const {
1131 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001132 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001133 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001134 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001135 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1136 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1137 MI.getOperand(3).getImm() == 0) {
1138 FrameIndex = MI.getOperand(1).getIndex();
1139 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001140 }
1141 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001142 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001143 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001144 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001145 case ARM::VSTRD:
1146 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001147 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1148 MI.getOperand(2).getImm() == 0) {
1149 FrameIndex = MI.getOperand(1).getIndex();
1150 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001151 }
1152 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001153 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001154 case ARM::VST1d64TPseudo:
1155 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1157 FrameIndex = MI.getOperand(0).getIndex();
1158 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001159 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001160 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001161 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001162 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1163 FrameIndex = MI.getOperand(1).getIndex();
1164 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001165 }
1166 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001167 }
1168
1169 return 0;
1170}
1171
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001172unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001173 int &FrameIndex) const {
1174 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001175 return MI.mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001176}
1177
David Goodwinaf7451b2009-07-08 16:09:28 +00001178void ARMBaseInstrInfo::
1179loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1180 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001181 const TargetRegisterClass *RC,
1182 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001183 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001184 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001185 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001186 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001187 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001188 MachineMemOperand *MMO = MF.getMachineMemOperand(
1189 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1190 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001191
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001192 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +00001193 case 2:
1194 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
1195 BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
1196 .addFrameIndex(FI)
1197 .addImm(0)
1198 .addMemOperand(MMO)
1199 .add(predOps(ARMCC::AL));
1200 } else
1201 llvm_unreachable("Unknown reg class!");
1202 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001203 case 4:
1204 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001205 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1206 .addFrameIndex(FI)
1207 .addImm(0)
1208 .addMemOperand(MMO)
1209 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001210 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001211 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1212 .addFrameIndex(FI)
1213 .addImm(0)
1214 .addMemOperand(MMO)
1215 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001216 } else
1217 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001218 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001219 case 8:
1220 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001221 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1222 .addFrameIndex(FI)
1223 .addImm(0)
1224 .addMemOperand(MMO)
1225 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001226 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001227 MachineInstrBuilder MIB;
1228
1229 if (Subtarget.hasV5TEOps()) {
1230 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1231 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1232 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001233 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1234 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001235 } else {
1236 // Fallback to LDM instruction, which has existed since the dawn of
1237 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001238 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1239 .addFrameIndex(FI)
1240 .addMemOperand(MMO)
1241 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001242 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1243 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1244 }
1245
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001246 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1247 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001248 } else
1249 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001250 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001251 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001252 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001253 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001254 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1255 .addFrameIndex(FI)
1256 .addImm(16)
1257 .addMemOperand(MMO)
1258 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001259 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001260 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1261 .addFrameIndex(FI)
1262 .addMemOperand(MMO)
1263 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001264 }
1265 } else
1266 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001267 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001268 case 24:
1269 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1270 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001271 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1272 .addFrameIndex(FI)
1273 .addImm(16)
1274 .addMemOperand(MMO)
1275 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001276 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001277 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1278 .addFrameIndex(FI)
1279 .addMemOperand(MMO)
1280 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001281 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1282 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1283 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1284 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1285 MIB.addReg(DestReg, RegState::ImplicitDefine);
1286 }
1287 } else
1288 llvm_unreachable("Unknown reg class!");
1289 break;
1290 case 32:
1291 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001292 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001293 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1294 .addFrameIndex(FI)
1295 .addImm(16)
1296 .addMemOperand(MMO)
1297 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001298 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001299 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1300 .addFrameIndex(FI)
1301 .add(predOps(ARMCC::AL))
1302 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001303 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1304 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1305 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1306 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001307 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1308 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001309 }
1310 } else
1311 llvm_unreachable("Unknown reg class!");
1312 break;
1313 case 64:
1314 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001315 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1316 .addFrameIndex(FI)
1317 .add(predOps(ARMCC::AL))
1318 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001319 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1320 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1321 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1322 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1323 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1324 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1325 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1326 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001327 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1328 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001329 } else
1330 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001331 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001332 default:
1333 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001334 }
1335}
1336
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001337unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1338 int &FrameIndex) const {
1339 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001340 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001341 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001342 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001343 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1344 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1345 MI.getOperand(3).getImm() == 0) {
1346 FrameIndex = MI.getOperand(1).getIndex();
1347 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001348 }
1349 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001350 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001351 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001352 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001353 case ARM::VLDRD:
1354 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001355 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1356 MI.getOperand(2).getImm() == 0) {
1357 FrameIndex = MI.getOperand(1).getIndex();
1358 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001359 }
1360 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001361 case ARM::VLD1q64:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001362 case ARM::VLD1d8TPseudo:
1363 case ARM::VLD1d16TPseudo:
1364 case ARM::VLD1d32TPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001365 case ARM::VLD1d64TPseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001366 case ARM::VLD1d8QPseudo:
1367 case ARM::VLD1d16QPseudo:
1368 case ARM::VLD1d32QPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001369 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001370 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1371 FrameIndex = MI.getOperand(1).getIndex();
1372 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001373 }
1374 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001375 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001376 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1377 FrameIndex = MI.getOperand(1).getIndex();
1378 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001379 }
1380 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001381 }
1382
1383 return 0;
1384}
1385
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001386unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1387 int &FrameIndex) const {
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001388 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001389 return MI.mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001390}
1391
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001392/// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
Scott Douglass953f9082015-10-05 14:49:54 +00001393/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001394void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001395 bool isThumb1 = Subtarget.isThumb1Only();
1396 bool isThumb2 = Subtarget.isThumb2();
1397 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1398
Scott Douglass953f9082015-10-05 14:49:54 +00001399 DebugLoc dl = MI->getDebugLoc();
1400 MachineBasicBlock *BB = MI->getParent();
1401
1402 MachineInstrBuilder LDM, STM;
1403 if (isThumb1 || !MI->getOperand(1).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001404 MachineOperand LDWb(MI->getOperand(1));
Scott Douglass953f9082015-10-05 14:49:54 +00001405 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1406 : isThumb1 ? ARM::tLDMIA_UPD
1407 : ARM::LDMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001408 .add(LDWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001409 } else {
1410 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1411 }
1412
1413 if (isThumb1 || !MI->getOperand(0).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001414 MachineOperand STWb(MI->getOperand(0));
Scott Douglass953f9082015-10-05 14:49:54 +00001415 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1416 : isThumb1 ? ARM::tSTMIA_UPD
1417 : ARM::STMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001418 .add(STWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001419 } else {
1420 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1421 }
1422
Geoff Berry60c43102017-12-12 17:53:59 +00001423 MachineOperand LDBase(MI->getOperand(3));
Geoff Berry60c43102017-12-12 17:53:59 +00001424 LDM.add(LDBase).add(predOps(ARMCC::AL));
1425
1426 MachineOperand STBase(MI->getOperand(2));
Geoff Berry60c43102017-12-12 17:53:59 +00001427 STM.add(STBase).add(predOps(ARMCC::AL));
Scott Douglass953f9082015-10-05 14:49:54 +00001428
1429 // Sort the scratch registers into ascending order.
1430 const TargetRegisterInfo &TRI = getRegisterInfo();
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001431 SmallVector<unsigned, 6> ScratchRegs;
Scott Douglass953f9082015-10-05 14:49:54 +00001432 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1433 ScratchRegs.push_back(MI->getOperand(I).getReg());
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001434 llvm::sort(ScratchRegs.begin(), ScratchRegs.end(),
1435 [&TRI](const unsigned &Reg1,
1436 const unsigned &Reg2) -> bool {
1437 return TRI.getEncodingValue(Reg1) <
1438 TRI.getEncodingValue(Reg2);
1439 });
Scott Douglass953f9082015-10-05 14:49:54 +00001440
1441 for (const auto &Reg : ScratchRegs) {
1442 LDM.addReg(Reg, RegState::Define);
1443 STM.addReg(Reg, RegState::Kill);
1444 }
1445
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001446 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001447}
1448
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001449bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1450 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001451 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001452 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001453 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001454 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001455 return true;
1456 }
1457
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001458 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001459 expandMEMCPY(MI);
1460 return true;
1461 }
1462
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001463 // This hook gets to expand COPY instructions before they become
1464 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1465 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1466 // changed into a VORR that can go down the NEON pipeline.
Diana Picusb772e402016-07-06 11:22:11 +00001467 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001468 return false;
1469
1470 // Look for a copy between even S-registers. That is where we keep floats
1471 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001472 unsigned DstRegS = MI.getOperand(0).getReg();
1473 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001474 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1475 return false;
1476
1477 const TargetRegisterInfo *TRI = &getRegisterInfo();
1478 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1479 &ARM::DPRRegClass);
1480 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1481 &ARM::DPRRegClass);
1482 if (!DstRegD || !SrcRegD)
1483 return false;
1484
1485 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1486 // legal if the COPY already defines the full DstRegD, and it isn't a
1487 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001488 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001489 return false;
1490
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001491 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001492 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001493 return false;
1494
1495 // All clear, widen the COPY.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001496 LLVM_DEBUG(dbgs() << "widening: " << MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001497 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001498
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001499 // Get rid of the old implicit-def of DstRegD. Leave it if it defines a Q-reg
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001500 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001501 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001502 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001503 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001504
1505 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001506 MI.setDesc(get(ARM::VMOVD));
1507 MI.getOperand(0).setReg(DstRegD);
1508 MI.getOperand(1).setReg(SrcRegD);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001509 MIB.add(predOps(ARMCC::AL));
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001510
1511 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1512 // register scavenger and machine verifier, so we need to indicate that we
1513 // are reading an undefined value from SrcRegD, but a proper value from
1514 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001515 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001516 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001517
1518 // SrcRegD may actually contain an unrelated value in the ssub_1
1519 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001520 if (MI.getOperand(1).isKill()) {
1521 MI.getOperand(1).setIsKill(false);
1522 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001523 }
1524
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001525 LLVM_DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001526 return true;
1527}
1528
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001529/// Create a copy of a const pool value. Update CPI to the new index and return
1530/// the label UID.
1531static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1532 MachineConstantPool *MCP = MF.getConstantPool();
1533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1534
1535 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1536 assert(MCPE.isMachineConstantPoolEntry() &&
1537 "Expecting a machine constantpool entry!");
1538 ARMConstantPoolValue *ACPV =
1539 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1540
Evan Chengdfce83c2011-01-17 08:03:18 +00001541 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001542 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001543
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001544 // FIXME: The below assumes PIC relocation model and that the function
1545 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1546 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1547 // instructions, so that's probably OK, but is PIC always correct when
1548 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001549 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001550 NewCPV = ARMConstantPoolConstant::Create(
1551 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1552 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001553 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001554 NewCPV = ARMConstantPoolSymbol::
Matthias Braunf1caa282017-12-15 22:22:58 +00001555 Create(MF.getFunction().getContext(),
Bill Wendlingc214cb02011-10-01 08:58:29 +00001556 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001557 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001558 NewCPV = ARMConstantPoolConstant::
1559 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1560 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001561 else if (ACPV->isLSDA())
Matthias Braunf1caa282017-12-15 22:22:58 +00001562 NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
Bill Wendling7753d662011-10-01 08:00:54 +00001563 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001564 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001565 NewCPV = ARMConstantPoolMBB::
Matthias Braunf1caa282017-12-15 22:22:58 +00001566 Create(MF.getFunction().getContext(),
Bill Wendling4a4772f2011-10-01 09:30:42 +00001567 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001568 else
1569 llvm_unreachable("Unexpected ARM constantpool value type!!");
1570 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1571 return PCLabelId;
1572}
1573
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001574void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1575 MachineBasicBlock::iterator I,
1576 unsigned DestReg, unsigned SubIdx,
1577 const MachineInstr &Orig,
1578 const TargetRegisterInfo &TRI) const {
1579 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001580 switch (Opcode) {
1581 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001582 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1583 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001584 MBB.insert(I, MI);
1585 break;
1586 }
1587 case ARM::tLDRpci_pic:
1588 case ARM::t2LDRpci_pic: {
1589 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001590 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001591 unsigned PCLabelId = duplicateCPV(MF, CPI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001592 MachineInstrBuilder MIB =
1593 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1594 .addConstantPoolIndex(CPI)
1595 .addImm(PCLabelId);
1596 MIB->setMemRefs(Orig.memoperands_begin(), Orig.memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001597 break;
1598 }
1599 }
Evan Chengfe864422009-11-08 00:15:23 +00001600}
1601
Matthias Braun55bc9b32017-08-22 23:56:30 +00001602MachineInstr &
1603ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1604 MachineBasicBlock::iterator InsertBefore,
1605 const MachineInstr &Orig) const {
1606 MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1607 MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1608 for (;;) {
1609 switch (I->getOpcode()) {
1610 case ARM::tLDRpci_pic:
1611 case ARM::t2LDRpci_pic: {
1612 MachineFunction &MF = *MBB.getParent();
1613 unsigned CPI = I->getOperand(1).getIndex();
1614 unsigned PCLabelId = duplicateCPV(MF, CPI);
1615 I->getOperand(1).setIndex(CPI);
1616 I->getOperand(2).setImm(PCLabelId);
1617 break;
1618 }
1619 }
1620 if (!I->isBundledWithSucc())
1621 break;
1622 ++I;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001623 }
Matthias Braun55bc9b32017-08-22 23:56:30 +00001624 return Cloned;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001625}
1626
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001627bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1628 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001629 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001630 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001631 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001632 Opcode == ARM::t2LDRpci_pic ||
1633 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001634 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001635 Opcode == ARM::LDRLIT_ga_pcrel ||
1636 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1637 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001638 Opcode == ARM::MOV_ga_pcrel ||
1639 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001640 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001641 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001642 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001643 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001644 return false;
1645
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001646 const MachineOperand &MO0 = MI0.getOperand(1);
1647 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001648 if (MO0.getOffset() != MO1.getOffset())
1649 return false;
1650
Tim Northover72360d22013-12-02 10:35:41 +00001651 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1652 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1653 Opcode == ARM::tLDRLIT_ga_pcrel ||
1654 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001655 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001656 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001657 // Ignore the PC labels.
1658 return MO0.getGlobal() == MO1.getGlobal();
1659
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001660 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001661 const MachineConstantPool *MCP = MF->getConstantPool();
1662 int CPI0 = MO0.getIndex();
1663 int CPI1 = MO1.getIndex();
1664 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1665 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001666 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1667 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1668 if (isARMCP0 && isARMCP1) {
1669 ARMConstantPoolValue *ACPV0 =
1670 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1671 ARMConstantPoolValue *ACPV1 =
1672 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1673 return ACPV0->hasSameValue(ACPV1);
1674 } else if (!isARMCP0 && !isARMCP1) {
1675 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1676 }
1677 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001678 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001679 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001680 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001681 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001682 return false;
1683
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001684 unsigned Addr0 = MI0.getOperand(1).getReg();
1685 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001686 if (Addr0 != Addr1) {
1687 if (!MRI ||
1688 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1689 !TargetRegisterInfo::isVirtualRegister(Addr1))
1690 return false;
1691
1692 // This assumes SSA form.
1693 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1694 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1695 // Check if the loaded value, e.g. a constantpool of a global address, are
1696 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001697 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001698 return false;
1699 }
1700
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001701 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00001702 // %12 = PICLDR %11, 0, 14, %noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001703 const MachineOperand &MO0 = MI0.getOperand(i);
1704 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001705 if (!MO0.isIdenticalTo(MO1))
1706 return false;
1707 }
1708 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001709 }
1710
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001711 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001712}
1713
Bill Wendlingf4707472010-06-23 23:00:16 +00001714/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1715/// determine if two loads are loading from the same base address. It should
1716/// only return true if the base pointers are the same and the only differences
1717/// between the two addresses is the offset. It also returns the offsets by
1718/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001719///
1720/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1721/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001722bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1723 int64_t &Offset1,
1724 int64_t &Offset2) const {
1725 // Don't worry about Thumb: just ARM and Thumb2.
1726 if (Subtarget.isThumb1Only()) return false;
1727
1728 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1729 return false;
1730
1731 switch (Load1->getMachineOpcode()) {
1732 default:
1733 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001734 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001735 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001736 case ARM::LDRD:
1737 case ARM::LDRH:
1738 case ARM::LDRSB:
1739 case ARM::LDRSH:
1740 case ARM::VLDRD:
1741 case ARM::VLDRS:
1742 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001743 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001744 case ARM::t2LDRDi8:
1745 case ARM::t2LDRSHi8:
1746 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001747 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001748 case ARM::t2LDRSHi12:
1749 break;
1750 }
1751
1752 switch (Load2->getMachineOpcode()) {
1753 default:
1754 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001755 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001756 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001757 case ARM::LDRD:
1758 case ARM::LDRH:
1759 case ARM::LDRSB:
1760 case ARM::LDRSH:
1761 case ARM::VLDRD:
1762 case ARM::VLDRS:
1763 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001764 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001765 case ARM::t2LDRSHi8:
1766 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001767 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001768 case ARM::t2LDRSHi12:
1769 break;
1770 }
1771
1772 // Check if base addresses and chain operands match.
1773 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1774 Load1->getOperand(4) != Load2->getOperand(4))
1775 return false;
1776
1777 // Index should be Reg0.
1778 if (Load1->getOperand(3) != Load2->getOperand(3))
1779 return false;
1780
1781 // Determine the offsets.
1782 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1783 isa<ConstantSDNode>(Load2->getOperand(1))) {
1784 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1785 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1786 return true;
1787 }
1788
1789 return false;
1790}
1791
1792/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001793/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001794/// be scheduled togther. On some targets if two loads are loading from
1795/// addresses in the same cache line, it's better if they are scheduled
1796/// together. This function takes two integers that represent the load offsets
1797/// from the common base address. It returns true if it decides it's desirable
1798/// to schedule the two loads together. "NumLoads" is the number of loads that
1799/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001800///
1801/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1802/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001803bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1804 int64_t Offset1, int64_t Offset2,
1805 unsigned NumLoads) const {
1806 // Don't worry about Thumb: just ARM and Thumb2.
1807 if (Subtarget.isThumb1Only()) return false;
1808
1809 assert(Offset2 > Offset1);
1810
1811 if ((Offset2 - Offset1) / 8 > 64)
1812 return false;
1813
Renato Golinb184cd92013-08-14 16:35:29 +00001814 // Check if the machine opcodes are different. If they are different
1815 // then we consider them to not be of the same base address,
1816 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1817 // In this case, they are considered to be the same because they are different
1818 // encoding forms of the same basic instruction.
1819 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1820 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1821 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1822 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1823 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001824 return false; // FIXME: overly conservative?
1825
1826 // Four loads in a row should be sufficient.
1827 if (NumLoads >= 3)
1828 return false;
1829
1830 return true;
1831}
1832
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001833bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001834 const MachineBasicBlock *MBB,
1835 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001836 // Debug info is never a scheduling boundary. It's necessary to be explicit
1837 // due to the special treatment of IT instructions below, otherwise a
1838 // dbg_value followed by an IT will result in the IT instruction being
1839 // considered a scheduling hazard, which is wrong. It should be the actual
1840 // instruction preceding the dbg_value instruction(s), just like it is
1841 // when debug info is not present.
Shiva Chen801bf7e2018-05-09 02:42:00 +00001842 if (MI.isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001843 return false;
1844
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001845 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001846 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001847 return true;
1848
1849 // Treat the start of the IT block as a scheduling boundary, but schedule
1850 // t2IT along with all instructions following it.
1851 // FIXME: This is a big hammer. But the alternative is to add all potential
1852 // true and anti dependencies to IT block instructions as implicit operands
1853 // to the t2IT instruction. The added compile time and complexity does not
1854 // seem worth it.
1855 MachineBasicBlock::const_iterator I = MI;
Shiva Chen801bf7e2018-05-09 02:42:00 +00001856 // Make sure to skip any debug instructions
1857 while (++I != MBB->end() && I->isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001858 ;
1859 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001860 return true;
1861
1862 // Don't attempt to schedule around any instruction that defines
1863 // a stack-oriented pointer, as it's unlikely to be profitable. This
1864 // saves compile time, because it doesn't require every single
1865 // stack slot reference to depend on the instruction that does the
1866 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001867 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001868 // No ARM calling conventions change the stack pointer. (X86 calling
1869 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001870 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001871 return true;
1872
1873 return false;
1874}
1875
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001876bool ARMBaseInstrInfo::
1877isProfitableToIfCvt(MachineBasicBlock &MBB,
1878 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001879 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001880 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001881 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001882
Peter Collingbourne65295232015-04-23 20:31:30 +00001883 // If we are optimizing for size, see if the branch in the predecessor can be
1884 // lowered to cbn?z by the constant island lowering pass, and return false if
1885 // so. This results in a shorter instruction sequence.
Matthias Braunf1caa282017-12-15 22:22:58 +00001886 if (MBB.getParent()->getFunction().optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001887 MachineBasicBlock *Pred = *MBB.pred_begin();
1888 if (!Pred->empty()) {
1889 MachineInstr *LastMI = &*Pred->rbegin();
1890 if (LastMI->getOpcode() == ARM::t2Bcc) {
1891 MachineBasicBlock::iterator CmpMI = LastMI;
1892 if (CmpMI != Pred->begin()) {
1893 --CmpMI;
1894 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1895 CmpMI->getOpcode() == ARM::t2CMPri) {
1896 unsigned Reg = CmpMI->getOperand(0).getReg();
1897 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001898 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
Peter Collingbourne65295232015-04-23 20:31:30 +00001899 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1900 isARMLowRegister(Reg))
1901 return false;
1902 }
1903 }
1904 }
1905 }
1906 }
Artyom Skrobov283316b2017-03-14 13:38:45 +00001907 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1908 MBB, 0, 0, Probability);
Evan Cheng02b184d2010-06-25 22:42:03 +00001909}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001910
Evan Cheng02b184d2010-06-25 22:42:03 +00001911bool ARMBaseInstrInfo::
John Brawn75d76e52017-06-28 14:11:15 +00001912isProfitableToIfCvt(MachineBasicBlock &TBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001913 unsigned TCycles, unsigned TExtra,
John Brawn75d76e52017-06-28 14:11:15 +00001914 MachineBasicBlock &FBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001915 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001916 BranchProbability Probability) const {
Artyom Skrobov283316b2017-03-14 13:38:45 +00001917 if (!TCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001918 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001919
Owen Anderson88af7d02010-09-28 18:32:13 +00001920 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001921 // Here we scale up each component of UnpredCost to avoid precision issue when
1922 // scaling TCycles/FCycles by Probability.
1923 const unsigned ScalingUpFactor = 1024;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001924
John Brawn75d76e52017-06-28 14:11:15 +00001925 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1926 unsigned UnpredCost;
1927 if (!Subtarget.hasBranchPredictor()) {
1928 // When we don't have a branch predictor it's always cheaper to not take a
1929 // branch than take it, so we have to take that into account.
1930 unsigned NotTakenBranchCost = 1;
1931 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1932 unsigned TUnpredCycles, FUnpredCycles;
1933 if (!FCycles) {
1934 // Triangle: TBB is the fallthrough
1935 TUnpredCycles = TCycles + NotTakenBranchCost;
1936 FUnpredCycles = TakenBranchCost;
1937 } else {
1938 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1939 TUnpredCycles = TCycles + TakenBranchCost;
1940 FUnpredCycles = FCycles + NotTakenBranchCost;
John Brawn97cc2832017-07-12 13:23:10 +00001941 // The branch at the end of FBB will disappear when it's predicated, so
1942 // discount it from PredCost.
1943 PredCost -= 1 * ScalingUpFactor;
John Brawn75d76e52017-06-28 14:11:15 +00001944 }
1945 // The total cost is the cost of each path scaled by their probabilites
1946 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1947 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1948 UnpredCost = TUnpredCost + FUnpredCost;
1949 // When predicating assume that the first IT can be folded away but later
1950 // ones cost one cycle each
1951 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1952 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1953 }
1954 } else {
1955 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1956 unsigned FUnpredCost =
1957 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1958 UnpredCost = TUnpredCost + FUnpredCost;
1959 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1960 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1961 }
1962
1963 return PredCost <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001964}
1965
Bob Wilsone8a549c2012-09-29 21:43:49 +00001966bool
1967ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1968 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001969 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001970 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001971 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001972}
1973
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001974/// getInstrPredicate - If instruction is predicated, returns its predicate
1975/// condition, otherwise returns AL. It also returns the condition code
1976/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001977ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1978 unsigned &PredReg) {
1979 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001980 if (PIdx == -1) {
1981 PredReg = 0;
1982 return ARMCC::AL;
1983 }
1984
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001985 PredReg = MI.getOperand(PIdx+1).getReg();
1986 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001987}
1988
Matthias Braunfa3872e2015-05-18 20:27:55 +00001989unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001990 if (Opc == ARM::B)
1991 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001992 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001993 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001994 if (Opc == ARM::t2B)
1995 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001996
1997 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001998}
1999
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002000MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002001 bool NewMI,
2002 unsigned OpIdx1,
2003 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002004 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002005 case ARM::MOVCCr:
2006 case ARM::t2MOVCCr: {
2007 // MOVCC can be commuted by inverting the condition.
2008 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002009 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002010 // MOVCC AL can't be inverted. Shouldn't happen.
2011 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00002012 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002013 MachineInstr *CommutedMI =
2014 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2015 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002016 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002017 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002018 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
2019 .setImm(ARMCC::getOppositeCondition(CC));
2020 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002021 }
2022 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002023 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002024}
Evan Cheng780748d2009-07-28 05:48:47 +00002025
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002026/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002027/// return the defining instruction.
2028static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
2029 const MachineRegisterInfo &MRI,
2030 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002031 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002032 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002033 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002034 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002035 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002036 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002037 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002038 // MI is folded into the MOVCC by predicating it.
2039 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00002040 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002041 // Check if MI has any non-dead defs or physreg uses. This also detects
2042 // predicated instructions which will be reading CPSR.
2043 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
2044 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00002045 // Reject frame index operands, PEI can't handle the predicated pseudos.
2046 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00002047 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002048 if (!MO.isReg())
2049 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002050 // MI can't have any tied operands, that would conflict with predication.
2051 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00002052 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002053 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00002054 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002055 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00002056 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002057 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002058 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00002059 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00002060 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002061 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002062}
2063
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002064bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002065 SmallVectorImpl<MachineOperand> &Cond,
2066 unsigned &TrueOp, unsigned &FalseOp,
2067 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002068 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002069 "Unknown select instruction");
2070 // MOVCC operands:
2071 // 0: Def.
2072 // 1: True use.
2073 // 2: False use.
2074 // 3: Condition code.
2075 // 4: CPSR use.
2076 TrueOp = 1;
2077 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002078 Cond.push_back(MI.getOperand(3));
2079 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002080 // We can always fold a def.
2081 Optimizable = true;
2082 return false;
2083}
2084
Mehdi Amini22e59742015-01-13 07:07:13 +00002085MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002086ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00002087 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2088 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002089 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002090 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002091 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2092 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002093 bool Invert = !DefMI;
2094 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002095 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002096 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002097 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002098
Matthias Braun2f169f92013-10-04 16:52:56 +00002099 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002100 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2101 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00002102 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2103 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00002104 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00002105
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002106 // Create a new predicated version of DefMI.
2107 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002108 MachineInstrBuilder NewMI =
2109 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002110
2111 // Copy all the DefMI operands, excluding its (null) predicate.
2112 const MCInstrDesc &DefDesc = DefMI->getDesc();
2113 for (unsigned i = 1, e = DefDesc.getNumOperands();
2114 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00002115 NewMI.add(DefMI->getOperand(i));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002116
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002117 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002118 if (Invert)
2119 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2120 else
2121 NewMI.addImm(CondCode);
Diana Picus116bbab2017-01-13 09:58:52 +00002122 NewMI.add(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002123
2124 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2125 if (NewMI->hasOptionalDef())
Diana Picus8a73f552017-01-13 10:18:01 +00002126 NewMI.add(condCodeOp());
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002127
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002128 // The output register value when the predicate is false is an implicit
2129 // register operand tied to the first def.
2130 // The tie makes the register allocator ensure the FalseReg is allocated the
2131 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002132 FalseReg.setImplicit();
Diana Picus116bbab2017-01-13 09:58:52 +00002133 NewMI.add(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002134 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2135
Mehdi Amini22e59742015-01-13 07:07:13 +00002136 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2137 SeenMIs.insert(NewMI);
2138 SeenMIs.erase(DefMI);
2139
Pete Cooper2127b002015-04-30 23:57:47 +00002140 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2141 // DefMI would be invalid when tranferred inside the loop. Checking for a
2142 // loop is expensive, but at least remove kill flags if they are in different
2143 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002144 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00002145 NewMI->clearKillInfo();
2146
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002147 // The caller will erase MI, but not DefMI.
2148 DefMI->eraseFromParent();
2149 return NewMI;
2150}
2151
Andrew Trick924123a2011-09-21 02:20:46 +00002152/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2153/// instruction is encoded with an 'S' bit is determined by the optional CPSR
2154/// def operand.
2155///
2156/// This will go away once we can teach tblgen how to set the optional CPSR def
2157/// operand itself.
2158struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00002159 uint16_t PseudoOpc;
2160 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002161};
2162
Craig Topper2fbd1302012-05-24 03:59:11 +00002163static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00002164 {ARM::ADDSri, ARM::ADDri},
2165 {ARM::ADDSrr, ARM::ADDrr},
2166 {ARM::ADDSrsi, ARM::ADDrsi},
2167 {ARM::ADDSrsr, ARM::ADDrsr},
2168
2169 {ARM::SUBSri, ARM::SUBri},
2170 {ARM::SUBSrr, ARM::SUBrr},
2171 {ARM::SUBSrsi, ARM::SUBrsi},
2172 {ARM::SUBSrsr, ARM::SUBrsr},
2173
2174 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00002175 {ARM::RSBSrsi, ARM::RSBrsi},
2176 {ARM::RSBSrsr, ARM::RSBrsr},
2177
Artyom Skrobov92c06532017-03-22 23:35:51 +00002178 {ARM::tADDSi3, ARM::tADDi3},
2179 {ARM::tADDSi8, ARM::tADDi8},
2180 {ARM::tADDSrr, ARM::tADDrr},
2181 {ARM::tADCS, ARM::tADC},
2182
2183 {ARM::tSUBSi3, ARM::tSUBi3},
2184 {ARM::tSUBSi8, ARM::tSUBi8},
2185 {ARM::tSUBSrr, ARM::tSUBrr},
2186 {ARM::tSBCS, ARM::tSBC},
2187
Andrew Trick924123a2011-09-21 02:20:46 +00002188 {ARM::t2ADDSri, ARM::t2ADDri},
2189 {ARM::t2ADDSrr, ARM::t2ADDrr},
2190 {ARM::t2ADDSrs, ARM::t2ADDrs},
2191
2192 {ARM::t2SUBSri, ARM::t2SUBri},
2193 {ARM::t2SUBSrr, ARM::t2SUBrr},
2194 {ARM::t2SUBSrs, ARM::t2SUBrs},
2195
2196 {ARM::t2RSBSri, ARM::t2RSBri},
2197 {ARM::t2RSBSrs, ARM::t2RSBrs},
2198};
2199
2200unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00002201 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2202 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2203 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002204 return 0;
2205}
2206
Evan Cheng780748d2009-07-28 05:48:47 +00002207void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002208 MachineBasicBlock::iterator &MBBI,
2209 const DebugLoc &dl, unsigned DestReg,
2210 unsigned BaseReg, int NumBytes,
2211 ARMCC::CondCodes Pred, unsigned PredReg,
2212 const ARMBaseInstrInfo &TII,
2213 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002214 if (NumBytes == 0 && DestReg != BaseReg) {
2215 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002216 .addReg(BaseReg, RegState::Kill)
2217 .add(predOps(Pred, PredReg))
2218 .add(condCodeOp())
2219 .setMIFlags(MIFlags);
Tim Northoverc9432eb2013-11-04 23:04:15 +00002220 return;
2221 }
2222
Evan Cheng780748d2009-07-28 05:48:47 +00002223 bool isSub = NumBytes < 0;
2224 if (isSub) NumBytes = -NumBytes;
2225
2226 while (NumBytes) {
2227 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2228 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2229 assert(ThisVal && "Didn't extract field correctly");
2230
2231 // We will handle these bits from offset, clear them.
2232 NumBytes &= ~ThisVal;
2233
2234 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2235
2236 // Build the new ADD / SUB.
2237 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2238 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002239 .addReg(BaseReg, RegState::Kill)
2240 .addImm(ThisVal)
2241 .add(predOps(Pred, PredReg))
2242 .add(condCodeOp())
2243 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002244 BaseReg = DestReg;
2245 }
2246}
2247
Tim Northoverdee86042013-12-02 14:46:26 +00002248bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2249 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002250 unsigned NumBytes) {
2251 // This optimisation potentially adds lots of load and store
2252 // micro-operations, it's only really a great benefit to code-size.
Matthias Braunf1caa282017-12-15 22:22:58 +00002253 if (!MF.getFunction().optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002254 return false;
2255
2256 // If only one register is pushed/popped, LLVM can use an LDR/STR
2257 // instead. We can't modify those so make sure we're dealing with an
2258 // instruction we understand.
2259 bool IsPop = isPopOpcode(MI->getOpcode());
2260 bool IsPush = isPushOpcode(MI->getOpcode());
2261 if (!IsPush && !IsPop)
2262 return false;
2263
2264 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2265 MI->getOpcode() == ARM::VLDMDIA_UPD;
2266 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2267 MI->getOpcode() == ARM::tPOP ||
2268 MI->getOpcode() == ARM::tPOP_RET;
2269
2270 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2271 MI->getOperand(1).getReg() == ARM::SP)) &&
2272 "trying to fold sp update into non-sp-updating push/pop");
2273
2274 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2275 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2276 // if this is violated.
2277 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2278 return false;
2279
2280 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2281 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2282 int RegListIdx = IsT1PushPop ? 2 : 4;
2283
2284 // Calculate the space we'll need in terms of registers.
Tim Northovera9cc3852016-10-26 20:01:00 +00002285 unsigned RegsNeeded;
2286 const TargetRegisterClass *RegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002287 if (IsVFPPushPop) {
Tim Northover93bcc662013-11-08 17:18:07 +00002288 RegsNeeded = NumBytes / 8;
Tim Northovera9cc3852016-10-26 20:01:00 +00002289 RegClass = &ARM::DPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002290 } else {
Tim Northover93bcc662013-11-08 17:18:07 +00002291 RegsNeeded = NumBytes / 4;
Tim Northovera9cc3852016-10-26 20:01:00 +00002292 RegClass = &ARM::GPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002293 }
2294
2295 // We're going to have to strip all list operands off before
2296 // re-adding them since the order matters, so save the existing ones
2297 // for later.
2298 SmallVector<MachineOperand, 4> RegList;
Tim Northovera9cc3852016-10-26 20:01:00 +00002299
2300 // We're also going to need the first register transferred by this
2301 // instruction, which won't necessarily be the first register in the list.
2302 unsigned FirstRegEnc = -1;
Tim Northover93bcc662013-11-08 17:18:07 +00002303
Tim Northover93bcc662013-11-08 17:18:07 +00002304 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northovera9cc3852016-10-26 20:01:00 +00002305 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2306 MachineOperand &MO = MI->getOperand(i);
2307 RegList.push_back(MO);
2308
2309 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2310 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2311 }
2312
Tim Northover45479dc2013-12-01 14:16:24 +00002313 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002314
2315 // Now try to find enough space in the reglist to allocate NumBytes.
Tim Northovera9cc3852016-10-26 20:01:00 +00002316 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2317 --CurRegEnc) {
2318 unsigned CurReg = RegClass->getRegister(CurRegEnc);
Tim Northover93bcc662013-11-08 17:18:07 +00002319 if (!IsPop) {
Momchil Velikovac7c5c12018-01-08 14:47:19 +00002320 // Pushing any register is completely harmless, mark the register involved
2321 // as undef since we don't care about its value and must not restore it
2322 // during stack unwinding.
Tim Northover93bcc662013-11-08 17:18:07 +00002323 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2324 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002325 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002326 continue;
2327 }
2328
Tim Northover45479dc2013-12-01 14:16:24 +00002329 // However, we can only pop an extra register if it's not live. For
2330 // registers live within the function we might clobber a return value
2331 // register; the other way a register can be live here is if it's
2332 // callee-saved.
2333 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002334 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2335 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002336 // VFP pops don't allow holes in the register list, so any skip is fatal
2337 // for our transformation. GPR pops do, so we should just keep looking.
2338 if (IsVFPPushPop)
2339 return false;
2340 else
2341 continue;
2342 }
Tim Northover93bcc662013-11-08 17:18:07 +00002343
2344 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002345 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2346 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002347 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002348 }
2349
2350 if (RegsNeeded > 0)
2351 return false;
2352
2353 // Finally we know we can profitably perform the optimisation so go
2354 // ahead: strip all existing registers off and add them back again
2355 // in the right order.
2356 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2357 MI->RemoveOperand(i);
2358
2359 // Add the complete list back in.
2360 MachineInstrBuilder MIB(MF, &*MI);
2361 for (int i = RegList.size() - 1; i >= 0; --i)
Diana Picus116bbab2017-01-13 09:58:52 +00002362 MIB.add(RegList[i]);
Tim Northover93bcc662013-11-08 17:18:07 +00002363
2364 return true;
2365}
2366
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002367bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2368 unsigned FrameReg, int &Offset,
2369 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002370 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002371 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002372 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2373 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002374
Evan Cheng780748d2009-07-28 05:48:47 +00002375 // Memory operands in inline assembly always use AddrMode2.
2376 if (Opcode == ARM::INLINEASM)
2377 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002378
Evan Cheng780748d2009-07-28 05:48:47 +00002379 if (Opcode == ARM::ADDri) {
2380 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2381 if (Offset == 0) {
2382 // Turn it into a move.
2383 MI.setDesc(TII.get(ARM::MOVr));
2384 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2385 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002386 Offset = 0;
2387 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002388 } else if (Offset < 0) {
2389 Offset = -Offset;
2390 isSub = true;
2391 MI.setDesc(TII.get(ARM::SUBri));
2392 }
2393
2394 // Common case: small offset, fits into instruction.
2395 if (ARM_AM::getSOImmVal(Offset) != -1) {
2396 // Replace the FrameIndex with sp / fp
2397 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2398 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002399 Offset = 0;
2400 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002401 }
2402
2403 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2404 // as possible.
2405 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2406 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2407
2408 // We will handle these bits from offset, clear them.
2409 Offset &= ~ThisImmVal;
2410
2411 // Get the properly encoded SOImmVal field.
2412 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2413 "Bit extraction didn't work?");
2414 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2415 } else {
2416 unsigned ImmIdx = 0;
2417 int InstrOffs = 0;
2418 unsigned NumBits = 0;
2419 unsigned Scale = 1;
2420 switch (AddrMode) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002421 case ARMII::AddrMode_i12:
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002422 ImmIdx = FrameRegIdx + 1;
2423 InstrOffs = MI.getOperand(ImmIdx).getImm();
2424 NumBits = 12;
2425 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002426 case ARMII::AddrMode2:
Evan Cheng780748d2009-07-28 05:48:47 +00002427 ImmIdx = FrameRegIdx+2;
2428 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2429 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2430 InstrOffs *= -1;
2431 NumBits = 12;
2432 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002433 case ARMII::AddrMode3:
Evan Cheng780748d2009-07-28 05:48:47 +00002434 ImmIdx = FrameRegIdx+2;
2435 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2436 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2437 InstrOffs *= -1;
2438 NumBits = 8;
2439 break;
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002440 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002441 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002442 // Can't fold any offset even if it's zero.
2443 return false;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002444 case ARMII::AddrMode5:
Evan Cheng780748d2009-07-28 05:48:47 +00002445 ImmIdx = FrameRegIdx+1;
2446 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2447 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2448 InstrOffs *= -1;
2449 NumBits = 8;
2450 Scale = 4;
2451 break;
Sjoerd Meijer011de9c2018-01-26 09:26:40 +00002452 case ARMII::AddrMode5FP16:
2453 ImmIdx = FrameRegIdx+1;
2454 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2455 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2456 InstrOffs *= -1;
2457 NumBits = 8;
2458 Scale = 2;
2459 break;
Evan Cheng780748d2009-07-28 05:48:47 +00002460 default:
2461 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002462 }
2463
2464 Offset += InstrOffs * Scale;
2465 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2466 if (Offset < 0) {
2467 Offset = -Offset;
2468 isSub = true;
2469 }
2470
2471 // Attempt to fold address comp. if opcode has offset bits
2472 if (NumBits > 0) {
2473 // Common case: small offset, fits into instruction.
2474 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2475 int ImmedOffset = Offset / Scale;
2476 unsigned Mask = (1 << NumBits) - 1;
2477 if ((unsigned)Offset <= Mask * Scale) {
2478 // Replace the FrameIndex with sp
2479 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002480 // FIXME: When addrmode2 goes away, this will simplify (like the
2481 // T2 version), as the LDR.i12 versions don't need the encoding
2482 // tricks for the offset value.
2483 if (isSub) {
2484 if (AddrMode == ARMII::AddrMode_i12)
2485 ImmedOffset = -ImmedOffset;
2486 else
2487 ImmedOffset |= 1 << NumBits;
2488 }
Evan Cheng780748d2009-07-28 05:48:47 +00002489 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002490 Offset = 0;
2491 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002492 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002493
Evan Cheng780748d2009-07-28 05:48:47 +00002494 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2495 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002496 if (isSub) {
2497 if (AddrMode == ARMII::AddrMode_i12)
2498 ImmedOffset = -ImmedOffset;
2499 else
2500 ImmedOffset |= 1 << NumBits;
2501 }
Evan Cheng780748d2009-07-28 05:48:47 +00002502 ImmOp.ChangeToImmediate(ImmedOffset);
2503 Offset &= ~(Mask*Scale);
2504 }
2505 }
2506
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002507 Offset = (isSub) ? -Offset : Offset;
2508 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002509}
Bill Wendling7de9d522010-08-06 01:32:48 +00002510
Manman Ren6fa76dc2012-06-29 21:33:59 +00002511/// analyzeCompare - For a comparison instruction, return the source registers
2512/// in SrcReg and SrcReg2 if having two register operands, and the value it
2513/// compares against in CmpValue. Return true if the comparison instruction
2514/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002515bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2516 unsigned &SrcReg2, int &CmpMask,
2517 int &CmpValue) const {
2518 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002519 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002520 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002521 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002522 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002523 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002524 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002525 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002526 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002527 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002528 case ARM::CMPrr:
2529 case ARM::t2CMPrr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002530 SrcReg = MI.getOperand(0).getReg();
2531 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002532 CmpMask = ~0;
2533 CmpValue = 0;
2534 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002535 case ARM::TSTri:
2536 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002537 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002538 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002539 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002540 CmpValue = 0;
2541 return true;
2542 }
2543
2544 return false;
2545}
2546
Gabor Greifd36e3e82010-09-29 10:12:08 +00002547/// isSuitableForMask - Identify a suitable 'and' instruction that
2548/// operates on the given source register and applies the same mask
2549/// as a 'tst' instruction. Provide a limited look-through for copies.
2550/// When successful, MI will hold the found instruction.
2551static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002552 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002553 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002554 case ARM::ANDri:
2555 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002556 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002557 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002558 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002559 return true;
2560 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002561 }
2562
2563 return false;
2564}
2565
Manman Renb1b3db62012-06-29 22:06:19 +00002566/// getSwappedCondition - assume the flags are set by MI(a,b), return
2567/// the condition code if we modify the instructions such that flags are
2568/// set by MI(b,a).
2569inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2570 switch (CC) {
2571 default: return ARMCC::AL;
2572 case ARMCC::EQ: return ARMCC::EQ;
2573 case ARMCC::NE: return ARMCC::NE;
2574 case ARMCC::HS: return ARMCC::LS;
2575 case ARMCC::LO: return ARMCC::HI;
2576 case ARMCC::HI: return ARMCC::LO;
2577 case ARMCC::LS: return ARMCC::HS;
2578 case ARMCC::GE: return ARMCC::LE;
2579 case ARMCC::LT: return ARMCC::GT;
2580 case ARMCC::GT: return ARMCC::LT;
2581 case ARMCC::LE: return ARMCC::GE;
2582 }
2583}
2584
Joel Galensonfe7fa402018-01-17 19:19:05 +00002585/// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2586/// the condition code if we modify the instructions such that flags are
2587/// set by ADD(a,b,X).
2588inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
2589 switch (CC) {
2590 default: return ARMCC::AL;
2591 case ARMCC::HS: return ARMCC::LO;
2592 case ARMCC::LO: return ARMCC::HS;
2593 case ARMCC::VS: return ARMCC::VS;
2594 case ARMCC::VC: return ARMCC::VC;
2595 }
2596}
2597
Manman Renb1b3db62012-06-29 22:06:19 +00002598/// isRedundantFlagInstr - check whether the first instruction, whose only
2599/// purpose is to update flags, can be made redundant.
2600/// CMPrr can be made redundant by SUBrr if the operands are the same.
2601/// CMPri can be made redundant by SUBri if the operands are the same.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002602/// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
Manman Renb1b3db62012-06-29 22:06:19 +00002603/// This function can be extended later on.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002604inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
2605 unsigned SrcReg, unsigned SrcReg2,
2606 int ImmValue, const MachineInstr *OI) {
Manman Renb1b3db62012-06-29 22:06:19 +00002607 if ((CmpI->getOpcode() == ARM::CMPrr ||
2608 CmpI->getOpcode() == ARM::t2CMPrr) &&
2609 (OI->getOpcode() == ARM::SUBrr ||
2610 OI->getOpcode() == ARM::t2SUBrr) &&
2611 ((OI->getOperand(1).getReg() == SrcReg &&
2612 OI->getOperand(2).getReg() == SrcReg2) ||
2613 (OI->getOperand(1).getReg() == SrcReg2 &&
2614 OI->getOperand(2).getReg() == SrcReg)))
2615 return true;
2616
2617 if ((CmpI->getOpcode() == ARM::CMPri ||
2618 CmpI->getOpcode() == ARM::t2CMPri) &&
2619 (OI->getOpcode() == ARM::SUBri ||
2620 OI->getOpcode() == ARM::t2SUBri) &&
2621 OI->getOperand(1).getReg() == SrcReg &&
2622 OI->getOperand(2).getImm() == ImmValue)
2623 return true;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002624
2625 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2626 (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
2627 OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
2628 OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
2629 OI->getOperand(0).getReg() == SrcReg &&
2630 OI->getOperand(1).getReg() == SrcReg2)
2631 return true;
Manman Renb1b3db62012-06-29 22:06:19 +00002632 return false;
2633}
2634
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002635static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2636 switch (MI->getOpcode()) {
2637 default: return false;
2638 case ARM::tLSLri:
2639 case ARM::tLSRri:
2640 case ARM::tLSLrr:
2641 case ARM::tLSRrr:
2642 case ARM::tSUBrr:
2643 case ARM::tADDrr:
2644 case ARM::tADDi3:
2645 case ARM::tADDi8:
2646 case ARM::tSUBi3:
2647 case ARM::tSUBi8:
2648 case ARM::tMUL:
2649 IsThumb1 = true;
2650 LLVM_FALLTHROUGH;
2651 case ARM::RSBrr:
2652 case ARM::RSBri:
2653 case ARM::RSCrr:
2654 case ARM::RSCri:
2655 case ARM::ADDrr:
2656 case ARM::ADDri:
2657 case ARM::ADCrr:
2658 case ARM::ADCri:
2659 case ARM::SUBrr:
2660 case ARM::SUBri:
2661 case ARM::SBCrr:
2662 case ARM::SBCri:
2663 case ARM::t2RSBri:
2664 case ARM::t2ADDrr:
2665 case ARM::t2ADDri:
2666 case ARM::t2ADCrr:
2667 case ARM::t2ADCri:
2668 case ARM::t2SUBrr:
2669 case ARM::t2SUBri:
2670 case ARM::t2SBCrr:
2671 case ARM::t2SBCri:
2672 case ARM::ANDrr:
2673 case ARM::ANDri:
2674 case ARM::t2ANDrr:
2675 case ARM::t2ANDri:
2676 case ARM::ORRrr:
2677 case ARM::ORRri:
2678 case ARM::t2ORRrr:
2679 case ARM::t2ORRri:
2680 case ARM::EORrr:
2681 case ARM::EORri:
2682 case ARM::t2EORrr:
2683 case ARM::t2EORri:
2684 case ARM::t2LSRri:
2685 case ARM::t2LSRrr:
2686 case ARM::t2LSLri:
2687 case ARM::t2LSLrr:
2688 return true;
2689 }
2690}
2691
Manman Ren6fa76dc2012-06-29 21:33:59 +00002692/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2693/// comparison into one that sets the zero bit in the flags register;
2694/// Remove a redundant Compare instruction if an earlier instruction can set the
2695/// flags in the same way as Compare.
2696/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2697/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2698/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002699bool ARMBaseInstrInfo::optimizeCompareInstr(
2700 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2701 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002702 // Get the unique definition of SrcReg.
2703 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2704 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002705
Gabor Greifadbbb932010-09-21 12:01:15 +00002706 // Masked compares sometimes use the same register as the corresponding 'and'.
2707 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002708 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002709 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002710 for (MachineRegisterInfo::use_instr_iterator
2711 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2712 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002713 if (UI->getParent() != CmpInstr.getParent())
2714 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002715 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002716 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002717 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002718 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002719 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002720 break;
2721 }
2722 if (!MI) return false;
2723 }
2724 }
2725
Manman Rendc8ad002012-05-11 01:30:47 +00002726 // Get ready to iterate backward from CmpInstr.
2727 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002728 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002729
2730 // Early exit if CmpInstr is at the beginning of the BB.
2731 if (I == B) return false;
2732
Manman Rendc8ad002012-05-11 01:30:47 +00002733 // There are two possible candidates which can be changed to set CPSR:
Joel Galensonfe7fa402018-01-17 19:19:05 +00002734 // One is MI, the other is a SUB or ADD instruction.
2735 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
2736 // ADDr[ri](r1, r2, X).
Manman Rendc8ad002012-05-11 01:30:47 +00002737 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Joel Galensonfe7fa402018-01-17 19:19:05 +00002738 MachineInstr *SubAdd = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002739 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002740 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002741 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002742 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002743 // Conservatively refuse to convert an instruction which isn't in the same
2744 // BB as the comparison.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002745 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002746 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002747 if (CmpInstr.getOpcode() == ARM::CMPri ||
2748 CmpInstr.getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002749 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002750 else
2751 return false;
2752 }
2753
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002754 bool IsThumb1 = false;
2755 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2756 return false;
2757
2758 // We also want to do this peephole for cases like this: if (a*b == 0),
2759 // and optimise away the CMP instruction from the generated code sequence:
2760 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2761 // resulting from the select instruction, but these MOVS instructions for
2762 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2763 // However, if we only have MOVS instructions in between the CMP and the
2764 // other instruction (the MULS in this example), then the CPSR is dead so we
2765 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2766 // reordering and then continue the analysis hoping we can eliminate the
2767 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2768 // consequence, the movs won't redefine/kill the MUL operands which would
2769 // make this reordering illegal.
2770 if (MI && IsThumb1) {
2771 --I;
2772 bool CanReorder = true;
2773 const bool HasStmts = I != E;
2774 for (; I != E; --I) {
2775 if (I->getOpcode() != ARM::tMOVi8) {
2776 CanReorder = false;
2777 break;
2778 }
2779 }
2780 if (HasStmts && CanReorder) {
2781 MI = MI->removeFromParent();
2782 E = CmpInstr;
2783 CmpInstr.getParent()->insert(E, MI);
2784 }
2785 I = CmpInstr;
2786 E = MI;
2787 }
2788
Manman Rendc8ad002012-05-11 01:30:47 +00002789 // Check that CPSR isn't set between the comparison instruction and the one we
Joel Galensonfe7fa402018-01-17 19:19:05 +00002790 // want to change. At the same time, search for SubAdd.
Manman Renb1b3db62012-06-29 22:06:19 +00002791 const TargetRegisterInfo *TRI = &getRegisterInfo();
Joel Galenson1d89cd22018-01-22 17:53:47 +00002792 do {
2793 const MachineInstr &Instr = *--I;
Bill Wendling7de9d522010-08-06 01:32:48 +00002794
Joel Galensonfe7fa402018-01-17 19:19:05 +00002795 // Check whether CmpInstr can be made redundant by the current instruction.
Joel Galenson1d89cd22018-01-22 17:53:47 +00002796 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr)) {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002797 SubAdd = &*I;
2798 break;
2799 }
2800
Joel Galenson1d89cd22018-01-22 17:53:47 +00002801 // Allow E (which was initially MI) to be SubAdd but do not search before E.
2802 if (I == E)
2803 break;
2804
Manman Renb1b3db62012-06-29 22:06:19 +00002805 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2806 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002807 // This instruction modifies or uses CPSR after the one we want to
2808 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002809 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002810
Joel Galenson1d89cd22018-01-22 17:53:47 +00002811 } while (I != B);
Bill Wendling7de9d522010-08-06 01:32:48 +00002812
Manman Rendc8ad002012-05-11 01:30:47 +00002813 // Return false if no candidates exist.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002814 if (!MI && !SubAdd)
Manman Rendc8ad002012-05-11 01:30:47 +00002815 return false;
2816
2817 // The single candidate is called MI.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002818 if (!MI) MI = SubAdd;
Manman Rendc8ad002012-05-11 01:30:47 +00002819
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002820 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002821 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002822 return false;
2823
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002824 // Scan forward for the use of CPSR
2825 // When checking against MI: if it's a conditional code that requires
2826 // checking of the V bit or C bit, then this is not safe to do.
2827 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2828 // If we are done with the basic block, we need to check whether CPSR is
2829 // live-out.
2830 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2831 OperandsToUpdate;
2832 bool isSafe = false;
2833 I = CmpInstr;
2834 E = CmpInstr.getParent()->end();
2835 while (!isSafe && ++I != E) {
2836 const MachineInstr &Instr = *I;
2837 for (unsigned IO = 0, EO = Instr.getNumOperands();
2838 !isSafe && IO != EO; ++IO) {
2839 const MachineOperand &MO = Instr.getOperand(IO);
2840 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2841 isSafe = true;
2842 break;
2843 }
2844 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2845 continue;
2846 if (MO.isDef()) {
2847 isSafe = true;
2848 break;
2849 }
2850 // Condition code is after the operand before CPSR except for VSELs.
2851 ARMCC::CondCodes CC;
2852 bool IsInstrVSel = true;
2853 switch (Instr.getOpcode()) {
2854 default:
2855 IsInstrVSel = false;
2856 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2857 break;
2858 case ARM::VSELEQD:
2859 case ARM::VSELEQS:
2860 CC = ARMCC::EQ;
2861 break;
2862 case ARM::VSELGTD:
2863 case ARM::VSELGTS:
2864 CC = ARMCC::GT;
2865 break;
2866 case ARM::VSELGED:
2867 case ARM::VSELGES:
2868 CC = ARMCC::GE;
2869 break;
2870 case ARM::VSELVSS:
2871 case ARM::VSELVSD:
2872 CC = ARMCC::VS;
2873 break;
2874 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002875
Joel Galensonfe7fa402018-01-17 19:19:05 +00002876 if (SubAdd) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002877 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2878 // on CMP needs to be updated to be based on SUB.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002879 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
2880 // needs to be modified.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002881 // Push the condition code operands to OperandsToUpdate.
2882 // If it is safe to remove CmpInstr, the condition code of these
2883 // operands will be modified.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002884 unsigned Opc = SubAdd->getOpcode();
2885 bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
2886 Opc == ARM::SUBri || Opc == ARM::t2SUBri;
2887 if (!IsSub || (SrcReg2 != 0 && SubAdd->getOperand(1).getReg() == SrcReg2 &&
2888 SubAdd->getOperand(2).getReg() == SrcReg)) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002889 // VSel doesn't support condition code update.
2890 if (IsInstrVSel)
Manman Rendc8ad002012-05-11 01:30:47 +00002891 return false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002892 // Ensure we can swap the condition.
2893 ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
2894 if (NewCC == ARMCC::AL)
2895 return false;
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002896 OperandsToUpdate.push_back(
2897 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2898 }
2899 } else {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002900 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002901 switch (CC) {
2902 case ARMCC::EQ: // Z
2903 case ARMCC::NE: // Z
2904 case ARMCC::MI: // N
2905 case ARMCC::PL: // N
2906 case ARMCC::AL: // none
2907 // CPSR can be used multiple times, we should continue.
2908 break;
2909 case ARMCC::HS: // C
2910 case ARMCC::LO: // C
2911 case ARMCC::VS: // V
2912 case ARMCC::VC: // V
2913 case ARMCC::HI: // C Z
2914 case ARMCC::LS: // C Z
2915 case ARMCC::GE: // N V
2916 case ARMCC::LT: // N V
2917 case ARMCC::GT: // Z N V
2918 case ARMCC::LE: // Z N V
2919 // The instruction uses the V bit or C bit which is not safe.
2920 return false;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002921 }
Evan Cheng425489d2011-03-23 22:52:04 +00002922 }
2923 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002924 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002925
2926 // If CPSR is not killed nor re-defined, we should check whether it is
2927 // live-out. If it is live-out, do not optimize.
2928 if (!isSafe) {
2929 MachineBasicBlock *MBB = CmpInstr.getParent();
2930 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2931 SE = MBB->succ_end(); SI != SE; ++SI)
2932 if ((*SI)->isLiveIn(ARM::CPSR))
2933 return false;
Cameron Zwarich0829b302011-04-15 20:45:00 +00002934 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002935
2936 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2937 // set CPSR so this is represented as an explicit output)
2938 if (!IsThumb1) {
2939 MI->getOperand(5).setReg(ARM::CPSR);
2940 MI->getOperand(5).setIsDef(true);
2941 }
2942 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
2943 CmpInstr.eraseFromParent();
2944
2945 // Modify the condition code of operands in OperandsToUpdate.
2946 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2947 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2948 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2949 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2950
2951 return true;
Bill Wendling7de9d522010-08-06 01:32:48 +00002952}
Evan Cheng367a5df2010-09-09 18:18:55 +00002953
Joel Galensonfe7fa402018-01-17 19:19:05 +00002954bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
2955 // Do not sink MI if it might be used to optimize a redundant compare.
2956 // We heuristically only look at the instruction immediately following MI to
2957 // avoid potentially searching the entire basic block.
2958 if (isPredicated(MI))
2959 return true;
2960 MachineBasicBlock::const_iterator Next = &MI;
2961 ++Next;
2962 unsigned SrcReg, SrcReg2;
2963 int CmpMask, CmpValue;
2964 if (Next != MI.getParent()->end() &&
2965 analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
2966 isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI))
2967 return false;
2968 return true;
2969}
2970
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002971bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2972 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002973 MachineRegisterInfo *MRI) const {
2974 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002975 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002976 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2977 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002978 if (!DefMI.getOperand(1).isImm())
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00002979 // Could be t2MOVi32imm @xx
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002980 return false;
2981
2982 if (!MRI->hasOneNonDBGUse(Reg))
2983 return false;
2984
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002985 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002986 if (DefMCID.hasOptionalDef()) {
2987 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002988 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00002989 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2990 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2991 // to delete DefMI.
2992 return false;
2993 }
2994
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002995 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002996 if (UseMCID.hasOptionalDef()) {
2997 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002998 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00002999 // If the instruction sets the flag, do not attempt this optimization
3000 // since it may change the semantics of the code.
3001 return false;
3002 }
3003
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003004 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003005 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003006 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003007 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003008 bool Commute = false;
3009 switch (UseOpc) {
3010 default: return false;
3011 case ARM::SUBrr:
3012 case ARM::ADDrr:
3013 case ARM::ORRrr:
3014 case ARM::EORrr:
3015 case ARM::t2SUBrr:
3016 case ARM::t2ADDrr:
3017 case ARM::t2ORRrr:
3018 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003019 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003020 switch (UseOpc) {
3021 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003022 case ARM::ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003023 case ARM::SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003024 if (UseOpc == ARM::SUBrr && Commute)
3025 return false;
3026
3027 // ADD/SUB are special because they're essentially the same operation, so
3028 // we can handle a larger range of immediates.
3029 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
3030 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
3031 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
3032 ImmVal = -ImmVal;
3033 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
3034 } else
3035 return false;
3036 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3037 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3038 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003039 case ARM::ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003040 case ARM::EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003041 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
3042 return false;
3043 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3044 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3045 switch (UseOpc) {
3046 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003047 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
3048 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
3049 }
3050 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003051 case ARM::t2ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003052 case ARM::t2SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003053 if (UseOpc == ARM::t2SUBrr && Commute)
3054 return false;
3055
3056 // ADD/SUB are special because they're essentially the same operation, so
3057 // we can handle a larger range of immediates.
3058 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3059 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
3060 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
3061 ImmVal = -ImmVal;
3062 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
3063 } else
3064 return false;
3065 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3066 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3067 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003068 case ARM::t2ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003069 case ARM::t2EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003070 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3071 return false;
3072 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3073 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3074 switch (UseOpc) {
3075 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003076 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
3077 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
3078 }
3079 break;
3080 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003081 }
3082 }
3083
3084 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003085 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
3086 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003087 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Diana Picus8a73f552017-01-13 10:18:01 +00003088 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
3089 NewReg)
3090 .addReg(Reg1, getKillRegState(isKill))
3091 .addImm(SOImmValV1)
3092 .add(predOps(ARMCC::AL))
3093 .add(condCodeOp());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003094 UseMI.setDesc(get(NewUseOpc));
3095 UseMI.getOperand(1).setReg(NewReg);
3096 UseMI.getOperand(1).setIsKill();
3097 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
3098 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003099 return true;
3100}
3101
Bob Wilsone8a549c2012-09-29 21:43:49 +00003102static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003103 const MachineInstr &MI) {
3104 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003105 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003106 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003107 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
3108 assert(UOps >= 0 && "bad # UOps");
3109 return UOps;
3110 }
3111
3112 case ARM::LDRrs:
3113 case ARM::LDRBrs:
3114 case ARM::STRrs:
3115 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003116 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003117 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3118 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3119 if (!isSub &&
3120 (ShImm == 0 ||
3121 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3122 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3123 return 1;
3124 return 2;
3125 }
3126
3127 case ARM::LDRH:
3128 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003129 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00003130 return 1;
3131
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003132 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003133 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3134 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3135 if (!isSub &&
3136 (ShImm == 0 ||
3137 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3138 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3139 return 1;
3140 return 2;
3141 }
3142
3143 case ARM::LDRSB:
3144 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003145 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003146
3147 case ARM::LDRSB_POST:
3148 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003149 unsigned Rt = MI.getOperand(0).getReg();
3150 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003151 return (Rt == Rm) ? 4 : 3;
3152 }
3153
3154 case ARM::LDR_PRE_REG:
3155 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003156 unsigned Rt = MI.getOperand(0).getReg();
3157 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003158 if (Rt == Rm)
3159 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003160 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003161 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3162 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3163 if (!isSub &&
3164 (ShImm == 0 ||
3165 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3166 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3167 return 2;
3168 return 3;
3169 }
3170
3171 case ARM::STR_PRE_REG:
3172 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003173 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003174 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3175 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3176 if (!isSub &&
3177 (ShImm == 0 ||
3178 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3179 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3180 return 2;
3181 return 3;
3182 }
3183
3184 case ARM::LDRH_PRE:
3185 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003186 unsigned Rt = MI.getOperand(0).getReg();
3187 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003188 if (!Rm)
3189 return 2;
3190 if (Rt == Rm)
3191 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003192 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003193 }
3194
3195 case ARM::LDR_POST_REG:
3196 case ARM::LDRB_POST_REG:
3197 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003198 unsigned Rt = MI.getOperand(0).getReg();
3199 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003200 return (Rt == Rm) ? 3 : 2;
3201 }
3202
3203 case ARM::LDR_PRE_IMM:
3204 case ARM::LDRB_PRE_IMM:
3205 case ARM::LDR_POST_IMM:
3206 case ARM::LDRB_POST_IMM:
3207 case ARM::STRB_POST_IMM:
3208 case ARM::STRB_POST_REG:
3209 case ARM::STRB_PRE_IMM:
3210 case ARM::STRH_POST:
3211 case ARM::STR_POST_IMM:
3212 case ARM::STR_POST_REG:
3213 case ARM::STR_PRE_IMM:
3214 return 2;
3215
3216 case ARM::LDRSB_PRE:
3217 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003218 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003219 if (Rm == 0)
3220 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003221 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003222 if (Rt == Rm)
3223 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003224 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003225 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3226 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3227 if (!isSub &&
3228 (ShImm == 0 ||
3229 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3230 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3231 return 3;
3232 return 4;
3233 }
3234
3235 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003236 unsigned Rt = MI.getOperand(0).getReg();
3237 unsigned Rn = MI.getOperand(2).getReg();
3238 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003239 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003240 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3241 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003242 return (Rt == Rn) ? 3 : 2;
3243 }
3244
3245 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003246 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003247 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003248 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3249 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003250 return 2;
3251 }
3252
3253 case ARM::LDRD_POST:
3254 case ARM::t2LDRD_POST:
3255 return 3;
3256
3257 case ARM::STRD_POST:
3258 case ARM::t2STRD_POST:
3259 return 4;
3260
3261 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003262 unsigned Rt = MI.getOperand(0).getReg();
3263 unsigned Rn = MI.getOperand(3).getReg();
3264 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003265 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003266 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3267 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003268 return (Rt == Rn) ? 4 : 3;
3269 }
3270
3271 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003272 unsigned Rt = MI.getOperand(0).getReg();
3273 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003274 return (Rt == Rn) ? 4 : 3;
3275 }
3276
3277 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003278 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003279 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003280 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3281 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003282 return 3;
3283 }
3284
3285 case ARM::t2STRD_PRE:
3286 return 3;
3287
3288 case ARM::t2LDR_POST:
3289 case ARM::t2LDRB_POST:
3290 case ARM::t2LDRB_PRE:
3291 case ARM::t2LDRSBi12:
3292 case ARM::t2LDRSBi8:
3293 case ARM::t2LDRSBpci:
3294 case ARM::t2LDRSBs:
3295 case ARM::t2LDRH_POST:
3296 case ARM::t2LDRH_PRE:
3297 case ARM::t2LDRSBT:
3298 case ARM::t2LDRSB_POST:
3299 case ARM::t2LDRSB_PRE:
3300 case ARM::t2LDRSH_POST:
3301 case ARM::t2LDRSH_PRE:
3302 case ARM::t2LDRSHi12:
3303 case ARM::t2LDRSHi8:
3304 case ARM::t2LDRSHpci:
3305 case ARM::t2LDRSHs:
3306 return 2;
3307
3308 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003309 unsigned Rt = MI.getOperand(0).getReg();
3310 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003311 return (Rt == Rn) ? 3 : 2;
3312 }
3313
3314 case ARM::t2STRB_POST:
3315 case ARM::t2STRB_PRE:
3316 case ARM::t2STRBs:
3317 case ARM::t2STRDi8:
3318 case ARM::t2STRH_POST:
3319 case ARM::t2STRH_PRE:
3320 case ARM::t2STRHs:
3321 case ARM::t2STR_POST:
3322 case ARM::t2STR_PRE:
3323 case ARM::t2STRs:
3324 return 2;
3325 }
3326}
3327
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003328// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3329// can't be easily determined return 0 (missing MachineMemOperand).
3330//
3331// FIXME: The current MachineInstr design does not support relying on machine
3332// mem operands to determine the width of a memory access. Instead, we expect
3333// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003334// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003335// two reasons:
3336//
3337// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3338// operands. This is much more dangerous than using the MachineMemOperand
3339// sizes because CodeGen passes can insert/remove optional machine operands. In
3340// fact, it's totally incorrect for preRA passes and appears to be wrong for
3341// postRA passes as well.
3342//
3343// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3344// machine model that calls this should handle the unknown (zero size) case.
3345//
3346// Long term, we should require a target hook that verifies MachineMemOperand
3347// sizes during MC lowering. That target hook should be local to MC lowering
3348// because we can't ensure that it is aware of other MI forms. Doing this will
3349// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003350unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003351 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003352 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3353 E = MI.memoperands_end();
3354 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003355 Size += (*I)->getSize();
3356 }
3357 return Size / 4;
3358}
3359
Diana Picus92423ce2016-06-27 09:08:23 +00003360static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3361 unsigned NumRegs) {
3362 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3363 switch (Opc) {
3364 default:
3365 break;
3366 case ARM::VLDMDIA_UPD:
3367 case ARM::VLDMDDB_UPD:
3368 case ARM::VLDMSIA_UPD:
3369 case ARM::VLDMSDB_UPD:
3370 case ARM::VSTMDIA_UPD:
3371 case ARM::VSTMDDB_UPD:
3372 case ARM::VSTMSIA_UPD:
3373 case ARM::VSTMSDB_UPD:
3374 case ARM::LDMIA_UPD:
3375 case ARM::LDMDA_UPD:
3376 case ARM::LDMDB_UPD:
3377 case ARM::LDMIB_UPD:
3378 case ARM::STMIA_UPD:
3379 case ARM::STMDA_UPD:
3380 case ARM::STMDB_UPD:
3381 case ARM::STMIB_UPD:
3382 case ARM::tLDMIA_UPD:
3383 case ARM::tSTMIA_UPD:
3384 case ARM::t2LDMIA_UPD:
3385 case ARM::t2LDMDB_UPD:
3386 case ARM::t2STMIA_UPD:
3387 case ARM::t2STMDB_UPD:
3388 ++UOps; // One for base register writeback.
3389 break;
3390 case ARM::LDMIA_RET:
3391 case ARM::tPOP_RET:
3392 case ARM::t2LDMIA_RET:
3393 UOps += 2; // One for base reg wb, one for write to pc.
3394 break;
3395 }
3396 return UOps;
3397}
3398
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003399unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3400 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003401 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003402 return 1;
3403
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003404 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003405 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003406 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003407 if (ItinUOps >= 0) {
3408 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3409 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3410
Andrew Trickf161e392012-07-02 18:10:42 +00003411 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003412 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003413
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003414 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003415 switch (Opc) {
3416 default:
3417 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003418 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003419 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003420 return 2;
3421
3422 // The number of uOps for load / store multiple are determined by the number
3423 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003424 //
Evan Chengbf407072010-09-10 01:29:16 +00003425 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3426 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003427 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003428 //
Evan Chengbf407072010-09-10 01:29:16 +00003429 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003430 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3431 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3432 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003433 case ARM::VLDMDIA_UPD:
3434 case ARM::VLDMDDB_UPD:
3435 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003436 case ARM::VLDMSIA_UPD:
3437 case ARM::VLDMSDB_UPD:
3438 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003439 case ARM::VSTMDIA_UPD:
3440 case ARM::VSTMDDB_UPD:
3441 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003442 case ARM::VSTMSIA_UPD:
3443 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003444 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003445 return (NumRegs / 2) + (NumRegs % 2) + 1;
3446 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003447
3448 case ARM::LDMIA_RET:
3449 case ARM::LDMIA:
3450 case ARM::LDMDA:
3451 case ARM::LDMDB:
3452 case ARM::LDMIB:
3453 case ARM::LDMIA_UPD:
3454 case ARM::LDMDA_UPD:
3455 case ARM::LDMDB_UPD:
3456 case ARM::LDMIB_UPD:
3457 case ARM::STMIA:
3458 case ARM::STMDA:
3459 case ARM::STMDB:
3460 case ARM::STMIB:
3461 case ARM::STMIA_UPD:
3462 case ARM::STMDA_UPD:
3463 case ARM::STMDB_UPD:
3464 case ARM::STMIB_UPD:
3465 case ARM::tLDMIA:
3466 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003467 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003468 case ARM::tPOP_RET:
3469 case ARM::tPOP:
3470 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003471 case ARM::t2LDMIA_RET:
3472 case ARM::t2LDMIA:
3473 case ARM::t2LDMDB:
3474 case ARM::t2LDMIA_UPD:
3475 case ARM::t2LDMDB_UPD:
3476 case ARM::t2STMIA:
3477 case ARM::t2STMDB:
3478 case ARM::t2STMIA_UPD:
3479 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003480 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003481 switch (Subtarget.getLdStMultipleTiming()) {
3482 case ARMSubtarget::SingleIssuePlusExtras:
3483 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3484 case ARMSubtarget::SingleIssue:
3485 // Assume the worst.
3486 return NumRegs;
3487 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003488 if (NumRegs < 4)
3489 return 2;
3490 // 4 registers would be issued: 2, 2.
3491 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003492 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003493 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003494 ++UOps;
3495 return UOps;
3496 }
3497 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3498 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003499 // If there are odd number of registers or if it's not 64-bit aligned,
3500 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003501 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3502 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003503 ++UOps;
3504 return UOps;
3505 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003506 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003507 }
3508 }
Diana Picus92423ce2016-06-27 09:08:23 +00003509 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003510}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003511
3512int
Evan Cheng412e37b2010-10-07 23:12:15 +00003513ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003514 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003515 unsigned DefClass,
3516 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003517 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003518 if (RegNo <= 0)
3519 // Def is the address writeback.
3520 return ItinData->getOperandCycle(DefClass, DefIdx);
3521
3522 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003523 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003524 // (regno / 2) + (regno % 2) + 1
3525 DefCycle = RegNo / 2 + 1;
3526 if (RegNo % 2)
3527 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003528 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003529 DefCycle = RegNo;
3530 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003531
Evan Cheng6cc775f2011-06-28 19:10:37 +00003532 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003533 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003534 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003535 case ARM::VLDMSIA_UPD:
3536 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003537 isSLoad = true;
3538 break;
3539 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003540
Evan Cheng412e37b2010-10-07 23:12:15 +00003541 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3542 // then it takes an extra cycle.
3543 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3544 ++DefCycle;
3545 } else {
3546 // Assume the worst.
3547 DefCycle = RegNo + 2;
3548 }
3549
3550 return DefCycle;
3551}
3552
Javed Absar4ae7e8122017-06-02 08:53:19 +00003553bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3554 unsigned BaseReg = MI.getOperand(0).getReg();
3555 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3556 const auto &Op = MI.getOperand(i);
3557 if (Op.isReg() && Op.getReg() == BaseReg)
3558 return true;
3559 }
3560 return false;
3561}
3562unsigned
3563ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00003564 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops
3565 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops)
Javed Absar4ae7e8122017-06-02 08:53:19 +00003566 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3567}
3568
Evan Cheng412e37b2010-10-07 23:12:15 +00003569int
3570ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003571 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003572 unsigned DefClass,
3573 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003574 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003575 if (RegNo <= 0)
3576 // Def is the address writeback.
3577 return ItinData->getOperandCycle(DefClass, DefIdx);
3578
3579 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003580 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003581 // 4 registers would be issued: 1, 2, 1.
3582 // 5 registers would be issued: 1, 2, 2.
3583 DefCycle = RegNo / 2;
3584 if (DefCycle < 1)
3585 DefCycle = 1;
3586 // Result latency is issue cycle + 2: E2.
3587 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003588 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003589 DefCycle = (RegNo / 2);
3590 // If there are odd number of registers or if it's not 64-bit aligned,
3591 // then it takes an extra AGU (Address Generation Unit) cycle.
3592 if ((RegNo % 2) || DefAlign < 8)
3593 ++DefCycle;
3594 // Result latency is AGU cycles + 2.
3595 DefCycle += 2;
3596 } else {
3597 // Assume the worst.
3598 DefCycle = RegNo + 2;
3599 }
3600
3601 return DefCycle;
3602}
3603
3604int
3605ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003606 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003607 unsigned UseClass,
3608 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003609 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003610 if (RegNo <= 0)
3611 return ItinData->getOperandCycle(UseClass, UseIdx);
3612
3613 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003614 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003615 // (regno / 2) + (regno % 2) + 1
3616 UseCycle = RegNo / 2 + 1;
3617 if (RegNo % 2)
3618 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003619 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003620 UseCycle = RegNo;
3621 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003622
Evan Cheng6cc775f2011-06-28 19:10:37 +00003623 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003624 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003625 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003626 case ARM::VSTMSIA_UPD:
3627 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003628 isSStore = true;
3629 break;
3630 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003631
Evan Cheng412e37b2010-10-07 23:12:15 +00003632 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3633 // then it takes an extra cycle.
3634 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3635 ++UseCycle;
3636 } else {
3637 // Assume the worst.
3638 UseCycle = RegNo + 2;
3639 }
3640
3641 return UseCycle;
3642}
3643
3644int
3645ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003646 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003647 unsigned UseClass,
3648 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003649 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003650 if (RegNo <= 0)
3651 return ItinData->getOperandCycle(UseClass, UseIdx);
3652
3653 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003654 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003655 UseCycle = RegNo / 2;
3656 if (UseCycle < 2)
3657 UseCycle = 2;
3658 // Read in E3.
3659 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003660 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003661 UseCycle = (RegNo / 2);
3662 // If there are odd number of registers or if it's not 64-bit aligned,
3663 // then it takes an extra AGU (Address Generation Unit) cycle.
3664 if ((RegNo % 2) || UseAlign < 8)
3665 ++UseCycle;
3666 } else {
3667 // Assume the worst.
3668 UseCycle = 1;
3669 }
3670 return UseCycle;
3671}
3672
3673int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003674ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003675 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003676 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003677 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003678 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003679 unsigned DefClass = DefMCID.getSchedClass();
3680 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003681
Evan Cheng6cc775f2011-06-28 19:10:37 +00003682 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003683 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3684
3685 // This may be a def / use of a variable_ops instruction, the operand
3686 // latency might be determinable dynamically. Let the target try to
3687 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003688 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003689 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003690 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003691 default:
3692 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3693 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003694
3695 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003696 case ARM::VLDMDIA_UPD:
3697 case ARM::VLDMDDB_UPD:
3698 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003699 case ARM::VLDMSIA_UPD:
3700 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003701 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003702 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003703
3704 case ARM::LDMIA_RET:
3705 case ARM::LDMIA:
3706 case ARM::LDMDA:
3707 case ARM::LDMDB:
3708 case ARM::LDMIB:
3709 case ARM::LDMIA_UPD:
3710 case ARM::LDMDA_UPD:
3711 case ARM::LDMDB_UPD:
3712 case ARM::LDMIB_UPD:
3713 case ARM::tLDMIA:
3714 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003715 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003716 case ARM::t2LDMIA_RET:
3717 case ARM::t2LDMIA:
3718 case ARM::t2LDMDB:
3719 case ARM::t2LDMIA_UPD:
3720 case ARM::t2LDMDB_UPD:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003721 LdmBypass = true;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003722 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003723 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003724 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003725
3726 if (DefCycle == -1)
3727 // We can't seem to determine the result latency of the def, assume it's 2.
3728 DefCycle = 2;
3729
3730 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003731 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003732 default:
3733 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3734 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003735
3736 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003737 case ARM::VSTMDIA_UPD:
3738 case ARM::VSTMDDB_UPD:
3739 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003740 case ARM::VSTMSIA_UPD:
3741 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003742 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003743 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003744
3745 case ARM::STMIA:
3746 case ARM::STMDA:
3747 case ARM::STMDB:
3748 case ARM::STMIB:
3749 case ARM::STMIA_UPD:
3750 case ARM::STMDA_UPD:
3751 case ARM::STMDB_UPD:
3752 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003753 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003754 case ARM::tPOP_RET:
3755 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003756 case ARM::t2STMIA:
3757 case ARM::t2STMDB:
3758 case ARM::t2STMIA_UPD:
3759 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003760 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003761 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003762 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003763
3764 if (UseCycle == -1)
3765 // Assume it's read in the first stage.
3766 UseCycle = 1;
3767
3768 UseCycle = DefCycle - UseCycle + 1;
3769 if (UseCycle > 0) {
3770 if (LdmBypass) {
3771 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3772 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003773 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003774 UseClass, UseIdx))
3775 --UseCycle;
3776 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003777 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003778 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003779 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003780 }
3781
3782 return UseCycle;
3783}
3784
Evan Cheng7fae11b2011-12-14 02:11:42 +00003785static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003786 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003787 unsigned &DefIdx, unsigned &Dist) {
3788 Dist = 0;
3789
3790 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003791 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003792 assert(II->isInsideBundle() && "Empty bundle?");
3793
3794 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003795 while (II->isInsideBundle()) {
3796 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3797 if (Idx != -1)
3798 break;
3799 --II;
3800 ++Dist;
3801 }
3802
3803 assert(Idx != -1 && "Cannot find bundled definition!");
3804 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003805 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003806}
3807
3808static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003809 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003810 unsigned &UseIdx, unsigned &Dist) {
3811 Dist = 0;
3812
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003813 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003814 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003815 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003816
3817 // FIXME: This doesn't properly handle multiple uses.
3818 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003819 while (II != E && II->isInsideBundle()) {
3820 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3821 if (Idx != -1)
3822 break;
3823 if (II->getOpcode() != ARM::t2IT)
3824 ++Dist;
3825 ++II;
3826 }
3827
Evan Chengda103bf2011-12-14 20:00:08 +00003828 if (Idx == -1) {
3829 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003830 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003831 }
3832
Evan Cheng7fae11b2011-12-14 02:11:42 +00003833 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003834 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003835}
3836
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003837/// Return the number of cycles to add to (or subtract from) the static
3838/// itinerary based on the def opcode and alignment. The caller will ensure that
3839/// adjusted latency is at least one cycle.
3840static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003841 const MachineInstr &DefMI,
3842 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003843 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003844 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003845 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3846 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003847 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003848 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003849 case ARM::LDRrs:
3850 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003851 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003852 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3853 if (ShImm == 0 ||
3854 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003855 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003856 break;
3857 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003858 case ARM::t2LDRs:
3859 case ARM::t2LDRBs:
3860 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003861 case ARM::t2LDRSHs: {
3862 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003863 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003864 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003865 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003866 break;
3867 }
3868 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003869 } else if (Subtarget.isSwift()) {
3870 // FIXME: Properly handle all of the latency adjustments for address
3871 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003872 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003873 default: break;
3874 case ARM::LDRrs:
3875 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003876 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003877 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3878 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3879 if (!isSub &&
3880 (ShImm == 0 ||
3881 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3882 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3883 Adjust -= 2;
3884 else if (!isSub &&
3885 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3886 --Adjust;
3887 break;
3888 }
3889 case ARM::t2LDRs:
3890 case ARM::t2LDRBs:
3891 case ARM::t2LDRHs:
3892 case ARM::t2LDRSHs: {
3893 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003894 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003895 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3896 Adjust -= 2;
3897 break;
3898 }
3899 }
Evan Chengff310732010-10-28 06:47:08 +00003900 }
3901
Diana Picus92423ce2016-06-27 09:08:23 +00003902 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003903 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003904 default: break;
3905 case ARM::VLD1q8:
3906 case ARM::VLD1q16:
3907 case ARM::VLD1q32:
3908 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003909 case ARM::VLD1q8wb_fixed:
3910 case ARM::VLD1q16wb_fixed:
3911 case ARM::VLD1q32wb_fixed:
3912 case ARM::VLD1q64wb_fixed:
3913 case ARM::VLD1q8wb_register:
3914 case ARM::VLD1q16wb_register:
3915 case ARM::VLD1q32wb_register:
3916 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003917 case ARM::VLD2d8:
3918 case ARM::VLD2d16:
3919 case ARM::VLD2d32:
3920 case ARM::VLD2q8:
3921 case ARM::VLD2q16:
3922 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003923 case ARM::VLD2d8wb_fixed:
3924 case ARM::VLD2d16wb_fixed:
3925 case ARM::VLD2d32wb_fixed:
3926 case ARM::VLD2q8wb_fixed:
3927 case ARM::VLD2q16wb_fixed:
3928 case ARM::VLD2q32wb_fixed:
3929 case ARM::VLD2d8wb_register:
3930 case ARM::VLD2d16wb_register:
3931 case ARM::VLD2d32wb_register:
3932 case ARM::VLD2q8wb_register:
3933 case ARM::VLD2q16wb_register:
3934 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003935 case ARM::VLD3d8:
3936 case ARM::VLD3d16:
3937 case ARM::VLD3d32:
3938 case ARM::VLD1d64T:
3939 case ARM::VLD3d8_UPD:
3940 case ARM::VLD3d16_UPD:
3941 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003942 case ARM::VLD1d64Twb_fixed:
3943 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003944 case ARM::VLD3q8_UPD:
3945 case ARM::VLD3q16_UPD:
3946 case ARM::VLD3q32_UPD:
3947 case ARM::VLD4d8:
3948 case ARM::VLD4d16:
3949 case ARM::VLD4d32:
3950 case ARM::VLD1d64Q:
3951 case ARM::VLD4d8_UPD:
3952 case ARM::VLD4d16_UPD:
3953 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003954 case ARM::VLD1d64Qwb_fixed:
3955 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003956 case ARM::VLD4q8_UPD:
3957 case ARM::VLD4q16_UPD:
3958 case ARM::VLD4q32_UPD:
3959 case ARM::VLD1DUPq8:
3960 case ARM::VLD1DUPq16:
3961 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003962 case ARM::VLD1DUPq8wb_fixed:
3963 case ARM::VLD1DUPq16wb_fixed:
3964 case ARM::VLD1DUPq32wb_fixed:
3965 case ARM::VLD1DUPq8wb_register:
3966 case ARM::VLD1DUPq16wb_register:
3967 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003968 case ARM::VLD2DUPd8:
3969 case ARM::VLD2DUPd16:
3970 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003971 case ARM::VLD2DUPd8wb_fixed:
3972 case ARM::VLD2DUPd16wb_fixed:
3973 case ARM::VLD2DUPd32wb_fixed:
3974 case ARM::VLD2DUPd8wb_register:
3975 case ARM::VLD2DUPd16wb_register:
3976 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003977 case ARM::VLD4DUPd8:
3978 case ARM::VLD4DUPd16:
3979 case ARM::VLD4DUPd32:
3980 case ARM::VLD4DUPd8_UPD:
3981 case ARM::VLD4DUPd16_UPD:
3982 case ARM::VLD4DUPd32_UPD:
3983 case ARM::VLD1LNd8:
3984 case ARM::VLD1LNd16:
3985 case ARM::VLD1LNd32:
3986 case ARM::VLD1LNd8_UPD:
3987 case ARM::VLD1LNd16_UPD:
3988 case ARM::VLD1LNd32_UPD:
3989 case ARM::VLD2LNd8:
3990 case ARM::VLD2LNd16:
3991 case ARM::VLD2LNd32:
3992 case ARM::VLD2LNq16:
3993 case ARM::VLD2LNq32:
3994 case ARM::VLD2LNd8_UPD:
3995 case ARM::VLD2LNd16_UPD:
3996 case ARM::VLD2LNd32_UPD:
3997 case ARM::VLD2LNq16_UPD:
3998 case ARM::VLD2LNq32_UPD:
3999 case ARM::VLD4LNd8:
4000 case ARM::VLD4LNd16:
4001 case ARM::VLD4LNd32:
4002 case ARM::VLD4LNq16:
4003 case ARM::VLD4LNq32:
4004 case ARM::VLD4LNd8_UPD:
4005 case ARM::VLD4LNd16_UPD:
4006 case ARM::VLD4LNd32_UPD:
4007 case ARM::VLD4LNq16_UPD:
4008 case ARM::VLD4LNq32_UPD:
4009 // If the address is not 64-bit aligned, the latencies of these
4010 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004011 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004012 break;
4013 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004014 }
4015 return Adjust;
4016}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004017
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004018int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4019 const MachineInstr &DefMI,
4020 unsigned DefIdx,
4021 const MachineInstr &UseMI,
4022 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004023 // No operand latency. The caller may fall back to getInstrLatency.
4024 if (!ItinData || ItinData->isEmpty())
4025 return -1;
4026
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004027 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004028 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004029
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004030 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004031 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004032 if (DefMI.isBundle())
4033 ResolvedDefMI =
4034 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
4035 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
4036 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004037 return 1;
4038 }
4039
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004040 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004041 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004042 if (UseMI.isBundle()) {
4043 ResolvedUseMI =
4044 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
4045 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00004046 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004047 }
4048
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004049 return getOperandLatencyImpl(
4050 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
4051 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
4052}
4053
4054int ARMBaseInstrInfo::getOperandLatencyImpl(
4055 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4056 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
4057 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
4058 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004059 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004060 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004061 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00004062 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004063 }
4064
4065 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004066 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004067 return 0;
4068
4069 // Otherwise it takes the instruction latency (generally one).
4070 unsigned Latency = getInstrLatency(ItinData, DefMI);
4071
4072 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4073 // its uses. Instructions which are otherwise scheduled between them may
4074 // incur a code size penalty (not able to use the CPSR setting 16-bit
4075 // instructions).
4076 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004077 const MachineFunction *MF = DefMI.getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00004078 // FIXME: Use Function::optForSize().
Matthias Braunf1caa282017-12-15 22:22:58 +00004079 if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004080 --Latency;
4081 }
4082 return Latency;
4083 }
4084
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004085 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00004086 return -1;
4087
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004088 unsigned DefAlign = DefMI.hasOneMemOperand()
4089 ? (*DefMI.memoperands_begin())->getAlignment()
4090 : 0;
4091 unsigned UseAlign = UseMI.hasOneMemOperand()
4092 ? (*UseMI.memoperands_begin())->getAlignment()
4093 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004094
4095 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004096 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
4097 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004098 // Unable to find operand latency. The caller may resort to getInstrLatency.
4099 if (Latency < 0)
4100 return Latency;
4101
4102 // Adjust for IT block position.
4103 int Adj = DefAdj + UseAdj;
4104
4105 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4106 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4107 if (Adj >= 0 || (int)Latency > -Adj) {
4108 return Latency + Adj;
4109 }
4110 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00004111 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004112}
4113
4114int
4115ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4116 SDNode *DefNode, unsigned DefIdx,
4117 SDNode *UseNode, unsigned UseIdx) const {
4118 if (!DefNode->isMachineOpcode())
4119 return 1;
4120
Evan Cheng6cc775f2011-06-28 19:10:37 +00004121 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00004122
Evan Cheng6cc775f2011-06-28 19:10:37 +00004123 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00004124 return 0;
4125
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004126 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004127 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004128
Evan Cheng6c1414f2010-10-29 18:09:28 +00004129 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00004130 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00004131 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4132 int Threshold = 1 + Adj;
4133 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00004134 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004135
Evan Cheng6cc775f2011-06-28 19:10:37 +00004136 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004137 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4138 unsigned DefAlign = !DefMN->memoperands_empty()
4139 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4140 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4141 unsigned UseAlign = !UseMN->memoperands_empty()
4142 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00004143 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4144 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00004145
4146 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00004147 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4148 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00004149 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4150 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00004151 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00004152 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004153 case ARM::LDRrs:
4154 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00004155 unsigned ShOpVal =
4156 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4157 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4158 if (ShImm == 0 ||
4159 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4160 --Latency;
4161 break;
4162 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004163 case ARM::t2LDRs:
4164 case ARM::t2LDRBs:
4165 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00004166 case ARM::t2LDRSHs: {
4167 // Thumb2 mode: lsl only.
4168 unsigned ShAmt =
4169 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4170 if (ShAmt == 0 || ShAmt == 2)
4171 --Latency;
4172 break;
4173 }
4174 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00004175 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4176 // FIXME: Properly handle all of the latency adjustments for address
4177 // writeback.
4178 switch (DefMCID.getOpcode()) {
4179 default: break;
4180 case ARM::LDRrs:
4181 case ARM::LDRBrs: {
4182 unsigned ShOpVal =
4183 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4184 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4185 if (ShImm == 0 ||
4186 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4187 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4188 Latency -= 2;
4189 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4190 --Latency;
4191 break;
4192 }
4193 case ARM::t2LDRs:
4194 case ARM::t2LDRBs:
4195 case ARM::t2LDRHs:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004196 case ARM::t2LDRSHs:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004197 // Thumb2 mode: lsl 0-3 only.
4198 Latency -= 2;
4199 break;
4200 }
Evan Chengff310732010-10-28 06:47:08 +00004201 }
4202
Diana Picus92423ce2016-06-27 09:08:23 +00004203 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004204 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004205 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004206 case ARM::VLD1q8:
4207 case ARM::VLD1q16:
4208 case ARM::VLD1q32:
4209 case ARM::VLD1q64:
4210 case ARM::VLD1q8wb_register:
4211 case ARM::VLD1q16wb_register:
4212 case ARM::VLD1q32wb_register:
4213 case ARM::VLD1q64wb_register:
4214 case ARM::VLD1q8wb_fixed:
4215 case ARM::VLD1q16wb_fixed:
4216 case ARM::VLD1q32wb_fixed:
4217 case ARM::VLD1q64wb_fixed:
4218 case ARM::VLD2d8:
4219 case ARM::VLD2d16:
4220 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004221 case ARM::VLD2q8Pseudo:
4222 case ARM::VLD2q16Pseudo:
4223 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004224 case ARM::VLD2d8wb_fixed:
4225 case ARM::VLD2d16wb_fixed:
4226 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00004227 case ARM::VLD2q8PseudoWB_fixed:
4228 case ARM::VLD2q16PseudoWB_fixed:
4229 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004230 case ARM::VLD2d8wb_register:
4231 case ARM::VLD2d16wb_register:
4232 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00004233 case ARM::VLD2q8PseudoWB_register:
4234 case ARM::VLD2q16PseudoWB_register:
4235 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004236 case ARM::VLD3d8Pseudo:
4237 case ARM::VLD3d16Pseudo:
4238 case ARM::VLD3d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004239 case ARM::VLD1d8TPseudo:
4240 case ARM::VLD1d16TPseudo:
4241 case ARM::VLD1d32TPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004242 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004243 case ARM::VLD1d64TPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004244 case ARM::VLD1d64TPseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004245 case ARM::VLD3d8Pseudo_UPD:
4246 case ARM::VLD3d16Pseudo_UPD:
4247 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004248 case ARM::VLD3q8Pseudo_UPD:
4249 case ARM::VLD3q16Pseudo_UPD:
4250 case ARM::VLD3q32Pseudo_UPD:
4251 case ARM::VLD3q8oddPseudo:
4252 case ARM::VLD3q16oddPseudo:
4253 case ARM::VLD3q32oddPseudo:
4254 case ARM::VLD3q8oddPseudo_UPD:
4255 case ARM::VLD3q16oddPseudo_UPD:
4256 case ARM::VLD3q32oddPseudo_UPD:
4257 case ARM::VLD4d8Pseudo:
4258 case ARM::VLD4d16Pseudo:
4259 case ARM::VLD4d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004260 case ARM::VLD1d8QPseudo:
4261 case ARM::VLD1d16QPseudo:
4262 case ARM::VLD1d32QPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004263 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004264 case ARM::VLD1d64QPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004265 case ARM::VLD1d64QPseudoWB_register:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004266 case ARM::VLD1q8HighQPseudo:
4267 case ARM::VLD1q8LowQPseudo_UPD:
4268 case ARM::VLD1q8HighTPseudo:
4269 case ARM::VLD1q8LowTPseudo_UPD:
4270 case ARM::VLD1q16HighQPseudo:
4271 case ARM::VLD1q16LowQPseudo_UPD:
4272 case ARM::VLD1q16HighTPseudo:
4273 case ARM::VLD1q16LowTPseudo_UPD:
4274 case ARM::VLD1q32HighQPseudo:
4275 case ARM::VLD1q32LowQPseudo_UPD:
4276 case ARM::VLD1q32HighTPseudo:
4277 case ARM::VLD1q32LowTPseudo_UPD:
4278 case ARM::VLD1q64HighQPseudo:
4279 case ARM::VLD1q64LowQPseudo_UPD:
4280 case ARM::VLD1q64HighTPseudo:
4281 case ARM::VLD1q64LowTPseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004282 case ARM::VLD4d8Pseudo_UPD:
4283 case ARM::VLD4d16Pseudo_UPD:
4284 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004285 case ARM::VLD4q8Pseudo_UPD:
4286 case ARM::VLD4q16Pseudo_UPD:
4287 case ARM::VLD4q32Pseudo_UPD:
4288 case ARM::VLD4q8oddPseudo:
4289 case ARM::VLD4q16oddPseudo:
4290 case ARM::VLD4q32oddPseudo:
4291 case ARM::VLD4q8oddPseudo_UPD:
4292 case ARM::VLD4q16oddPseudo_UPD:
4293 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00004294 case ARM::VLD1DUPq8:
4295 case ARM::VLD1DUPq16:
4296 case ARM::VLD1DUPq32:
4297 case ARM::VLD1DUPq8wb_fixed:
4298 case ARM::VLD1DUPq16wb_fixed:
4299 case ARM::VLD1DUPq32wb_fixed:
4300 case ARM::VLD1DUPq8wb_register:
4301 case ARM::VLD1DUPq16wb_register:
4302 case ARM::VLD1DUPq32wb_register:
4303 case ARM::VLD2DUPd8:
4304 case ARM::VLD2DUPd16:
4305 case ARM::VLD2DUPd32:
4306 case ARM::VLD2DUPd8wb_fixed:
4307 case ARM::VLD2DUPd16wb_fixed:
4308 case ARM::VLD2DUPd32wb_fixed:
4309 case ARM::VLD2DUPd8wb_register:
4310 case ARM::VLD2DUPd16wb_register:
4311 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004312 case ARM::VLD4DUPd8Pseudo:
4313 case ARM::VLD4DUPd16Pseudo:
4314 case ARM::VLD4DUPd32Pseudo:
4315 case ARM::VLD4DUPd8Pseudo_UPD:
4316 case ARM::VLD4DUPd16Pseudo_UPD:
4317 case ARM::VLD4DUPd32Pseudo_UPD:
4318 case ARM::VLD1LNq8Pseudo:
4319 case ARM::VLD1LNq16Pseudo:
4320 case ARM::VLD1LNq32Pseudo:
4321 case ARM::VLD1LNq8Pseudo_UPD:
4322 case ARM::VLD1LNq16Pseudo_UPD:
4323 case ARM::VLD1LNq32Pseudo_UPD:
4324 case ARM::VLD2LNd8Pseudo:
4325 case ARM::VLD2LNd16Pseudo:
4326 case ARM::VLD2LNd32Pseudo:
4327 case ARM::VLD2LNq16Pseudo:
4328 case ARM::VLD2LNq32Pseudo:
4329 case ARM::VLD2LNd8Pseudo_UPD:
4330 case ARM::VLD2LNd16Pseudo_UPD:
4331 case ARM::VLD2LNd32Pseudo_UPD:
4332 case ARM::VLD2LNq16Pseudo_UPD:
4333 case ARM::VLD2LNq32Pseudo_UPD:
4334 case ARM::VLD4LNd8Pseudo:
4335 case ARM::VLD4LNd16Pseudo:
4336 case ARM::VLD4LNd32Pseudo:
4337 case ARM::VLD4LNq16Pseudo:
4338 case ARM::VLD4LNq32Pseudo:
4339 case ARM::VLD4LNd8Pseudo_UPD:
4340 case ARM::VLD4LNd16Pseudo_UPD:
4341 case ARM::VLD4LNd32Pseudo_UPD:
4342 case ARM::VLD4LNq16Pseudo_UPD:
4343 case ARM::VLD4LNq32Pseudo_UPD:
4344 // If the address is not 64-bit aligned, the latencies of these
4345 // instructions increases by one.
4346 ++Latency;
4347 break;
4348 }
4349
Evan Chengff310732010-10-28 06:47:08 +00004350 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004351}
Evan Cheng63c76082010-10-19 18:58:51 +00004352
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004353unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4354 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4355 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004356 return 0;
4357
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004358 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004359 return 0;
4360
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004361 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004362
Javed Absar4ae7e8122017-06-02 08:53:19 +00004363 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4364 !Subtarget.cheapPredicableCPSRDef())) {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004365 // When predicated, CPSR is an additional source operand for CPSR updating
4366 // instructions, this apparently increases their latencies.
4367 return 1;
4368 }
4369 return 0;
4370}
4371
Andrew Trick45446062012-06-05 21:11:27 +00004372unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004373 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004374 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004375 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4376 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004377 return 1;
4378
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004379 // An instruction scheduler typically runs on unbundled instructions, however
4380 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004381 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004382 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004383 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4384 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004385 while (++I != E && I->isInsideBundle()) {
4386 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004387 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004388 }
4389 return Latency;
4390 }
4391
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004392 const MCInstrDesc &MCID = MI.getDesc();
Javed Absar4ae7e8122017-06-02 08:53:19 +00004393 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4394 !Subtarget.cheapPredicableCPSRDef()))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004395 // When predicated, CPSR is an additional source operand for CPSR updating
4396 // instructions, this apparently increases their latencies.
4397 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004398 }
4399 // Be sure to call getStageLatency for an empty itinerary in case it has a
4400 // valid MinLatency property.
4401 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004402 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004403
4404 unsigned Class = MCID.getSchedClass();
4405
4406 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004407 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004408 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004409
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004410 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004411 unsigned Latency = ItinData->getStageLatency(Class);
4412
4413 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004414 unsigned DefAlign =
4415 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4416 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004417 if (Adj >= 0 || (int)Latency > -Adj) {
4418 return Latency + Adj;
4419 }
4420 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004421}
4422
4423int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4424 SDNode *Node) const {
4425 if (!Node->isMachineOpcode())
4426 return 1;
4427
4428 if (!ItinData || ItinData->isEmpty())
4429 return 1;
4430
4431 unsigned Opcode = Node->getMachineOpcode();
4432 switch (Opcode) {
4433 default:
4434 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004435 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004436 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004437 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004438 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004439}
4440
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004441bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4442 const MachineRegisterInfo *MRI,
4443 const MachineInstr &DefMI,
4444 unsigned DefIdx,
4445 const MachineInstr &UseMI,
4446 unsigned UseIdx) const {
4447 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4448 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004449 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004450 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004451 return true;
4452
4453 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004454 unsigned Latency =
4455 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004456 if (Latency <= 3)
4457 return false;
4458 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4459 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4460}
Evan Chenge96b8d72010-10-26 02:08:50 +00004461
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004462bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4463 const MachineInstr &DefMI,
4464 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004465 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004466 if (!ItinData || ItinData->isEmpty())
4467 return false;
4468
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004469 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004470 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004471 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004472 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4473 return (DefCycle != -1 && DefCycle <= 2);
4474 }
4475 return false;
4476}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004477
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004478bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004479 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004480 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004481 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4482 return false;
4483 }
4484 return true;
4485}
4486
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004487// LoadStackGuard has so far only been implemented for MachO. Different code
4488// sequence is needed for other targets.
4489void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4490 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004491 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004492 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4493 "ROPI/RWPI not currently supported with stack guard");
4494
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004495 MachineBasicBlock &MBB = *MI->getParent();
4496 DebugLoc DL = MI->getDebugLoc();
4497 unsigned Reg = MI->getOperand(0).getReg();
4498 const GlobalValue *GV =
4499 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4500 MachineInstrBuilder MIB;
4501
4502 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4503 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4504
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004505 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004506 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4507 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004508 auto Flags = MachineMemOperand::MOLoad |
4509 MachineMemOperand::MODereferenceable |
4510 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004511 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004512 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004513 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004514 }
4515
4516 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004517 MIB.addReg(Reg, RegState::Kill)
4518 .addImm(0)
4519 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end())
4520 .add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004521}
4522
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004523bool
4524ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4525 unsigned &AddSubOpc,
4526 bool &NegAcc, bool &HasLane) const {
4527 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4528 if (I == MLxEntryMap.end())
4529 return false;
4530
4531 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4532 MulOpc = Entry.MulOpc;
4533 AddSubOpc = Entry.AddSubOpc;
4534 NegAcc = Entry.NegAcc;
4535 HasLane = Entry.HasLane;
4536 return true;
4537}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004538
4539//===----------------------------------------------------------------------===//
4540// Execution domains.
4541//===----------------------------------------------------------------------===//
4542//
4543// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4544// and some can go down both. The vmov instructions go down the VFP pipeline,
4545// but they can be changed to vorr equivalents that are executed by the NEON
4546// pipeline.
4547//
4548// We use the following execution domain numbering:
4549//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004550enum ARMExeDomain {
4551 ExeGeneric = 0,
4552 ExeVFP = 1,
4553 ExeNEON = 2
4554};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004555
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004556//
4557// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4558//
4559std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004560ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004561 // If we don't have access to NEON instructions then we won't be able
4562 // to swizzle anything to the NEON domain. Check to make sure.
4563 if (Subtarget.hasNEON()) {
4564 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4565 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004566 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004567 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004568
Eric Christopher7e70aba2015-03-07 00:12:22 +00004569 // CortexA9 is particularly picky about mixing the two and wants these
4570 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004571 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4572 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4573 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004574 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4575 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004576 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004577 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004578
4579 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004580 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004581
4582 // Certain instructions can go either way on Cortex-A8.
4583 // Treat them as NEON instructions.
4584 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004585 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004586
4587 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004588 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004589
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004590 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004591}
4592
Tim Northover771f1602012-08-29 16:36:07 +00004593static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4594 unsigned SReg, unsigned &Lane) {
4595 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4596 Lane = 0;
4597
4598 if (DReg != ARM::NoRegister)
4599 return DReg;
4600
4601 Lane = 1;
4602 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4603
4604 assert(DReg && "S-register with no D super-register?");
4605 return DReg;
4606}
4607
Andrew Trickd9296ec2012-10-10 05:43:01 +00004608/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004609/// set ImplicitSReg to a register number that must be marked as implicit-use or
4610/// zero if no register needs to be defined as implicit-use.
4611///
4612/// If the function cannot determine if an SPR should be marked implicit use or
4613/// not, it returns false.
4614///
4615/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004616/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004617/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4618/// lane of the DPR).
4619///
4620/// If the other SPR is defined, an implicit-use of it should be added. Else,
4621/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004622///
James Molloyea052562012-09-18 08:31:15 +00004623static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004624 MachineInstr &MI, unsigned DReg,
4625 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004626 // If the DPR is defined or used already, the other SPR lane will be chained
4627 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004628 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004629 ImplicitSReg = 0;
4630 return true;
4631 }
4632
4633 // Otherwise we need to go searching to see if the SPR is set explicitly.
4634 ImplicitSReg = TRI->getSubReg(DReg,
4635 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4636 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004637 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004638
4639 if (LQR == MachineBasicBlock::LQR_Live)
4640 return true;
4641 else if (LQR == MachineBasicBlock::LQR_Unknown)
4642 return false;
4643
4644 // If the register is known not to be live, there is no need to add an
4645 // implicit-use.
4646 ImplicitSReg = 0;
4647 return true;
4648}
Tim Northover771f1602012-08-29 16:36:07 +00004649
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004650void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4651 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004652 unsigned DstReg, SrcReg, DReg;
4653 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004654 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004655 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004656 switch (MI.getOpcode()) {
4657 default:
4658 llvm_unreachable("cannot handle opcode!");
4659 break;
4660 case ARM::VMOVD:
4661 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004662 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004663
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004664 // Zap the predicate operands.
4665 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004666
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004667 // Make sure we've got NEON instructions.
4668 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004669
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004670 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4671 DstReg = MI.getOperand(0).getReg();
4672 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004673
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004674 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4675 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004676
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004677 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4678 MI.setDesc(get(ARM::VORRd));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004679 MIB.addReg(DstReg, RegState::Define)
4680 .addReg(SrcReg)
4681 .addReg(SrcReg)
4682 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004683 break;
4684 case ARM::VMOVRS:
4685 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004686 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004687 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004688
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004689 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4690 DstReg = MI.getOperand(0).getReg();
4691 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004692
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004693 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4694 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004695
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004696 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004697
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004698 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4699 // Note that DSrc has been widened and the other lane may be undef, which
4700 // contaminates the entire register.
4701 MI.setDesc(get(ARM::VGETLNi32));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004702 MIB.addReg(DstReg, RegState::Define)
4703 .addReg(DReg, RegState::Undef)
4704 .addImm(Lane)
4705 .add(predOps(ARMCC::AL));
Tim Northoverf6618152012-08-17 11:32:52 +00004706
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004707 // The old source should be an implicit use, otherwise we might think it
4708 // was dead before here.
4709 MIB.addReg(SrcReg, RegState::Implicit);
4710 break;
4711 case ARM::VMOVSR: {
4712 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004713 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004714 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004715
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004716 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4717 DstReg = MI.getOperand(0).getReg();
4718 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004719
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004720 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004721
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004722 unsigned ImplicitSReg;
4723 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004724 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004725
4726 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4727 MI.RemoveOperand(i - 1);
4728
4729 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4730 // Again DDst may be undefined at the beginning of this instruction.
4731 MI.setDesc(get(ARM::VSETLNi32));
4732 MIB.addReg(DReg, RegState::Define)
4733 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4734 .addReg(SrcReg)
Diana Picus4f8c3e12017-01-13 09:37:56 +00004735 .addImm(Lane)
4736 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004737
4738 // The narrower destination must be marked as set to keep previous chains
4739 // in place.
4740 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4741 if (ImplicitSReg != 0)
4742 MIB.addReg(ImplicitSReg, RegState::Implicit);
4743 break;
James Molloyea052562012-09-18 08:31:15 +00004744 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004745 case ARM::VMOVS: {
4746 if (Domain != ExeNEON)
4747 break;
4748
4749 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004750 DstReg = MI.getOperand(0).getReg();
4751 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004752
Tim Northoverca9f3842012-08-30 10:17:45 +00004753 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4754 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4755 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4756
James Molloyea052562012-09-18 08:31:15 +00004757 unsigned ImplicitSReg;
4758 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4759 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004760
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004761 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4762 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004763
Tim Northoverca9f3842012-08-30 10:17:45 +00004764 if (DSrc == DDst) {
4765 // Destination can be:
4766 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004767 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004768 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004769 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
Diana Picus4f8c3e12017-01-13 09:37:56 +00004770 .addImm(SrcLane)
4771 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004772
4773 // Neither the source or the destination are naturally represented any
4774 // more, so add them in manually.
4775 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4776 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004777 if (ImplicitSReg != 0)
4778 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004779 break;
4780 }
4781
4782 // In general there's no single instruction that can perform an S <-> S
4783 // move in NEON space, but a pair of VEXT instructions *can* do the
4784 // job. It turns out that the VEXTs needed will only use DSrc once, with
4785 // the position based purely on the combination of lane-0 and lane-1
4786 // involved. For example
4787 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4788 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4789 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4790 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4791 //
4792 // Pattern of the MachineInstrs is:
4793 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4794 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004795 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4796 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004797
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004798 // On the first instruction, both DSrc and DDst may be undef if present.
Tim Northover726d32c2012-09-01 18:07:29 +00004799 // Specifically when the original instruction didn't have them as an
4800 // <imp-use>.
4801 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004802 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004803 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4804
4805 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004806 CurUndef = !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004807 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4808 .addImm(1)
4809 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004810
4811 if (SrcLane == DstLane)
4812 NewMIB.addReg(SrcReg, RegState::Implicit);
4813
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004814 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004815 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004816
4817 // On the second instruction, DDst has definitely been defined above, so
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004818 // it is not undef. DSrc, if present, can be undef as above.
Tim Northover726d32c2012-09-01 18:07:29 +00004819 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004820 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004821 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4822
4823 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004824 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004825 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4826 .addImm(1)
4827 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004828
4829 if (SrcLane != DstLane)
4830 MIB.addReg(SrcReg, RegState::Implicit);
4831
4832 // As before, the original destination is no longer represented, add it
4833 // implicitly.
4834 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004835 if (ImplicitSReg != 0)
4836 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004837 break;
4838 }
Tim Northoverf6618152012-08-17 11:32:52 +00004839 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004840}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004841
Bob Wilsone8a549c2012-09-29 21:43:49 +00004842//===----------------------------------------------------------------------===//
4843// Partial register updates
4844//===----------------------------------------------------------------------===//
4845//
4846// Swift renames NEON registers with 64-bit granularity. That means any
4847// instruction writing an S-reg implicitly reads the containing D-reg. The
4848// problem is mostly avoided by translating f32 operations to v2f32 operations
4849// on D-registers, but f32 loads are still a problem.
4850//
4851// These instructions can load an f32 into a NEON register:
4852//
4853// VLDRS - Only writes S, partial D update.
4854// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4855// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4856//
4857// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004858unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4859 const MachineInstr &MI, unsigned OpNum,
4860 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004861 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4862 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004863 return 0;
4864
4865 assert(TRI && "Need TRI instance");
4866
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004867 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004868 if (MO.readsReg())
4869 return 0;
4870 unsigned Reg = MO.getReg();
4871 int UseOp = -1;
4872
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004873 switch (MI.getOpcode()) {
4874 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004875 case ARM::VLDRS:
4876 case ARM::FCONSTS:
4877 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004878 case ARM::VMOVv8i8:
4879 case ARM::VMOVv4i16:
4880 case ARM::VMOVv2i32:
4881 case ARM::VMOVv2f32:
4882 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004883 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004884 break;
4885
4886 // Explicitly reads the dependency.
4887 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004888 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004889 break;
4890 default:
4891 return 0;
4892 }
4893
4894 // If this instruction actually reads a value from Reg, there is no unwanted
4895 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004896 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00004897 return 0;
4898
4899 // We must be able to clobber the whole D-reg.
4900 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004901 // Virtual register must be a def undef foo:ssub_0 operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004902 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004903 return 0;
4904 } else if (ARM::SPRRegClass.contains(Reg)) {
4905 // Physical register: MI must define the full D-reg.
4906 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4907 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004908 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004909 return 0;
4910 }
4911
4912 // MI has an unwanted D-register dependency.
4913 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00004914 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004915}
4916
4917// Break a partial register dependency after getPartialRegUpdateClearance
4918// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004919void ARMBaseInstrInfo::breakPartialRegDependency(
4920 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4921 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004922 assert(TRI && "Need TRI instance");
4923
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004924 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004925 unsigned Reg = MO.getReg();
4926 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4927 "Can't break virtual register dependencies.");
4928 unsigned DReg = Reg;
4929
4930 // If MI defines an S-reg, find the corresponding D super-register.
4931 if (ARM::SPRRegClass.contains(Reg)) {
4932 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4933 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4934 }
4935
4936 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004937 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004938
4939 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4940 // the full D-register by loading the same value to both lanes. The
4941 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004942 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004943 // too big regressions.
4944
4945 // Insert the dependency-breaking FCONSTD before MI.
4946 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Diana Picus4f8c3e12017-01-13 09:37:56 +00004947 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4948 .addImm(96)
4949 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004950 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004951}
4952
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004953bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004954 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004955}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004956
4957bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004958 if (MI->getNumOperands() < 4)
4959 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004960 unsigned ShOpVal = MI->getOperand(3).getImm();
4961 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4962 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4963 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4964 ((ShImm == 1 || ShImm == 2) &&
4965 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4966 return true;
4967
4968 return false;
4969}
Quentin Colombetd358e842014-08-22 18:05:22 +00004970
4971bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4972 const MachineInstr &MI, unsigned DefIdx,
4973 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4974 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4975 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4976
4977 switch (MI.getOpcode()) {
4978 case ARM::VMOVDRR:
4979 // dX = VMOVDRR rY, rZ
4980 // is the same as:
4981 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4982 // Populate the InputRegs accordingly.
4983 // rY
4984 const MachineOperand *MOReg = &MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00004985 if (!MOReg->isUndef())
4986 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
4987 MOReg->getSubReg(), ARM::ssub_0));
Quentin Colombetd358e842014-08-22 18:05:22 +00004988 // rZ
4989 MOReg = &MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00004990 if (!MOReg->isUndef())
4991 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
4992 MOReg->getSubReg(), ARM::ssub_1));
Quentin Colombetd358e842014-08-22 18:05:22 +00004993 return true;
4994 }
4995 llvm_unreachable("Target dependent opcode missing");
4996}
4997
4998bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4999 const MachineInstr &MI, unsigned DefIdx,
5000 RegSubRegPairAndIdx &InputReg) const {
5001 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5002 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
5003
5004 switch (MI.getOpcode()) {
5005 case ARM::VMOVRRD:
5006 // rX, rY = VMOVRRD dZ
5007 // is the same as:
5008 // rX = EXTRACT_SUBREG dZ, ssub_0
5009 // rY = EXTRACT_SUBREG dZ, ssub_1
5010 const MachineOperand &MOReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005011 if (MOReg.isUndef())
5012 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005013 InputReg.Reg = MOReg.getReg();
5014 InputReg.SubReg = MOReg.getSubReg();
5015 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
5016 return true;
5017 }
5018 llvm_unreachable("Target dependent opcode missing");
5019}
5020
5021bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5022 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
5023 RegSubRegPairAndIdx &InsertedReg) const {
5024 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5025 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
5026
5027 switch (MI.getOpcode()) {
5028 case ARM::VSETLNi32:
5029 // dX = VSETLNi32 dY, rZ, imm
5030 const MachineOperand &MOBaseReg = MI.getOperand(1);
5031 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005032 if (MOInsertedReg.isUndef())
5033 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005034 const MachineOperand &MOIndex = MI.getOperand(3);
5035 BaseReg.Reg = MOBaseReg.getReg();
5036 BaseReg.SubReg = MOBaseReg.getSubReg();
5037
5038 InsertedReg.Reg = MOInsertedReg.getReg();
5039 InsertedReg.SubReg = MOInsertedReg.getSubReg();
5040 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
5041 return true;
5042 }
5043 llvm_unreachable("Target dependent opcode missing");
5044}