blob: eaa899576b7bf52920fa6297d043957d5d46d4e9 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David Goodwinaf7451b2009-07-08 16:09:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the Base ARM implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
Amara Emerson52cfb6a2013-10-03 09:31:51 +000013#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000014#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000015#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000016#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000017#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000018#include "ARMMachineFunctionInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000019#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000020#include "MCTargetDesc/ARMAddressingModes.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000021#include "MCTargetDesc/ARMBaseInfo.h"
22#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000024#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000026#include "llvm/ADT/Triple.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000027#include "llvm/CodeGen/LiveVariables.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000029#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstr.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000035#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000037#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Chenga20cde32011-07-20 23:34:39 +000038#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000039#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun88e21312015-06-13 03:42:11 +000041#include "llvm/CodeGen/TargetSchedule.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000042#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Constants.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000044#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Function.h"
46#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000047#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000048#include "llvm/MC/MCInstrDesc.h"
49#include "llvm/MC/MCInstrItineraries.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000050#include "llvm/Support/BranchProbability.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000051#include "llvm/Support/Casting.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000052#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000053#include "llvm/Support/Compiler.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000054#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000055#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000056#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000057#include "llvm/Target/TargetMachine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000058#include <algorithm>
59#include <cassert>
60#include <cstdint>
61#include <iterator>
62#include <new>
63#include <utility>
64#include <vector>
Evan Cheng1e210d02011-06-28 20:07:07 +000065
David Goodwinaf7451b2009-07-08 16:09:28 +000066using namespace llvm;
67
Chandler Carruthe96dd892014-04-21 22:55:11 +000068#define DEBUG_TYPE "arm-instrinfo"
69
Chandler Carruthd174b722014-04-22 02:03:14 +000070#define GET_INSTRINFO_CTOR_DTOR
71#include "ARMGenInstrInfo.inc"
72
David Goodwinaf7451b2009-07-08 16:09:28 +000073static cl::opt<bool>
74EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
75 cl::desc("Enable ARM 2-addr to 3-addr conv"));
76
Evan Cheng62c7b5b2010-12-05 22:04:16 +000077/// ARM_MLxEntry - Record information about MLA / MLS instructions.
78struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000079 uint16_t MLxOpc; // MLA / MLS opcode
80 uint16_t MulOpc; // Expanded multiplication opcode
81 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000082 bool NegAcc; // True if the acc is negated before the add / sub.
83 bool HasLane; // True if instruction has an extra "lane" operand.
84};
85
86static const ARM_MLxEntry ARM_MLxTable[] = {
87 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
88 // fp scalar ops
89 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
90 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
91 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
92 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000093 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
94 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
95 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
96 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
97
98 // fp SIMD ops
99 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
100 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
101 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
102 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
103 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
104 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
105 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
106 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
107};
108
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000109ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +0000110 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000111 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000112 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
113 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000114 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000115 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
116 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
117 }
118}
119
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000120// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
121// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000122ScheduleHazardRecognizer *
123ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
124 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000125 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000126 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000127 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000128 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
129 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000130 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000131}
132
133ScheduleHazardRecognizer *ARMBaseInstrInfo::
134CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
135 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000136 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000137 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000138 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000139}
140
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000141MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
142 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000143 // FIXME: Thumb2 support.
144
David Goodwinaf7451b2009-07-08 16:09:28 +0000145 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000146 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000147
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 MachineFunction &MF = *MI.getParent()->getParent();
149 uint64_t TSFlags = MI.getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000150 bool isPre = false;
151 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000153 case ARMII::IndexModePre:
154 isPre = true;
155 break;
156 case ARMII::IndexModePost:
157 break;
158 }
159
160 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
161 // operation.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000162 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
David Goodwinaf7451b2009-07-08 16:09:28 +0000163 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000164 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000165
Craig Topper062a2ba2014-04-25 05:30:21 +0000166 MachineInstr *UpdateMI = nullptr;
167 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000168 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000170 unsigned NumOps = MCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000171 bool isLoad = !MI.mayStore();
172 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
173 const MachineOperand &Base = MI.getOperand(2);
174 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
David Goodwinaf7451b2009-07-08 16:09:28 +0000175 unsigned WBReg = WB.getReg();
176 unsigned BaseReg = Base.getReg();
177 unsigned OffReg = Offset.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000178 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
179 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
David Goodwinaf7451b2009-07-08 16:09:28 +0000180 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000181 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000182 case ARMII::AddrMode2: {
183 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
184 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
185 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000186 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000187 // Can't encode it in a so_imm operand. This transformation will
188 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000189 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000190 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000191 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000192 .addReg(BaseReg)
193 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000194 .add(predOps(Pred))
195 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000196 } else if (Amt != 0) {
197 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
198 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000199 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000200 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000201 .addReg(BaseReg)
202 .addReg(OffReg)
203 .addReg(0)
204 .addImm(SOOpc)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000205 .add(predOps(Pred))
206 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000207 } else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000208 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000209 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000210 .addReg(BaseReg)
211 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000212 .add(predOps(Pred))
213 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000214 break;
215 }
216 case ARMII::AddrMode3 : {
217 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
218 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
219 if (OffReg == 0)
220 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000221 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000222 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 .addReg(BaseReg)
224 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000225 .add(predOps(Pred))
226 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000227 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000228 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000229 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230 .addReg(BaseReg)
231 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000232 .add(predOps(Pred))
233 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000234 break;
235 }
236 }
237
238 std::vector<MachineInstr*> NewMIs;
239 if (isPre) {
240 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000241 MemMI =
242 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
243 .addReg(WBReg)
244 .addImm(0)
245 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000246 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000247 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
248 .addReg(MI.getOperand(1).getReg())
249 .addReg(WBReg)
250 .addReg(0)
251 .addImm(0)
252 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000253 NewMIs.push_back(MemMI);
254 NewMIs.push_back(UpdateMI);
255 } else {
256 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000257 MemMI =
258 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
259 .addReg(BaseReg)
260 .addImm(0)
261 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000262 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000263 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
264 .addReg(MI.getOperand(1).getReg())
265 .addReg(BaseReg)
266 .addReg(0)
267 .addImm(0)
268 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000269 if (WB.isDead())
270 UpdateMI->getOperand(0).setIsDead();
271 NewMIs.push_back(UpdateMI);
272 NewMIs.push_back(MemMI);
273 }
274
275 // Transfer LiveVariables states, kill / dead info.
276 if (LV) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000277 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
278 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000279 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000280 unsigned Reg = MO.getReg();
281
282 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
283 if (MO.isDef()) {
284 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
285 if (MO.isDead())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000286 LV->addVirtualRegisterDead(Reg, *NewMI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000287 }
288 if (MO.isUse() && MO.isKill()) {
289 for (unsigned j = 0; j < 2; ++j) {
290 // Look at the two new MI's in reverse order.
291 MachineInstr *NewMI = NewMIs[j];
292 if (!NewMI->readsRegister(Reg))
293 continue;
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000294 LV->addVirtualRegisterKilled(Reg, *NewMI);
295 if (VI.removeKill(MI))
David Goodwinaf7451b2009-07-08 16:09:28 +0000296 VI.Kills.push_back(NewMI);
297 break;
298 }
299 }
300 }
301 }
302 }
303
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000304 MachineBasicBlock::iterator MBBI = MI.getIterator();
David Goodwinaf7451b2009-07-08 16:09:28 +0000305 MFI->insert(MBBI, NewMIs[1]);
306 MFI->insert(MBBI, NewMIs[0]);
307 return NewMIs[0];
308}
309
310// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000311bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
312 MachineBasicBlock *&TBB,
313 MachineBasicBlock *&FBB,
314 SmallVectorImpl<MachineOperand> &Cond,
315 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000316 TBB = nullptr;
317 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000318
David Goodwinaf7451b2009-07-08 16:09:28 +0000319 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000320 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000321 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000322 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000323
324 // Walk backwards from the end of the basic block until the branch is
325 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000326 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000327 // Flag to be raised on unanalyzeable instructions. This is useful in cases
328 // where we want to clean up on the end of the basic block before we bail
329 // out.
330 bool CantAnalyze = false;
331
332 // Skip over DEBUG values and predicated nonterminators.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000333 while (I->isDebugInstr() || !I->isTerminator()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000334 if (I == MBB.begin())
335 return false;
336 --I;
337 }
338
339 if (isIndirectBranchOpcode(I->getOpcode()) ||
340 isJumpTableBranchOpcode(I->getOpcode())) {
341 // Indirect branches and jump tables can't be analyzed, but we still want
342 // to clean up any instructions at the tail of the basic block.
343 CantAnalyze = true;
344 } else if (isUncondBranchOpcode(I->getOpcode())) {
345 TBB = I->getOperand(0).getMBB();
346 } else if (isCondBranchOpcode(I->getOpcode())) {
347 // Bail out if we encounter multiple conditional branches.
348 if (!Cond.empty())
349 return true;
350
351 assert(!FBB && "FBB should have been null.");
352 FBB = TBB;
353 TBB = I->getOperand(0).getMBB();
354 Cond.push_back(I->getOperand(1));
355 Cond.push_back(I->getOperand(2));
356 } else if (I->isReturn()) {
357 // Returns can't be analyzed, but we should run cleanup.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000358 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000359 } else {
360 // We encountered other unrecognized terminator. Bail out immediately.
361 return true;
362 }
363
364 // Cleanup code - to be run for unpredicated unconditional branches and
365 // returns.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000366 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000367 (isUncondBranchOpcode(I->getOpcode()) ||
368 isIndirectBranchOpcode(I->getOpcode()) ||
369 isJumpTableBranchOpcode(I->getOpcode()) ||
370 I->isReturn())) {
371 // Forget any previous condition branch information - it no longer applies.
372 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000373 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000374
375 // If we can modify the function, delete everything below this
376 // unconditional branch.
377 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000378 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000379 while (DI != MBB.end()) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000380 MachineInstr &InstToDelete = *DI;
Lang Hames24864fe2013-07-19 23:52:47 +0000381 ++DI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000382 InstToDelete.eraseFromParent();
Lang Hames24864fe2013-07-19 23:52:47 +0000383 }
384 }
385 }
386
387 if (CantAnalyze)
388 return true;
389
Dale Johannesen4244d122010-04-02 01:38:09 +0000390 if (I == MBB.begin())
391 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000392
Dale Johannesen4244d122010-04-02 01:38:09 +0000393 --I;
394 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000395
Lang Hames24864fe2013-07-19 23:52:47 +0000396 // We made it past the terminators without bailing out - we must have
397 // analyzed this branch successfully.
398 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000399}
400
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000401unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000402 int *BytesRemoved) const {
403 assert(!BytesRemoved && "code size not handled");
404
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000405 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
406 if (I == MBB.end())
407 return 0;
408
Evan Cheng056c6692009-07-27 18:20:05 +0000409 if (!isUncondBranchOpcode(I->getOpcode()) &&
410 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000411 return 0;
412
413 // Remove the branch.
414 I->eraseFromParent();
415
416 I = MBB.end();
417
418 if (I == MBB.begin()) return 1;
419 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000420 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000421 return 1;
422
423 // Remove the branch.
424 I->eraseFromParent();
425 return 2;
426}
427
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000428unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000429 MachineBasicBlock *TBB,
430 MachineBasicBlock *FBB,
431 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000432 const DebugLoc &DL,
433 int *BytesAdded) const {
434 assert(!BytesAdded && "code size not handled");
Evan Cheng780748d2009-07-28 05:48:47 +0000435 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
436 int BOpc = !AFI->isThumbFunction()
437 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
438 int BccOpc = !AFI->isThumbFunction()
439 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000440 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000441
David Goodwinaf7451b2009-07-08 16:09:28 +0000442 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000443 assert(TBB && "insertBranch must not be told to insert a fallthrough");
David Goodwinaf7451b2009-07-08 16:09:28 +0000444 assert((Cond.size() == 2 || Cond.size() == 0) &&
445 "ARM branch conditions have two components!");
446
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000447 // For conditional branches, we use addOperand to preserve CPSR flags.
448
Craig Topper062a2ba2014-04-25 05:30:21 +0000449 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000450 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000451 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000452 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000453 else
454 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000455 } else
Diana Picus116bbab2017-01-13 09:58:52 +0000456 BuildMI(&MBB, DL, get(BccOpc))
457 .addMBB(TBB)
458 .addImm(Cond[0].getImm())
459 .add(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000460 return 1;
461 }
462
463 // Two-way conditional branch.
Diana Picus116bbab2017-01-13 09:58:52 +0000464 BuildMI(&MBB, DL, get(BccOpc))
465 .addMBB(TBB)
466 .addImm(Cond[0].getImm())
467 .add(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000468 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000469 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000470 else
471 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000472 return 2;
473}
474
475bool ARMBaseInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000476reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000477 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
478 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
479 return false;
480}
481
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000482bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
483 if (MI.isBundle()) {
484 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
485 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000486 while (++I != E && I->isInsideBundle()) {
487 int PIdx = I->findFirstPredOperandIdx();
488 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
489 return true;
490 }
491 return false;
492 }
493
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000494 int PIdx = MI.findFirstPredOperandIdx();
495 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000496}
497
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000498bool ARMBaseInstrInfo::PredicateInstruction(
499 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
500 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000501 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000502 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
503 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000504 .addImm(Pred[0].getImm())
505 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000506 return true;
507 }
508
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000509 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000510 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000511 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000512 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000513 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000514 return true;
515 }
516 return false;
517}
518
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000519bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
520 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000521 if (Pred1.size() > 2 || Pred2.size() > 2)
522 return false;
523
524 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
525 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
526 if (CC1 == CC2)
527 return true;
528
529 switch (CC1) {
530 default:
531 return false;
532 case ARMCC::AL:
533 return true;
534 case ARMCC::HS:
535 return CC2 == ARMCC::HI;
536 case ARMCC::LS:
537 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
538 case ARMCC::GE:
539 return CC2 == ARMCC::GT;
540 case ARMCC::LE:
541 return CC2 == ARMCC::LT;
542 }
543}
544
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000545bool ARMBaseInstrInfo::DefinesPredicate(
546 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000547 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000548 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
549 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000550 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
551 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000552 Pred.push_back(MO);
553 Found = true;
554 }
555 }
556
557 return Found;
558}
559
Javed Absar4ae7e8122017-06-02 08:53:19 +0000560bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
561 for (const auto &MO : MI.operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000562 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000563 return true;
564 return false;
565}
566
Javed Absar4ae7e8122017-06-02 08:53:19 +0000567bool ARMBaseInstrInfo::isAddrMode3OpImm(const MachineInstr &MI,
568 unsigned Op) const {
569 const MachineOperand &Offset = MI.getOperand(Op + 1);
570 return Offset.getReg() != 0;
571}
572
573// Load with negative register offset requires additional 1cyc and +I unit
574// for Cortex A57
575bool ARMBaseInstrInfo::isAddrMode3OpMinusReg(const MachineInstr &MI,
576 unsigned Op) const {
577 const MachineOperand &Offset = MI.getOperand(Op + 1);
578 const MachineOperand &Opc = MI.getOperand(Op + 2);
579 assert(Opc.isImm());
580 assert(Offset.isReg());
581 int64_t OpcImm = Opc.getImm();
582
583 bool isSub = ARM_AM::getAM3Op(OpcImm) == ARM_AM::sub;
584 return (isSub && Offset.getReg() != 0);
585}
586
587bool ARMBaseInstrInfo::isLdstScaledReg(const MachineInstr &MI,
588 unsigned Op) const {
589 const MachineOperand &Opc = MI.getOperand(Op + 2);
590 unsigned OffImm = Opc.getImm();
591 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
592}
593
594// Load, scaled register offset, not plus LSL2
595bool ARMBaseInstrInfo::isLdstScaledRegNotPlusLsl2(const MachineInstr &MI,
596 unsigned Op) const {
597 const MachineOperand &Opc = MI.getOperand(Op + 2);
598 unsigned OffImm = Opc.getImm();
599
600 bool isAdd = ARM_AM::getAM2Op(OffImm) == ARM_AM::add;
601 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
602 ARM_AM::ShiftOpc ShiftOpc = ARM_AM::getAM2ShiftOpc(OffImm);
603 if (ShiftOpc == ARM_AM::no_shift) return false; // not scaled
604 bool SimpleScaled = (isAdd && ShiftOpc == ARM_AM::lsl && Amt == 2);
605 return !SimpleScaled;
606}
607
608// Minus reg for ldstso addr mode
609bool ARMBaseInstrInfo::isLdstSoMinusReg(const MachineInstr &MI,
610 unsigned Op) const {
611 unsigned OffImm = MI.getOperand(Op + 2).getImm();
612 return ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
613}
614
615// Load, scaled register offset
616bool ARMBaseInstrInfo::isAm2ScaledReg(const MachineInstr &MI,
617 unsigned Op) const {
618 unsigned OffImm = MI.getOperand(Op + 2).getImm();
619 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
620}
621
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000622static bool isEligibleForITBlock(const MachineInstr *MI) {
623 switch (MI->getOpcode()) {
624 default: return true;
625 case ARM::tADC: // ADC (register) T1
626 case ARM::tADDi3: // ADD (immediate) T1
627 case ARM::tADDi8: // ADD (immediate) T2
628 case ARM::tADDrr: // ADD (register) T1
629 case ARM::tAND: // AND (register) T1
630 case ARM::tASRri: // ASR (immediate) T1
631 case ARM::tASRrr: // ASR (register) T1
632 case ARM::tBIC: // BIC (register) T1
633 case ARM::tEOR: // EOR (register) T1
634 case ARM::tLSLri: // LSL (immediate) T1
635 case ARM::tLSLrr: // LSL (register) T1
636 case ARM::tLSRri: // LSR (immediate) T1
637 case ARM::tLSRrr: // LSR (register) T1
638 case ARM::tMUL: // MUL T1
639 case ARM::tMVN: // MVN (register) T1
640 case ARM::tORR: // ORR (register) T1
641 case ARM::tROR: // ROR (register) T1
642 case ARM::tRSB: // RSB (immediate) T1
643 case ARM::tSBC: // SBC (register) T1
644 case ARM::tSUBi3: // SUB (immediate) T1
645 case ARM::tSUBi8: // SUB (immediate) T2
646 case ARM::tSUBrr: // SUB (register) T1
Javed Absar4ae7e8122017-06-02 08:53:19 +0000647 return !ARMBaseInstrInfo::isCPSRDefined(*MI);
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000648 }
649}
650
Evan Chenga33fc862009-11-21 06:21:52 +0000651/// isPredicable - Return true if the specified instruction can be predicated.
652/// By default, this returns true for every instruction with a
653/// PredicateOperand.
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000654bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000655 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000656 return false;
657
Saleem Abdulrasoolbfa25bd2016-09-06 04:00:12 +0000658 if (MI.isBundle())
659 return false;
660
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000661 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000662 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000663
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000664 const ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000665 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000666
Kristof Beyls96652492017-06-22 12:11:38 +0000667 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
668 // In their ARM encoding, they can't be encoded in a conditional form.
669 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
670 return false;
671
Joey Goulya5153cb2013-09-09 14:21:49 +0000672 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000673 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000674 return isV8EligibleForIT(&MI);
Evan Chenga33fc862009-11-21 06:21:52 +0000675 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000676
Evan Chenga33fc862009-11-21 06:21:52 +0000677 return true;
678}
David Goodwinaf7451b2009-07-08 16:09:28 +0000679
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000680namespace llvm {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000681
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000682template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000683 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
684 const MachineOperand &MO = MI->getOperand(i);
685 if (!MO.isReg() || MO.isUndef() || MO.isUse())
686 continue;
687 if (MO.getReg() != ARM::CPSR)
688 continue;
689 if (!MO.isDead())
690 return false;
691 }
692 // all definitions of CPSR are dead
693 return true;
694}
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000695
696} // end namespace llvm
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000697
David Goodwinaf7451b2009-07-08 16:09:28 +0000698/// GetInstSize - Return the size of the specified MachineInstr.
699///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000700unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000701 const MachineBasicBlock &MBB = *MI.getParent();
David Goodwinaf7451b2009-07-08 16:09:28 +0000702 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000703 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000704
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000705 const MCInstrDesc &MCID = MI.getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000706 if (MCID.getSize())
707 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000708
David Blaikie46a9f012012-01-20 21:51:11 +0000709 // If this machine instr is an inline asm, measure it.
Peter Smith6f36cd42018-10-08 09:38:28 +0000710 if (MI.getOpcode() == ARM::INLINEASM) {
711 unsigned Size = getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
712 if (!MF->getInfo<ARMFunctionInfo>()->isThumbFunction())
713 Size = alignTo(Size, 4);
714 return Size;
715 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000716 unsigned Opc = MI.getOpcode();
David Blaikie46a9f012012-01-20 21:51:11 +0000717 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000718 default:
719 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000720 return 0;
721 case TargetOpcode::BUNDLE:
722 return getInstBundleLength(MI);
723 case ARM::MOVi16_ga_pcrel:
724 case ARM::MOVTi16_ga_pcrel:
725 case ARM::t2MOVi16_ga_pcrel:
726 case ARM::t2MOVTi16_ga_pcrel:
727 return 4;
728 case ARM::MOVi32imm:
729 case ARM::t2MOVi32imm:
730 return 8;
731 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000732 case ARM::JUMPTABLE_INSTS:
733 case ARM::JUMPTABLE_ADDRS:
734 case ARM::JUMPTABLE_TBB:
735 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000736 // If this machine instr is a constant pool entry, its size is recorded as
737 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000738 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000739 case ARM::Int_eh_sjlj_longjmp:
740 return 16;
741 case ARM::tInt_eh_sjlj_longjmp:
742 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000743 case ARM::tInt_WIN_eh_sjlj_longjmp:
744 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000745 case ARM::Int_eh_sjlj_setjmp:
746 case ARM::Int_eh_sjlj_setjmp_nofp:
747 return 20;
748 case ARM::tInt_eh_sjlj_setjmp:
749 case ARM::t2Int_eh_sjlj_setjmp:
750 case ARM::t2Int_eh_sjlj_setjmp_nofp:
751 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000752 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000753 return MI.getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000754 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000755}
756
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000757unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000758 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000759 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
760 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000761 while (++I != E && I->isInsideBundle()) {
762 assert(!I->isBundle() && "No nested bundle!");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000763 Size += getInstSizeInBytes(*I);
Evan Cheng7fae11b2011-12-14 02:11:42 +0000764 }
765 return Size;
766}
767
Tim Northover5d72c5d2014-10-01 19:21:03 +0000768void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
769 MachineBasicBlock::iterator I,
770 unsigned DestReg, bool KillSrc,
771 const ARMSubtarget &Subtarget) const {
772 unsigned Opc = Subtarget.isThumb()
773 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
774 : ARM::MRS;
775
776 MachineInstrBuilder MIB =
777 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
778
779 // There is only 1 A/R class MRS instruction, and it always refers to
780 // APSR. However, there are lots of other possibilities on M-class cores.
781 if (Subtarget.isMClass())
782 MIB.addImm(0x800);
783
Diana Picus4f8c3e12017-01-13 09:37:56 +0000784 MIB.add(predOps(ARMCC::AL))
785 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
Tim Northover5d72c5d2014-10-01 19:21:03 +0000786}
787
788void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
789 MachineBasicBlock::iterator I,
790 unsigned SrcReg, bool KillSrc,
791 const ARMSubtarget &Subtarget) const {
792 unsigned Opc = Subtarget.isThumb()
793 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
794 : ARM::MSR;
795
796 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
797
798 if (Subtarget.isMClass())
799 MIB.addImm(0x800);
800 else
801 MIB.addImm(8);
802
Diana Picus4f8c3e12017-01-13 09:37:56 +0000803 MIB.addReg(SrcReg, getKillRegState(KillSrc))
804 .add(predOps(ARMCC::AL))
805 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
Tim Northover5d72c5d2014-10-01 19:21:03 +0000806}
807
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000808void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000809 MachineBasicBlock::iterator I,
810 const DebugLoc &DL, unsigned DestReg,
811 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000812 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000813 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000814
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000815 if (GPRDest && GPRSrc) {
Diana Picus8a73f552017-01-13 10:18:01 +0000816 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
817 .addReg(SrcReg, getKillRegState(KillSrc))
818 .add(predOps(ARMCC::AL))
819 .add(condCodeOp());
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000820 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000821 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000822
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000823 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000824 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000825
Chad Rosierbe762512011-08-20 00:17:25 +0000826 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000827 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000828 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000829 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000830 Opc = ARM::VMOVRS;
831 else if (SPRDest && GPRSrc)
832 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000833 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000834 Opc = ARM::VMOVD;
835 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000836 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000837
Chad Rosierbe762512011-08-20 00:17:25 +0000838 if (Opc) {
839 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000840 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000841 if (Opc == ARM::VORRq)
842 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Diana Picus4f8c3e12017-01-13 09:37:56 +0000843 MIB.add(predOps(ARMCC::AL));
Chad Rosierbe762512011-08-20 00:17:25 +0000844 return;
845 }
846
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000847 // Handle register classes that require multiple instructions.
848 unsigned BeginIdx = 0;
849 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000850 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000851
852 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000853 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
854 Opc = ARM::VORRq;
855 BeginIdx = ARM::qsub_0;
856 SubRegs = 2;
857 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
858 Opc = ARM::VORRq;
859 BeginIdx = ARM::qsub_0;
860 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000861 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000862 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
863 Opc = ARM::VMOVD;
864 BeginIdx = ARM::dsub_0;
865 SubRegs = 2;
866 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
867 Opc = ARM::VMOVD;
868 BeginIdx = ARM::dsub_0;
869 SubRegs = 3;
870 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
871 Opc = ARM::VMOVD;
872 BeginIdx = ARM::dsub_0;
873 SubRegs = 4;
874 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000875 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000876 BeginIdx = ARM::gsub_0;
877 SubRegs = 2;
878 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
879 Opc = ARM::VMOVD;
880 BeginIdx = ARM::dsub_0;
881 SubRegs = 2;
882 Spacing = 2;
883 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
884 Opc = ARM::VMOVD;
885 BeginIdx = ARM::dsub_0;
886 SubRegs = 3;
887 Spacing = 2;
888 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
889 Opc = ARM::VMOVD;
890 BeginIdx = ARM::dsub_0;
891 SubRegs = 4;
892 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000893 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
894 Opc = ARM::VMOVS;
895 BeginIdx = ARM::ssub_0;
896 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000897 } else if (SrcReg == ARM::CPSR) {
898 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
899 return;
900 } else if (DestReg == ARM::CPSR) {
901 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
902 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000903 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000904
Andrew Trickb57e2252012-08-29 04:41:37 +0000905 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000906
Andrew Trick4cc69492012-08-29 01:58:52 +0000907 const TargetRegisterInfo *TRI = &getRegisterInfo();
908 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000909
910 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
911 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000912 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000913 Spacing = -Spacing;
914 }
915#ifndef NDEBUG
916 SmallSet<unsigned, 4> DstRegs;
917#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000918 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000919 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
920 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000921 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000922#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000923 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000924 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000925#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000926 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000927 // VORR takes two source operands.
928 if (Opc == ARM::VORRq)
929 Mov.addReg(Src);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000930 Mov = Mov.add(predOps(ARMCC::AL));
JF Bastien583db652013-07-12 23:33:03 +0000931 // MOVr can set CC.
932 if (Opc == ARM::MOVr)
Diana Picus8a73f552017-01-13 10:18:01 +0000933 Mov = Mov.add(condCodeOp());
Andrew Trick4cc69492012-08-29 01:58:52 +0000934 }
935 // Add implicit super-register defs and kills to the last instruction.
936 Mov->addRegisterDefined(DestReg, TRI);
937 if (KillSrc)
938 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000939}
940
Alexander Ivchenkoaf961122018-08-30 14:32:47 +0000941bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
942 const MachineOperand *&Src,
943 const MachineOperand *&Dest) const {
Petar Jovanovicc0510002018-05-23 15:28:28 +0000944 // VMOVRRD is also a copy instruction but it requires
945 // special way of handling. It is more complex copy version
946 // and since that we are not considering it. For recognition
947 // of such instruction isExtractSubregLike MI interface fuction
948 // could be used.
949 // VORRq is considered as a move only if two inputs are
950 // the same register.
951 if (!MI.isMoveReg() ||
952 (MI.getOpcode() == ARM::VORRq &&
953 MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
954 return false;
Petar Jovanovic8cb6a522018-06-06 16:36:30 +0000955 Dest = &MI.getOperand(0);
956 Src = &MI.getOperand(1);
Petar Jovanovicc0510002018-05-23 15:28:28 +0000957 return true;
958}
959
Tim Northover798697d2013-04-21 11:57:07 +0000960const MachineInstrBuilder &
961ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
962 unsigned SubIdx, unsigned State,
963 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000964 if (!SubIdx)
965 return MIB.addReg(Reg, State);
966
967 if (TargetRegisterInfo::isPhysicalRegister(Reg))
968 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
969 return MIB.addReg(Reg, State, SubIdx);
970}
971
David Goodwinaf7451b2009-07-08 16:09:28 +0000972void ARMBaseInstrInfo::
973storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
974 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000975 const TargetRegisterClass *RC,
976 const TargetRegisterInfo *TRI) const {
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000977 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000978 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000979 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000980
Alex Lorenze40c8a22015-08-11 23:09:45 +0000981 MachineMemOperand *MMO = MF.getMachineMemOperand(
982 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
983 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000984
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000985 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000986 case 2:
987 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +0000988 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRH))
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000989 .addReg(SrcReg, getKillRegState(isKill))
990 .addFrameIndex(FI)
991 .addImm(0)
992 .addMemOperand(MMO)
993 .add(predOps(ARMCC::AL));
994 } else
995 llvm_unreachable("Unknown reg class!");
996 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000997 case 4:
998 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +0000999 BuildMI(MBB, I, DebugLoc(), get(ARM::STRi12))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001000 .addReg(SrcReg, getKillRegState(isKill))
1001 .addFrameIndex(FI)
1002 .addImm(0)
1003 .addMemOperand(MMO)
1004 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001005 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001006 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRS))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001007 .addReg(SrcReg, getKillRegState(isKill))
1008 .addFrameIndex(FI)
1009 .addImm(0)
1010 .addMemOperand(MMO)
1011 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001012 } else
1013 llvm_unreachable("Unknown reg class!");
1014 break;
1015 case 8:
1016 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001017 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRD))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001018 .addReg(SrcReg, getKillRegState(isKill))
1019 .addFrameIndex(FI)
1020 .addImm(0)
1021 .addMemOperand(MMO)
1022 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001023 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001024 if (Subtarget.hasV5TEOps()) {
Matthias Braun81578e92018-10-05 22:00:13 +00001025 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STRD));
Tim Northover798697d2013-04-21 11:57:07 +00001026 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1027 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001028 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1029 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001030 } else {
1031 // Fallback to STM instruction, which has existed since the dawn of
1032 // time.
Matthias Braun81578e92018-10-05 22:00:13 +00001033 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STMIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001034 .addFrameIndex(FI)
1035 .addMemOperand(MMO)
1036 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001037 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1038 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1039 }
Owen Anderson732f82c2011-08-10 17:21:20 +00001040 } else
1041 llvm_unreachable("Unknown reg class!");
1042 break;
1043 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001044 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001045 // Use aligned spills if the stack can be realigned.
1046 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001047 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1q64))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001048 .addFrameIndex(FI)
1049 .addImm(16)
1050 .addReg(SrcReg, getKillRegState(isKill))
1051 .addMemOperand(MMO)
1052 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001053 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001054 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMQIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001055 .addReg(SrcReg, getKillRegState(isKill))
1056 .addFrameIndex(FI)
1057 .addMemOperand(MMO)
1058 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001059 }
1060 } else
1061 llvm_unreachable("Unknown reg class!");
1062 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001063 case 24:
1064 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1065 // Use aligned spills if the stack can be realigned.
1066 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001067 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64TPseudo))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001068 .addFrameIndex(FI)
1069 .addImm(16)
1070 .addReg(SrcReg, getKillRegState(isKill))
1071 .addMemOperand(MMO)
1072 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001073 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001074 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1075 get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001076 .addFrameIndex(FI)
1077 .add(predOps(ARMCC::AL))
1078 .addMemOperand(MMO);
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001079 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1080 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1081 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1082 }
1083 } else
1084 llvm_unreachable("Unknown reg class!");
1085 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001086 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001087 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001088 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1089 // FIXME: It's possible to only store part of the QQ register if the
1090 // spilled def has a sub-register index.
Matthias Braun81578e92018-10-05 22:00:13 +00001091 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64QPseudo))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001092 .addFrameIndex(FI)
1093 .addImm(16)
1094 .addReg(SrcReg, getKillRegState(isKill))
1095 .addMemOperand(MMO)
1096 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001097 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001098 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1099 get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001100 .addFrameIndex(FI)
1101 .add(predOps(ARMCC::AL))
1102 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001103 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1104 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1105 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1106 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1107 }
1108 } else
1109 llvm_unreachable("Unknown reg class!");
1110 break;
1111 case 64:
1112 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001113 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001114 .addFrameIndex(FI)
1115 .add(predOps(ARMCC::AL))
1116 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001117 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1118 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1119 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1120 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1121 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1122 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1123 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1124 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1125 } else
1126 llvm_unreachable("Unknown reg class!");
1127 break;
1128 default:
1129 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001130 }
1131}
1132
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001133unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1134 int &FrameIndex) const {
1135 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001136 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001137 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001138 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001139 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1140 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1141 MI.getOperand(3).getImm() == 0) {
1142 FrameIndex = MI.getOperand(1).getIndex();
1143 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001144 }
1145 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001146 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001147 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001148 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001149 case ARM::VSTRD:
1150 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1152 MI.getOperand(2).getImm() == 0) {
1153 FrameIndex = MI.getOperand(1).getIndex();
1154 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001155 }
1156 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001157 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001158 case ARM::VST1d64TPseudo:
1159 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001160 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1161 FrameIndex = MI.getOperand(0).getIndex();
1162 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001163 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001164 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001165 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1167 FrameIndex = MI.getOperand(1).getIndex();
1168 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001169 }
1170 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001171 }
1172
1173 return 0;
1174}
1175
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001176unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001177 int &FrameIndex) const {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001178 SmallVector<const MachineMemOperand *, 1> Accesses;
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001179 if (MI.mayStore() && hasStoreToStackSlot(MI, Accesses)) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001180 FrameIndex =
1181 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1182 ->getFrameIndex();
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001183 return true;
1184 }
1185 return false;
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001186}
1187
David Goodwinaf7451b2009-07-08 16:09:28 +00001188void ARMBaseInstrInfo::
1189loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1190 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001191 const TargetRegisterClass *RC,
1192 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001193 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001194 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001195 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001196 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001197 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001198 MachineMemOperand *MMO = MF.getMachineMemOperand(
1199 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1200 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001201
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001202 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +00001203 case 2:
1204 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
1205 BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
1206 .addFrameIndex(FI)
1207 .addImm(0)
1208 .addMemOperand(MMO)
1209 .add(predOps(ARMCC::AL));
1210 } else
1211 llvm_unreachable("Unknown reg class!");
1212 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001213 case 4:
1214 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001215 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1216 .addFrameIndex(FI)
1217 .addImm(0)
1218 .addMemOperand(MMO)
1219 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001220 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001221 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1222 .addFrameIndex(FI)
1223 .addImm(0)
1224 .addMemOperand(MMO)
1225 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001226 } else
1227 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001228 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001229 case 8:
1230 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001231 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1232 .addFrameIndex(FI)
1233 .addImm(0)
1234 .addMemOperand(MMO)
1235 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001236 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001237 MachineInstrBuilder MIB;
1238
1239 if (Subtarget.hasV5TEOps()) {
1240 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1241 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1242 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001243 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1244 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001245 } else {
1246 // Fallback to LDM instruction, which has existed since the dawn of
1247 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001248 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1249 .addFrameIndex(FI)
1250 .addMemOperand(MMO)
1251 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001252 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1253 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1254 }
1255
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001256 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1257 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001258 } else
1259 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001260 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001261 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001262 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001263 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001264 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1265 .addFrameIndex(FI)
1266 .addImm(16)
1267 .addMemOperand(MMO)
1268 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001269 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001270 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1271 .addFrameIndex(FI)
1272 .addMemOperand(MMO)
1273 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001274 }
1275 } else
1276 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001277 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001278 case 24:
1279 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1280 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001281 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1282 .addFrameIndex(FI)
1283 .addImm(16)
1284 .addMemOperand(MMO)
1285 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001286 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001287 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1288 .addFrameIndex(FI)
1289 .addMemOperand(MMO)
1290 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001291 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1292 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1293 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1294 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1295 MIB.addReg(DestReg, RegState::ImplicitDefine);
1296 }
1297 } else
1298 llvm_unreachable("Unknown reg class!");
1299 break;
1300 case 32:
1301 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001302 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001303 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1304 .addFrameIndex(FI)
1305 .addImm(16)
1306 .addMemOperand(MMO)
1307 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001308 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001309 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1310 .addFrameIndex(FI)
1311 .add(predOps(ARMCC::AL))
1312 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001313 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1314 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1315 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1316 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001317 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1318 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001319 }
1320 } else
1321 llvm_unreachable("Unknown reg class!");
1322 break;
1323 case 64:
1324 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001325 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1326 .addFrameIndex(FI)
1327 .add(predOps(ARMCC::AL))
1328 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001329 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1330 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1331 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1332 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1333 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1334 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1335 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1336 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001337 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1338 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001339 } else
1340 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001341 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001342 default:
1343 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001344 }
1345}
1346
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001347unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1348 int &FrameIndex) const {
1349 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001350 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001351 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001352 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001353 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1354 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1355 MI.getOperand(3).getImm() == 0) {
1356 FrameIndex = MI.getOperand(1).getIndex();
1357 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001358 }
1359 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001360 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001361 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001362 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001363 case ARM::VLDRD:
1364 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001365 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1366 MI.getOperand(2).getImm() == 0) {
1367 FrameIndex = MI.getOperand(1).getIndex();
1368 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001369 }
1370 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001371 case ARM::VLD1q64:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001372 case ARM::VLD1d8TPseudo:
1373 case ARM::VLD1d16TPseudo:
1374 case ARM::VLD1d32TPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001375 case ARM::VLD1d64TPseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001376 case ARM::VLD1d8QPseudo:
1377 case ARM::VLD1d16QPseudo:
1378 case ARM::VLD1d32QPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001379 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001380 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1381 FrameIndex = MI.getOperand(1).getIndex();
1382 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001383 }
1384 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001385 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001386 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1387 FrameIndex = MI.getOperand(1).getIndex();
1388 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001389 }
1390 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001391 }
1392
1393 return 0;
1394}
1395
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001396unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1397 int &FrameIndex) const {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001398 SmallVector<const MachineMemOperand *, 1> Accesses;
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001399 if (MI.mayLoad() && hasLoadFromStackSlot(MI, Accesses)) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001400 FrameIndex =
1401 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1402 ->getFrameIndex();
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001403 return true;
1404 }
1405 return false;
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001406}
1407
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001408/// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
Scott Douglass953f9082015-10-05 14:49:54 +00001409/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001410void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001411 bool isThumb1 = Subtarget.isThumb1Only();
1412 bool isThumb2 = Subtarget.isThumb2();
1413 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1414
Scott Douglass953f9082015-10-05 14:49:54 +00001415 DebugLoc dl = MI->getDebugLoc();
1416 MachineBasicBlock *BB = MI->getParent();
1417
1418 MachineInstrBuilder LDM, STM;
1419 if (isThumb1 || !MI->getOperand(1).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001420 MachineOperand LDWb(MI->getOperand(1));
Scott Douglass953f9082015-10-05 14:49:54 +00001421 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1422 : isThumb1 ? ARM::tLDMIA_UPD
1423 : ARM::LDMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001424 .add(LDWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001425 } else {
1426 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1427 }
1428
1429 if (isThumb1 || !MI->getOperand(0).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001430 MachineOperand STWb(MI->getOperand(0));
Scott Douglass953f9082015-10-05 14:49:54 +00001431 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1432 : isThumb1 ? ARM::tSTMIA_UPD
1433 : ARM::STMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001434 .add(STWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001435 } else {
1436 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1437 }
1438
Geoff Berry60c43102017-12-12 17:53:59 +00001439 MachineOperand LDBase(MI->getOperand(3));
Geoff Berry60c43102017-12-12 17:53:59 +00001440 LDM.add(LDBase).add(predOps(ARMCC::AL));
1441
1442 MachineOperand STBase(MI->getOperand(2));
Geoff Berry60c43102017-12-12 17:53:59 +00001443 STM.add(STBase).add(predOps(ARMCC::AL));
Scott Douglass953f9082015-10-05 14:49:54 +00001444
1445 // Sort the scratch registers into ascending order.
1446 const TargetRegisterInfo &TRI = getRegisterInfo();
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001447 SmallVector<unsigned, 6> ScratchRegs;
Scott Douglass953f9082015-10-05 14:49:54 +00001448 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1449 ScratchRegs.push_back(MI->getOperand(I).getReg());
Fangrui Song0cac7262018-09-27 02:13:45 +00001450 llvm::sort(ScratchRegs,
1451 [&TRI](const unsigned &Reg1, const unsigned &Reg2) -> bool {
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001452 return TRI.getEncodingValue(Reg1) <
1453 TRI.getEncodingValue(Reg2);
1454 });
Scott Douglass953f9082015-10-05 14:49:54 +00001455
1456 for (const auto &Reg : ScratchRegs) {
1457 LDM.addReg(Reg, RegState::Define);
1458 STM.addReg(Reg, RegState::Kill);
1459 }
1460
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001461 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001462}
1463
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001464bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1465 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001466 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001467 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001468 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001469 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001470 return true;
1471 }
1472
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001473 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001474 expandMEMCPY(MI);
1475 return true;
1476 }
1477
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001478 // This hook gets to expand COPY instructions before they become
1479 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1480 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1481 // changed into a VORR that can go down the NEON pipeline.
Diana Picusb772e402016-07-06 11:22:11 +00001482 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001483 return false;
1484
1485 // Look for a copy between even S-registers. That is where we keep floats
1486 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001487 unsigned DstRegS = MI.getOperand(0).getReg();
1488 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001489 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1490 return false;
1491
1492 const TargetRegisterInfo *TRI = &getRegisterInfo();
1493 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1494 &ARM::DPRRegClass);
1495 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1496 &ARM::DPRRegClass);
1497 if (!DstRegD || !SrcRegD)
1498 return false;
1499
1500 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1501 // legal if the COPY already defines the full DstRegD, and it isn't a
1502 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001503 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001504 return false;
1505
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001506 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001507 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001508 return false;
1509
1510 // All clear, widen the COPY.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001511 LLVM_DEBUG(dbgs() << "widening: " << MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001512 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001513
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001514 // 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 +00001515 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001516 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001517 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001518 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001519
1520 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001521 MI.setDesc(get(ARM::VMOVD));
1522 MI.getOperand(0).setReg(DstRegD);
1523 MI.getOperand(1).setReg(SrcRegD);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001524 MIB.add(predOps(ARMCC::AL));
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001525
1526 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1527 // register scavenger and machine verifier, so we need to indicate that we
1528 // are reading an undefined value from SrcRegD, but a proper value from
1529 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001530 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001531 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001532
1533 // SrcRegD may actually contain an unrelated value in the ssub_1
1534 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001535 if (MI.getOperand(1).isKill()) {
1536 MI.getOperand(1).setIsKill(false);
1537 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001538 }
1539
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001540 LLVM_DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001541 return true;
1542}
1543
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001544/// Create a copy of a const pool value. Update CPI to the new index and return
1545/// the label UID.
1546static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1547 MachineConstantPool *MCP = MF.getConstantPool();
1548 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1549
1550 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1551 assert(MCPE.isMachineConstantPoolEntry() &&
1552 "Expecting a machine constantpool entry!");
1553 ARMConstantPoolValue *ACPV =
1554 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1555
Evan Chengdfce83c2011-01-17 08:03:18 +00001556 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001557 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001558
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001559 // FIXME: The below assumes PIC relocation model and that the function
1560 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1561 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1562 // instructions, so that's probably OK, but is PIC always correct when
1563 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001564 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001565 NewCPV = ARMConstantPoolConstant::Create(
1566 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1567 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001568 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001569 NewCPV = ARMConstantPoolSymbol::
Matthias Braunf1caa282017-12-15 22:22:58 +00001570 Create(MF.getFunction().getContext(),
Bill Wendlingc214cb02011-10-01 08:58:29 +00001571 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001572 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001573 NewCPV = ARMConstantPoolConstant::
1574 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1575 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001576 else if (ACPV->isLSDA())
Matthias Braunf1caa282017-12-15 22:22:58 +00001577 NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
Bill Wendling7753d662011-10-01 08:00:54 +00001578 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001579 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001580 NewCPV = ARMConstantPoolMBB::
Matthias Braunf1caa282017-12-15 22:22:58 +00001581 Create(MF.getFunction().getContext(),
Bill Wendling4a4772f2011-10-01 09:30:42 +00001582 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001583 else
1584 llvm_unreachable("Unexpected ARM constantpool value type!!");
1585 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1586 return PCLabelId;
1587}
1588
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001589void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1590 MachineBasicBlock::iterator I,
1591 unsigned DestReg, unsigned SubIdx,
1592 const MachineInstr &Orig,
1593 const TargetRegisterInfo &TRI) const {
1594 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001595 switch (Opcode) {
1596 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001597 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1598 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001599 MBB.insert(I, MI);
1600 break;
1601 }
1602 case ARM::tLDRpci_pic:
1603 case ARM::t2LDRpci_pic: {
1604 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001605 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001606 unsigned PCLabelId = duplicateCPV(MF, CPI);
Chandler Carruthc73c0302018-08-16 21:30:05 +00001607 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1608 .addConstantPoolIndex(CPI)
1609 .addImm(PCLabelId)
1610 .cloneMemRefs(Orig);
Evan Chengfe864422009-11-08 00:15:23 +00001611 break;
1612 }
1613 }
Evan Chengfe864422009-11-08 00:15:23 +00001614}
1615
Matthias Braun55bc9b32017-08-22 23:56:30 +00001616MachineInstr &
1617ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1618 MachineBasicBlock::iterator InsertBefore,
1619 const MachineInstr &Orig) const {
1620 MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1621 MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1622 for (;;) {
1623 switch (I->getOpcode()) {
1624 case ARM::tLDRpci_pic:
1625 case ARM::t2LDRpci_pic: {
1626 MachineFunction &MF = *MBB.getParent();
1627 unsigned CPI = I->getOperand(1).getIndex();
1628 unsigned PCLabelId = duplicateCPV(MF, CPI);
1629 I->getOperand(1).setIndex(CPI);
1630 I->getOperand(2).setImm(PCLabelId);
1631 break;
1632 }
1633 }
1634 if (!I->isBundledWithSucc())
1635 break;
1636 ++I;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001637 }
Matthias Braun55bc9b32017-08-22 23:56:30 +00001638 return Cloned;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001639}
1640
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001641bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1642 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001643 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001644 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001645 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001646 Opcode == ARM::t2LDRpci_pic ||
1647 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001648 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001649 Opcode == ARM::LDRLIT_ga_pcrel ||
1650 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1651 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001652 Opcode == ARM::MOV_ga_pcrel ||
1653 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001654 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001655 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001656 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001657 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001658 return false;
1659
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001660 const MachineOperand &MO0 = MI0.getOperand(1);
1661 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001662 if (MO0.getOffset() != MO1.getOffset())
1663 return false;
1664
Tim Northover72360d22013-12-02 10:35:41 +00001665 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1666 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1667 Opcode == ARM::tLDRLIT_ga_pcrel ||
1668 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001669 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001670 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001671 // Ignore the PC labels.
1672 return MO0.getGlobal() == MO1.getGlobal();
1673
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001674 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001675 const MachineConstantPool *MCP = MF->getConstantPool();
1676 int CPI0 = MO0.getIndex();
1677 int CPI1 = MO1.getIndex();
1678 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1679 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001680 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1681 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1682 if (isARMCP0 && isARMCP1) {
1683 ARMConstantPoolValue *ACPV0 =
1684 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1685 ARMConstantPoolValue *ACPV1 =
1686 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1687 return ACPV0->hasSameValue(ACPV1);
1688 } else if (!isARMCP0 && !isARMCP1) {
1689 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1690 }
1691 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001692 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001693 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001694 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001695 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001696 return false;
1697
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001698 unsigned Addr0 = MI0.getOperand(1).getReg();
1699 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001700 if (Addr0 != Addr1) {
1701 if (!MRI ||
1702 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1703 !TargetRegisterInfo::isVirtualRegister(Addr1))
1704 return false;
1705
1706 // This assumes SSA form.
1707 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1708 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1709 // Check if the loaded value, e.g. a constantpool of a global address, are
1710 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001711 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001712 return false;
1713 }
1714
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001715 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00001716 // %12 = PICLDR %11, 0, 14, %noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001717 const MachineOperand &MO0 = MI0.getOperand(i);
1718 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001719 if (!MO0.isIdenticalTo(MO1))
1720 return false;
1721 }
1722 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001723 }
1724
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001725 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001726}
1727
Bill Wendlingf4707472010-06-23 23:00:16 +00001728/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1729/// determine if two loads are loading from the same base address. It should
1730/// only return true if the base pointers are the same and the only differences
1731/// between the two addresses is the offset. It also returns the offsets by
1732/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001733///
1734/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1735/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001736bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1737 int64_t &Offset1,
1738 int64_t &Offset2) const {
1739 // Don't worry about Thumb: just ARM and Thumb2.
1740 if (Subtarget.isThumb1Only()) return false;
1741
1742 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1743 return false;
1744
1745 switch (Load1->getMachineOpcode()) {
1746 default:
1747 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001748 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001749 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001750 case ARM::LDRD:
1751 case ARM::LDRH:
1752 case ARM::LDRSB:
1753 case ARM::LDRSH:
1754 case ARM::VLDRD:
1755 case ARM::VLDRS:
1756 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001757 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001758 case ARM::t2LDRDi8:
1759 case ARM::t2LDRSHi8:
1760 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001761 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001762 case ARM::t2LDRSHi12:
1763 break;
1764 }
1765
1766 switch (Load2->getMachineOpcode()) {
1767 default:
1768 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001769 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001770 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001771 case ARM::LDRD:
1772 case ARM::LDRH:
1773 case ARM::LDRSB:
1774 case ARM::LDRSH:
1775 case ARM::VLDRD:
1776 case ARM::VLDRS:
1777 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001778 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001779 case ARM::t2LDRSHi8:
1780 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001781 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001782 case ARM::t2LDRSHi12:
1783 break;
1784 }
1785
1786 // Check if base addresses and chain operands match.
1787 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1788 Load1->getOperand(4) != Load2->getOperand(4))
1789 return false;
1790
1791 // Index should be Reg0.
1792 if (Load1->getOperand(3) != Load2->getOperand(3))
1793 return false;
1794
1795 // Determine the offsets.
1796 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1797 isa<ConstantSDNode>(Load2->getOperand(1))) {
1798 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1799 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1800 return true;
1801 }
1802
1803 return false;
1804}
1805
1806/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001807/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001808/// be scheduled togther. On some targets if two loads are loading from
1809/// addresses in the same cache line, it's better if they are scheduled
1810/// together. This function takes two integers that represent the load offsets
1811/// from the common base address. It returns true if it decides it's desirable
1812/// to schedule the two loads together. "NumLoads" is the number of loads that
1813/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001814///
1815/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1816/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001817bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1818 int64_t Offset1, int64_t Offset2,
1819 unsigned NumLoads) const {
1820 // Don't worry about Thumb: just ARM and Thumb2.
1821 if (Subtarget.isThumb1Only()) return false;
1822
1823 assert(Offset2 > Offset1);
1824
1825 if ((Offset2 - Offset1) / 8 > 64)
1826 return false;
1827
Renato Golinb184cd92013-08-14 16:35:29 +00001828 // Check if the machine opcodes are different. If they are different
1829 // then we consider them to not be of the same base address,
1830 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1831 // In this case, they are considered to be the same because they are different
1832 // encoding forms of the same basic instruction.
1833 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1834 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1835 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1836 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1837 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001838 return false; // FIXME: overly conservative?
1839
1840 // Four loads in a row should be sufficient.
1841 if (NumLoads >= 3)
1842 return false;
1843
1844 return true;
1845}
1846
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001847bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001848 const MachineBasicBlock *MBB,
1849 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001850 // Debug info is never a scheduling boundary. It's necessary to be explicit
1851 // due to the special treatment of IT instructions below, otherwise a
1852 // dbg_value followed by an IT will result in the IT instruction being
1853 // considered a scheduling hazard, which is wrong. It should be the actual
1854 // instruction preceding the dbg_value instruction(s), just like it is
1855 // when debug info is not present.
Shiva Chen801bf7e2018-05-09 02:42:00 +00001856 if (MI.isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001857 return false;
1858
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001859 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001860 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001861 return true;
1862
1863 // Treat the start of the IT block as a scheduling boundary, but schedule
1864 // t2IT along with all instructions following it.
1865 // FIXME: This is a big hammer. But the alternative is to add all potential
1866 // true and anti dependencies to IT block instructions as implicit operands
1867 // to the t2IT instruction. The added compile time and complexity does not
1868 // seem worth it.
1869 MachineBasicBlock::const_iterator I = MI;
Shiva Chen801bf7e2018-05-09 02:42:00 +00001870 // Make sure to skip any debug instructions
1871 while (++I != MBB->end() && I->isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001872 ;
1873 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001874 return true;
1875
1876 // Don't attempt to schedule around any instruction that defines
1877 // a stack-oriented pointer, as it's unlikely to be profitable. This
1878 // saves compile time, because it doesn't require every single
1879 // stack slot reference to depend on the instruction that does the
1880 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001881 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001882 // No ARM calling conventions change the stack pointer. (X86 calling
1883 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001884 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001885 return true;
1886
1887 return false;
1888}
1889
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001890bool ARMBaseInstrInfo::
1891isProfitableToIfCvt(MachineBasicBlock &MBB,
1892 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001893 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001894 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001895 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001896
Peter Collingbourne65295232015-04-23 20:31:30 +00001897 // If we are optimizing for size, see if the branch in the predecessor can be
1898 // lowered to cbn?z by the constant island lowering pass, and return false if
1899 // so. This results in a shorter instruction sequence.
Matthias Braunf1caa282017-12-15 22:22:58 +00001900 if (MBB.getParent()->getFunction().optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001901 MachineBasicBlock *Pred = *MBB.pred_begin();
1902 if (!Pred->empty()) {
1903 MachineInstr *LastMI = &*Pred->rbegin();
1904 if (LastMI->getOpcode() == ARM::t2Bcc) {
1905 MachineBasicBlock::iterator CmpMI = LastMI;
1906 if (CmpMI != Pred->begin()) {
1907 --CmpMI;
1908 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1909 CmpMI->getOpcode() == ARM::t2CMPri) {
1910 unsigned Reg = CmpMI->getOperand(0).getReg();
1911 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001912 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
Peter Collingbourne65295232015-04-23 20:31:30 +00001913 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1914 isARMLowRegister(Reg))
1915 return false;
1916 }
1917 }
1918 }
1919 }
1920 }
Artyom Skrobov283316b2017-03-14 13:38:45 +00001921 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1922 MBB, 0, 0, Probability);
Evan Cheng02b184d2010-06-25 22:42:03 +00001923}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001924
Evan Cheng02b184d2010-06-25 22:42:03 +00001925bool ARMBaseInstrInfo::
John Brawn75d76e52017-06-28 14:11:15 +00001926isProfitableToIfCvt(MachineBasicBlock &TBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001927 unsigned TCycles, unsigned TExtra,
John Brawn75d76e52017-06-28 14:11:15 +00001928 MachineBasicBlock &FBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001929 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001930 BranchProbability Probability) const {
Artyom Skrobov283316b2017-03-14 13:38:45 +00001931 if (!TCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001932 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001933
Owen Anderson88af7d02010-09-28 18:32:13 +00001934 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001935 // Here we scale up each component of UnpredCost to avoid precision issue when
1936 // scaling TCycles/FCycles by Probability.
1937 const unsigned ScalingUpFactor = 1024;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001938
John Brawn75d76e52017-06-28 14:11:15 +00001939 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1940 unsigned UnpredCost;
1941 if (!Subtarget.hasBranchPredictor()) {
1942 // When we don't have a branch predictor it's always cheaper to not take a
1943 // branch than take it, so we have to take that into account.
1944 unsigned NotTakenBranchCost = 1;
1945 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1946 unsigned TUnpredCycles, FUnpredCycles;
1947 if (!FCycles) {
1948 // Triangle: TBB is the fallthrough
1949 TUnpredCycles = TCycles + NotTakenBranchCost;
1950 FUnpredCycles = TakenBranchCost;
1951 } else {
1952 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1953 TUnpredCycles = TCycles + TakenBranchCost;
1954 FUnpredCycles = FCycles + NotTakenBranchCost;
John Brawn97cc2832017-07-12 13:23:10 +00001955 // The branch at the end of FBB will disappear when it's predicated, so
1956 // discount it from PredCost.
1957 PredCost -= 1 * ScalingUpFactor;
John Brawn75d76e52017-06-28 14:11:15 +00001958 }
1959 // The total cost is the cost of each path scaled by their probabilites
1960 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1961 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1962 UnpredCost = TUnpredCost + FUnpredCost;
1963 // When predicating assume that the first IT can be folded away but later
1964 // ones cost one cycle each
1965 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1966 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1967 }
1968 } else {
1969 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1970 unsigned FUnpredCost =
1971 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1972 UnpredCost = TUnpredCost + FUnpredCost;
1973 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1974 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1975 }
1976
1977 return PredCost <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001978}
1979
Bob Wilsone8a549c2012-09-29 21:43:49 +00001980bool
1981ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1982 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001983 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001984 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001985 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001986}
1987
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001988/// getInstrPredicate - If instruction is predicated, returns its predicate
1989/// condition, otherwise returns AL. It also returns the condition code
1990/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001991ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1992 unsigned &PredReg) {
1993 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001994 if (PIdx == -1) {
1995 PredReg = 0;
1996 return ARMCC::AL;
1997 }
1998
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001999 PredReg = MI.getOperand(PIdx+1).getReg();
2000 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00002001}
2002
Matthias Braunfa3872e2015-05-18 20:27:55 +00002003unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00002004 if (Opc == ARM::B)
2005 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00002006 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00002007 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00002008 if (Opc == ARM::t2B)
2009 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00002010
2011 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00002012}
2013
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002014MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002015 bool NewMI,
2016 unsigned OpIdx1,
2017 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002018 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002019 case ARM::MOVCCr:
2020 case ARM::t2MOVCCr: {
2021 // MOVCC can be commuted by inverting the condition.
2022 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002023 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002024 // MOVCC AL can't be inverted. Shouldn't happen.
2025 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00002026 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002027 MachineInstr *CommutedMI =
2028 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2029 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002030 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002031 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002032 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
2033 .setImm(ARMCC::getOppositeCondition(CC));
2034 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002035 }
2036 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002037 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002038}
Evan Cheng780748d2009-07-28 05:48:47 +00002039
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002040/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002041/// return the defining instruction.
2042static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
2043 const MachineRegisterInfo &MRI,
2044 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002045 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002046 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002047 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002048 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002049 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002050 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002051 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002052 // MI is folded into the MOVCC by predicating it.
2053 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00002054 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002055 // Check if MI has any non-dead defs or physreg uses. This also detects
2056 // predicated instructions which will be reading CPSR.
2057 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
2058 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00002059 // Reject frame index operands, PEI can't handle the predicated pseudos.
2060 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00002061 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002062 if (!MO.isReg())
2063 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002064 // MI can't have any tied operands, that would conflict with predication.
2065 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00002066 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002067 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00002068 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002069 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00002070 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002071 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002072 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00002073 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00002074 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002075 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002076}
2077
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002078bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002079 SmallVectorImpl<MachineOperand> &Cond,
2080 unsigned &TrueOp, unsigned &FalseOp,
2081 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002082 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002083 "Unknown select instruction");
2084 // MOVCC operands:
2085 // 0: Def.
2086 // 1: True use.
2087 // 2: False use.
2088 // 3: Condition code.
2089 // 4: CPSR use.
2090 TrueOp = 1;
2091 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002092 Cond.push_back(MI.getOperand(3));
2093 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002094 // We can always fold a def.
2095 Optimizable = true;
2096 return false;
2097}
2098
Mehdi Amini22e59742015-01-13 07:07:13 +00002099MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002100ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00002101 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2102 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002103 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002104 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002105 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2106 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002107 bool Invert = !DefMI;
2108 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002109 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002110 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002111 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002112
Matthias Braun2f169f92013-10-04 16:52:56 +00002113 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002114 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2115 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00002116 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2117 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00002118 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00002119
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002120 // Create a new predicated version of DefMI.
2121 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002122 MachineInstrBuilder NewMI =
2123 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002124
2125 // Copy all the DefMI operands, excluding its (null) predicate.
2126 const MCInstrDesc &DefDesc = DefMI->getDesc();
2127 for (unsigned i = 1, e = DefDesc.getNumOperands();
2128 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00002129 NewMI.add(DefMI->getOperand(i));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002130
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002131 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002132 if (Invert)
2133 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2134 else
2135 NewMI.addImm(CondCode);
Diana Picus116bbab2017-01-13 09:58:52 +00002136 NewMI.add(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002137
2138 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2139 if (NewMI->hasOptionalDef())
Diana Picus8a73f552017-01-13 10:18:01 +00002140 NewMI.add(condCodeOp());
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002141
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002142 // The output register value when the predicate is false is an implicit
2143 // register operand tied to the first def.
2144 // The tie makes the register allocator ensure the FalseReg is allocated the
2145 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002146 FalseReg.setImplicit();
Diana Picus116bbab2017-01-13 09:58:52 +00002147 NewMI.add(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002148 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2149
Mehdi Amini22e59742015-01-13 07:07:13 +00002150 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2151 SeenMIs.insert(NewMI);
2152 SeenMIs.erase(DefMI);
2153
Pete Cooper2127b002015-04-30 23:57:47 +00002154 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2155 // DefMI would be invalid when tranferred inside the loop. Checking for a
2156 // loop is expensive, but at least remove kill flags if they are in different
2157 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002158 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00002159 NewMI->clearKillInfo();
2160
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002161 // The caller will erase MI, but not DefMI.
2162 DefMI->eraseFromParent();
2163 return NewMI;
2164}
2165
Andrew Trick924123a2011-09-21 02:20:46 +00002166/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2167/// instruction is encoded with an 'S' bit is determined by the optional CPSR
2168/// def operand.
2169///
2170/// This will go away once we can teach tblgen how to set the optional CPSR def
2171/// operand itself.
2172struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00002173 uint16_t PseudoOpc;
2174 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002175};
2176
Craig Topper2fbd1302012-05-24 03:59:11 +00002177static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00002178 {ARM::ADDSri, ARM::ADDri},
2179 {ARM::ADDSrr, ARM::ADDrr},
2180 {ARM::ADDSrsi, ARM::ADDrsi},
2181 {ARM::ADDSrsr, ARM::ADDrsr},
2182
2183 {ARM::SUBSri, ARM::SUBri},
2184 {ARM::SUBSrr, ARM::SUBrr},
2185 {ARM::SUBSrsi, ARM::SUBrsi},
2186 {ARM::SUBSrsr, ARM::SUBrsr},
2187
2188 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00002189 {ARM::RSBSrsi, ARM::RSBrsi},
2190 {ARM::RSBSrsr, ARM::RSBrsr},
2191
Artyom Skrobov92c06532017-03-22 23:35:51 +00002192 {ARM::tADDSi3, ARM::tADDi3},
2193 {ARM::tADDSi8, ARM::tADDi8},
2194 {ARM::tADDSrr, ARM::tADDrr},
2195 {ARM::tADCS, ARM::tADC},
2196
2197 {ARM::tSUBSi3, ARM::tSUBi3},
2198 {ARM::tSUBSi8, ARM::tSUBi8},
2199 {ARM::tSUBSrr, ARM::tSUBrr},
2200 {ARM::tSBCS, ARM::tSBC},
Eli Friedman063fd982018-10-31 21:45:48 +00002201 {ARM::tRSBS, ARM::tRSB},
Artyom Skrobov92c06532017-03-22 23:35:51 +00002202
Andrew Trick924123a2011-09-21 02:20:46 +00002203 {ARM::t2ADDSri, ARM::t2ADDri},
2204 {ARM::t2ADDSrr, ARM::t2ADDrr},
2205 {ARM::t2ADDSrs, ARM::t2ADDrs},
2206
2207 {ARM::t2SUBSri, ARM::t2SUBri},
2208 {ARM::t2SUBSrr, ARM::t2SUBrr},
2209 {ARM::t2SUBSrs, ARM::t2SUBrs},
2210
2211 {ARM::t2RSBSri, ARM::t2RSBri},
2212 {ARM::t2RSBSrs, ARM::t2RSBrs},
2213};
2214
2215unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00002216 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2217 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2218 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002219 return 0;
2220}
2221
Evan Cheng780748d2009-07-28 05:48:47 +00002222void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002223 MachineBasicBlock::iterator &MBBI,
2224 const DebugLoc &dl, unsigned DestReg,
2225 unsigned BaseReg, int NumBytes,
2226 ARMCC::CondCodes Pred, unsigned PredReg,
2227 const ARMBaseInstrInfo &TII,
2228 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002229 if (NumBytes == 0 && DestReg != BaseReg) {
2230 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002231 .addReg(BaseReg, RegState::Kill)
2232 .add(predOps(Pred, PredReg))
2233 .add(condCodeOp())
2234 .setMIFlags(MIFlags);
Tim Northoverc9432eb2013-11-04 23:04:15 +00002235 return;
2236 }
2237
Evan Cheng780748d2009-07-28 05:48:47 +00002238 bool isSub = NumBytes < 0;
2239 if (isSub) NumBytes = -NumBytes;
2240
2241 while (NumBytes) {
2242 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2243 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2244 assert(ThisVal && "Didn't extract field correctly");
2245
2246 // We will handle these bits from offset, clear them.
2247 NumBytes &= ~ThisVal;
2248
2249 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2250
2251 // Build the new ADD / SUB.
2252 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2253 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002254 .addReg(BaseReg, RegState::Kill)
2255 .addImm(ThisVal)
2256 .add(predOps(Pred, PredReg))
2257 .add(condCodeOp())
2258 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002259 BaseReg = DestReg;
2260 }
2261}
2262
Tim Northoverdee86042013-12-02 14:46:26 +00002263bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2264 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002265 unsigned NumBytes) {
2266 // This optimisation potentially adds lots of load and store
2267 // micro-operations, it's only really a great benefit to code-size.
Sam Parker5b098342019-02-08 07:57:42 +00002268 if (!Subtarget.optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002269 return false;
2270
2271 // If only one register is pushed/popped, LLVM can use an LDR/STR
2272 // instead. We can't modify those so make sure we're dealing with an
2273 // instruction we understand.
2274 bool IsPop = isPopOpcode(MI->getOpcode());
2275 bool IsPush = isPushOpcode(MI->getOpcode());
2276 if (!IsPush && !IsPop)
2277 return false;
2278
2279 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2280 MI->getOpcode() == ARM::VLDMDIA_UPD;
2281 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2282 MI->getOpcode() == ARM::tPOP ||
2283 MI->getOpcode() == ARM::tPOP_RET;
2284
2285 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2286 MI->getOperand(1).getReg() == ARM::SP)) &&
2287 "trying to fold sp update into non-sp-updating push/pop");
2288
2289 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2290 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2291 // if this is violated.
2292 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2293 return false;
2294
2295 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2296 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2297 int RegListIdx = IsT1PushPop ? 2 : 4;
2298
2299 // Calculate the space we'll need in terms of registers.
Tim Northovera9cc3852016-10-26 20:01:00 +00002300 unsigned RegsNeeded;
2301 const TargetRegisterClass *RegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002302 if (IsVFPPushPop) {
Tim Northover93bcc662013-11-08 17:18:07 +00002303 RegsNeeded = NumBytes / 8;
Tim Northovera9cc3852016-10-26 20:01:00 +00002304 RegClass = &ARM::DPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002305 } else {
Tim Northover93bcc662013-11-08 17:18:07 +00002306 RegsNeeded = NumBytes / 4;
Tim Northovera9cc3852016-10-26 20:01:00 +00002307 RegClass = &ARM::GPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002308 }
2309
2310 // We're going to have to strip all list operands off before
2311 // re-adding them since the order matters, so save the existing ones
2312 // for later.
2313 SmallVector<MachineOperand, 4> RegList;
Tim Northovera9cc3852016-10-26 20:01:00 +00002314
2315 // We're also going to need the first register transferred by this
2316 // instruction, which won't necessarily be the first register in the list.
2317 unsigned FirstRegEnc = -1;
Tim Northover93bcc662013-11-08 17:18:07 +00002318
Tim Northover93bcc662013-11-08 17:18:07 +00002319 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northovera9cc3852016-10-26 20:01:00 +00002320 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2321 MachineOperand &MO = MI->getOperand(i);
2322 RegList.push_back(MO);
2323
2324 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2325 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2326 }
2327
Tim Northover45479dc2013-12-01 14:16:24 +00002328 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002329
2330 // Now try to find enough space in the reglist to allocate NumBytes.
Tim Northovera9cc3852016-10-26 20:01:00 +00002331 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2332 --CurRegEnc) {
2333 unsigned CurReg = RegClass->getRegister(CurRegEnc);
Tim Northover93bcc662013-11-08 17:18:07 +00002334 if (!IsPop) {
Momchil Velikovac7c5c12018-01-08 14:47:19 +00002335 // Pushing any register is completely harmless, mark the register involved
2336 // as undef since we don't care about its value and must not restore it
2337 // during stack unwinding.
Tim Northover93bcc662013-11-08 17:18:07 +00002338 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2339 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002340 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002341 continue;
2342 }
2343
Tim Northover45479dc2013-12-01 14:16:24 +00002344 // However, we can only pop an extra register if it's not live. For
2345 // registers live within the function we might clobber a return value
2346 // register; the other way a register can be live here is if it's
2347 // callee-saved.
2348 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002349 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2350 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002351 // VFP pops don't allow holes in the register list, so any skip is fatal
2352 // for our transformation. GPR pops do, so we should just keep looking.
2353 if (IsVFPPushPop)
2354 return false;
2355 else
2356 continue;
2357 }
Tim Northover93bcc662013-11-08 17:18:07 +00002358
2359 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002360 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2361 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002362 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002363 }
2364
2365 if (RegsNeeded > 0)
2366 return false;
2367
2368 // Finally we know we can profitably perform the optimisation so go
2369 // ahead: strip all existing registers off and add them back again
2370 // in the right order.
2371 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2372 MI->RemoveOperand(i);
2373
2374 // Add the complete list back in.
2375 MachineInstrBuilder MIB(MF, &*MI);
2376 for (int i = RegList.size() - 1; i >= 0; --i)
Diana Picus116bbab2017-01-13 09:58:52 +00002377 MIB.add(RegList[i]);
Tim Northover93bcc662013-11-08 17:18:07 +00002378
2379 return true;
2380}
2381
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002382bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2383 unsigned FrameReg, int &Offset,
2384 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002385 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002386 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002387 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2388 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002389
Evan Cheng780748d2009-07-28 05:48:47 +00002390 // Memory operands in inline assembly always use AddrMode2.
2391 if (Opcode == ARM::INLINEASM)
2392 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002393
Evan Cheng780748d2009-07-28 05:48:47 +00002394 if (Opcode == ARM::ADDri) {
2395 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2396 if (Offset == 0) {
2397 // Turn it into a move.
2398 MI.setDesc(TII.get(ARM::MOVr));
2399 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2400 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002401 Offset = 0;
2402 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002403 } else if (Offset < 0) {
2404 Offset = -Offset;
2405 isSub = true;
2406 MI.setDesc(TII.get(ARM::SUBri));
2407 }
2408
2409 // Common case: small offset, fits into instruction.
2410 if (ARM_AM::getSOImmVal(Offset) != -1) {
2411 // Replace the FrameIndex with sp / fp
2412 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2413 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002414 Offset = 0;
2415 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002416 }
2417
2418 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2419 // as possible.
2420 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2421 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2422
2423 // We will handle these bits from offset, clear them.
2424 Offset &= ~ThisImmVal;
2425
2426 // Get the properly encoded SOImmVal field.
2427 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2428 "Bit extraction didn't work?");
2429 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2430 } else {
2431 unsigned ImmIdx = 0;
2432 int InstrOffs = 0;
2433 unsigned NumBits = 0;
2434 unsigned Scale = 1;
2435 switch (AddrMode) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002436 case ARMII::AddrMode_i12:
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002437 ImmIdx = FrameRegIdx + 1;
2438 InstrOffs = MI.getOperand(ImmIdx).getImm();
2439 NumBits = 12;
2440 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002441 case ARMII::AddrMode2:
Evan Cheng780748d2009-07-28 05:48:47 +00002442 ImmIdx = FrameRegIdx+2;
2443 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2444 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2445 InstrOffs *= -1;
2446 NumBits = 12;
2447 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002448 case ARMII::AddrMode3:
Evan Cheng780748d2009-07-28 05:48:47 +00002449 ImmIdx = FrameRegIdx+2;
2450 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2451 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2452 InstrOffs *= -1;
2453 NumBits = 8;
2454 break;
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002455 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002456 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002457 // Can't fold any offset even if it's zero.
2458 return false;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002459 case ARMII::AddrMode5:
Evan Cheng780748d2009-07-28 05:48:47 +00002460 ImmIdx = FrameRegIdx+1;
2461 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2462 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2463 InstrOffs *= -1;
2464 NumBits = 8;
2465 Scale = 4;
2466 break;
Sjoerd Meijer011de9c2018-01-26 09:26:40 +00002467 case ARMII::AddrMode5FP16:
2468 ImmIdx = FrameRegIdx+1;
2469 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2470 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2471 InstrOffs *= -1;
2472 NumBits = 8;
2473 Scale = 2;
2474 break;
Evan Cheng780748d2009-07-28 05:48:47 +00002475 default:
2476 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002477 }
2478
2479 Offset += InstrOffs * Scale;
2480 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2481 if (Offset < 0) {
2482 Offset = -Offset;
2483 isSub = true;
2484 }
2485
2486 // Attempt to fold address comp. if opcode has offset bits
2487 if (NumBits > 0) {
2488 // Common case: small offset, fits into instruction.
2489 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2490 int ImmedOffset = Offset / Scale;
2491 unsigned Mask = (1 << NumBits) - 1;
2492 if ((unsigned)Offset <= Mask * Scale) {
2493 // Replace the FrameIndex with sp
2494 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002495 // FIXME: When addrmode2 goes away, this will simplify (like the
2496 // T2 version), as the LDR.i12 versions don't need the encoding
2497 // tricks for the offset value.
2498 if (isSub) {
2499 if (AddrMode == ARMII::AddrMode_i12)
2500 ImmedOffset = -ImmedOffset;
2501 else
2502 ImmedOffset |= 1 << NumBits;
2503 }
Evan Cheng780748d2009-07-28 05:48:47 +00002504 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002505 Offset = 0;
2506 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002507 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002508
Evan Cheng780748d2009-07-28 05:48:47 +00002509 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2510 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002511 if (isSub) {
2512 if (AddrMode == ARMII::AddrMode_i12)
2513 ImmedOffset = -ImmedOffset;
2514 else
2515 ImmedOffset |= 1 << NumBits;
2516 }
Evan Cheng780748d2009-07-28 05:48:47 +00002517 ImmOp.ChangeToImmediate(ImmedOffset);
2518 Offset &= ~(Mask*Scale);
2519 }
2520 }
2521
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002522 Offset = (isSub) ? -Offset : Offset;
2523 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002524}
Bill Wendling7de9d522010-08-06 01:32:48 +00002525
Manman Ren6fa76dc2012-06-29 21:33:59 +00002526/// analyzeCompare - For a comparison instruction, return the source registers
2527/// in SrcReg and SrcReg2 if having two register operands, and the value it
2528/// compares against in CmpValue. Return true if the comparison instruction
2529/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002530bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2531 unsigned &SrcReg2, int &CmpMask,
2532 int &CmpValue) const {
2533 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002534 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002535 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002536 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002537 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002538 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002539 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002540 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002541 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002542 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002543 case ARM::CMPrr:
2544 case ARM::t2CMPrr:
David Greenacb628b22019-02-22 12:23:31 +00002545 case ARM::tCMPr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002546 SrcReg = MI.getOperand(0).getReg();
2547 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002548 CmpMask = ~0;
2549 CmpValue = 0;
2550 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002551 case ARM::TSTri:
2552 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002553 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002554 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002555 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002556 CmpValue = 0;
2557 return true;
2558 }
2559
2560 return false;
2561}
2562
Gabor Greifd36e3e82010-09-29 10:12:08 +00002563/// isSuitableForMask - Identify a suitable 'and' instruction that
2564/// operates on the given source register and applies the same mask
2565/// as a 'tst' instruction. Provide a limited look-through for copies.
2566/// When successful, MI will hold the found instruction.
2567static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002568 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002569 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002570 case ARM::ANDri:
2571 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002572 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002573 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002574 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002575 return true;
2576 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002577 }
2578
2579 return false;
2580}
2581
Manman Renb1b3db62012-06-29 22:06:19 +00002582/// getSwappedCondition - assume the flags are set by MI(a,b), return
2583/// the condition code if we modify the instructions such that flags are
2584/// set by MI(b,a).
2585inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2586 switch (CC) {
2587 default: return ARMCC::AL;
2588 case ARMCC::EQ: return ARMCC::EQ;
2589 case ARMCC::NE: return ARMCC::NE;
2590 case ARMCC::HS: return ARMCC::LS;
2591 case ARMCC::LO: return ARMCC::HI;
2592 case ARMCC::HI: return ARMCC::LO;
2593 case ARMCC::LS: return ARMCC::HS;
2594 case ARMCC::GE: return ARMCC::LE;
2595 case ARMCC::LT: return ARMCC::GT;
2596 case ARMCC::GT: return ARMCC::LT;
2597 case ARMCC::LE: return ARMCC::GE;
2598 }
2599}
2600
Joel Galensonfe7fa402018-01-17 19:19:05 +00002601/// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2602/// the condition code if we modify the instructions such that flags are
2603/// set by ADD(a,b,X).
2604inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
2605 switch (CC) {
2606 default: return ARMCC::AL;
2607 case ARMCC::HS: return ARMCC::LO;
2608 case ARMCC::LO: return ARMCC::HS;
2609 case ARMCC::VS: return ARMCC::VS;
2610 case ARMCC::VC: return ARMCC::VC;
2611 }
2612}
2613
Manman Renb1b3db62012-06-29 22:06:19 +00002614/// isRedundantFlagInstr - check whether the first instruction, whose only
2615/// purpose is to update flags, can be made redundant.
2616/// CMPrr can be made redundant by SUBrr if the operands are the same.
2617/// CMPri can be made redundant by SUBri if the operands are the same.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002618/// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
Manman Renb1b3db62012-06-29 22:06:19 +00002619/// This function can be extended later on.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002620inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
2621 unsigned SrcReg, unsigned SrcReg2,
David Greenacb628b22019-02-22 12:23:31 +00002622 int ImmValue, const MachineInstr *OI,
2623 bool &IsThumb1) {
David Green7e6da812019-02-07 10:51:04 +00002624 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2625 (OI->getOpcode() == ARM::SUBrr || OI->getOpcode() == ARM::t2SUBrr) &&
Manman Renb1b3db62012-06-29 22:06:19 +00002626 ((OI->getOperand(1).getReg() == SrcReg &&
2627 OI->getOperand(2).getReg() == SrcReg2) ||
2628 (OI->getOperand(1).getReg() == SrcReg2 &&
David Greenacb628b22019-02-22 12:23:31 +00002629 OI->getOperand(2).getReg() == SrcReg))) {
2630 IsThumb1 = false;
Manman Renb1b3db62012-06-29 22:06:19 +00002631 return true;
David Greenacb628b22019-02-22 12:23:31 +00002632 }
2633
2634 if (CmpI->getOpcode() == ARM::tCMPr && OI->getOpcode() == ARM::tSUBrr &&
2635 ((OI->getOperand(2).getReg() == SrcReg &&
2636 OI->getOperand(3).getReg() == SrcReg2) ||
2637 (OI->getOperand(2).getReg() == SrcReg2 &&
2638 OI->getOperand(3).getReg() == SrcReg))) {
2639 IsThumb1 = true;
2640 return true;
2641 }
Manman Renb1b3db62012-06-29 22:06:19 +00002642
David Green7e6da812019-02-07 10:51:04 +00002643 if ((CmpI->getOpcode() == ARM::CMPri || CmpI->getOpcode() == ARM::t2CMPri) &&
2644 (OI->getOpcode() == ARM::SUBri || OI->getOpcode() == ARM::t2SUBri) &&
Manman Renb1b3db62012-06-29 22:06:19 +00002645 OI->getOperand(1).getReg() == SrcReg &&
David Greenacb628b22019-02-22 12:23:31 +00002646 OI->getOperand(2).getImm() == ImmValue) {
2647 IsThumb1 = false;
Manman Renb1b3db62012-06-29 22:06:19 +00002648 return true;
David Greenacb628b22019-02-22 12:23:31 +00002649 }
2650
2651 if (CmpI->getOpcode() == ARM::tCMPi8 &&
2652 (OI->getOpcode() == ARM::tSUBi8 || OI->getOpcode() == ARM::tSUBi3) &&
2653 OI->getOperand(2).getReg() == SrcReg &&
2654 OI->getOperand(3).getImm() == ImmValue) {
2655 IsThumb1 = true;
2656 return true;
2657 }
Joel Galensonfe7fa402018-01-17 19:19:05 +00002658
2659 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2660 (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
2661 OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
David Green7a183a82019-02-21 11:03:13 +00002662 OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
Joel Galensonfe7fa402018-01-17 19:19:05 +00002663 OI->getOperand(0).getReg() == SrcReg &&
David Greenacb628b22019-02-22 12:23:31 +00002664 OI->getOperand(1).getReg() == SrcReg2) {
2665 IsThumb1 = false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002666 return true;
David Greenacb628b22019-02-22 12:23:31 +00002667 }
2668
2669 if (CmpI->getOpcode() == ARM::tCMPr &&
2670 (OI->getOpcode() == ARM::tADDi3 || OI->getOpcode() == ARM::tADDi8 ||
2671 OI->getOpcode() == ARM::tADDrr) &&
2672 OI->getOperand(0).getReg() == SrcReg &&
2673 OI->getOperand(2).getReg() == SrcReg2) {
2674 IsThumb1 = true;
2675 return true;
2676 }
2677
Manman Renb1b3db62012-06-29 22:06:19 +00002678 return false;
2679}
2680
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002681static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2682 switch (MI->getOpcode()) {
2683 default: return false;
2684 case ARM::tLSLri:
2685 case ARM::tLSRri:
2686 case ARM::tLSLrr:
2687 case ARM::tLSRrr:
2688 case ARM::tSUBrr:
2689 case ARM::tADDrr:
2690 case ARM::tADDi3:
2691 case ARM::tADDi8:
2692 case ARM::tSUBi3:
2693 case ARM::tSUBi8:
2694 case ARM::tMUL:
David Greenb504f102019-02-25 15:50:54 +00002695 case ARM::tADC:
2696 case ARM::tSBC:
2697 case ARM::tRSB:
2698 case ARM::tAND:
2699 case ARM::tORR:
2700 case ARM::tEOR:
2701 case ARM::tBIC:
2702 case ARM::tMVN:
2703 case ARM::tASRri:
2704 case ARM::tASRrr:
2705 case ARM::tROR:
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002706 IsThumb1 = true;
2707 LLVM_FALLTHROUGH;
2708 case ARM::RSBrr:
2709 case ARM::RSBri:
2710 case ARM::RSCrr:
2711 case ARM::RSCri:
2712 case ARM::ADDrr:
2713 case ARM::ADDri:
2714 case ARM::ADCrr:
2715 case ARM::ADCri:
2716 case ARM::SUBrr:
2717 case ARM::SUBri:
2718 case ARM::SBCrr:
2719 case ARM::SBCri:
2720 case ARM::t2RSBri:
2721 case ARM::t2ADDrr:
2722 case ARM::t2ADDri:
2723 case ARM::t2ADCrr:
2724 case ARM::t2ADCri:
2725 case ARM::t2SUBrr:
2726 case ARM::t2SUBri:
2727 case ARM::t2SBCrr:
2728 case ARM::t2SBCri:
2729 case ARM::ANDrr:
2730 case ARM::ANDri:
2731 case ARM::t2ANDrr:
2732 case ARM::t2ANDri:
2733 case ARM::ORRrr:
2734 case ARM::ORRri:
2735 case ARM::t2ORRrr:
2736 case ARM::t2ORRri:
2737 case ARM::EORrr:
2738 case ARM::EORri:
2739 case ARM::t2EORrr:
2740 case ARM::t2EORri:
2741 case ARM::t2LSRri:
2742 case ARM::t2LSRrr:
2743 case ARM::t2LSLri:
2744 case ARM::t2LSLrr:
2745 return true;
2746 }
2747}
2748
Manman Ren6fa76dc2012-06-29 21:33:59 +00002749/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2750/// comparison into one that sets the zero bit in the flags register;
2751/// Remove a redundant Compare instruction if an earlier instruction can set the
2752/// flags in the same way as Compare.
2753/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2754/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2755/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002756bool ARMBaseInstrInfo::optimizeCompareInstr(
2757 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2758 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002759 // Get the unique definition of SrcReg.
2760 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2761 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002762
Gabor Greifadbbb932010-09-21 12:01:15 +00002763 // Masked compares sometimes use the same register as the corresponding 'and'.
2764 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002765 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002766 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002767 for (MachineRegisterInfo::use_instr_iterator
2768 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2769 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002770 if (UI->getParent() != CmpInstr.getParent())
2771 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002772 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002773 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002774 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002775 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002776 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002777 break;
2778 }
2779 if (!MI) return false;
2780 }
2781 }
2782
Manman Rendc8ad002012-05-11 01:30:47 +00002783 // Get ready to iterate backward from CmpInstr.
2784 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002785 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002786
2787 // Early exit if CmpInstr is at the beginning of the BB.
2788 if (I == B) return false;
2789
Manman Rendc8ad002012-05-11 01:30:47 +00002790 // There are two possible candidates which can be changed to set CPSR:
Joel Galensonfe7fa402018-01-17 19:19:05 +00002791 // One is MI, the other is a SUB or ADD instruction.
2792 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
2793 // ADDr[ri](r1, r2, X).
Manman Rendc8ad002012-05-11 01:30:47 +00002794 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Joel Galensonfe7fa402018-01-17 19:19:05 +00002795 MachineInstr *SubAdd = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002796 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002797 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002798 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002799 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002800 // Conservatively refuse to convert an instruction which isn't in the same
2801 // BB as the comparison.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002802 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002803 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002804 if (CmpInstr.getOpcode() == ARM::CMPri ||
David Greenacb628b22019-02-22 12:23:31 +00002805 CmpInstr.getOpcode() == ARM::t2CMPri ||
2806 CmpInstr.getOpcode() == ARM::tCMPi8)
Craig Topper062a2ba2014-04-25 05:30:21 +00002807 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002808 else
2809 return false;
2810 }
2811
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002812 bool IsThumb1 = false;
2813 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2814 return false;
2815
2816 // We also want to do this peephole for cases like this: if (a*b == 0),
2817 // and optimise away the CMP instruction from the generated code sequence:
2818 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2819 // resulting from the select instruction, but these MOVS instructions for
2820 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2821 // However, if we only have MOVS instructions in between the CMP and the
2822 // other instruction (the MULS in this example), then the CPSR is dead so we
2823 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2824 // reordering and then continue the analysis hoping we can eliminate the
2825 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2826 // consequence, the movs won't redefine/kill the MUL operands which would
2827 // make this reordering illegal.
David Greenb504f102019-02-25 15:50:54 +00002828 const TargetRegisterInfo *TRI = &getRegisterInfo();
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002829 if (MI && IsThumb1) {
2830 --I;
David Greenb504f102019-02-25 15:50:54 +00002831 if (I != E && !MI->readsRegister(ARM::CPSR, TRI)) {
2832 bool CanReorder = true;
2833 for (; I != E; --I) {
2834 if (I->getOpcode() != ARM::tMOVi8) {
2835 CanReorder = false;
2836 break;
2837 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002838 }
David Greenb504f102019-02-25 15:50:54 +00002839 if (CanReorder) {
2840 MI = MI->removeFromParent();
2841 E = CmpInstr;
2842 CmpInstr.getParent()->insert(E, MI);
2843 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002844 }
2845 I = CmpInstr;
2846 E = MI;
2847 }
2848
Manman Rendc8ad002012-05-11 01:30:47 +00002849 // Check that CPSR isn't set between the comparison instruction and the one we
Joel Galensonfe7fa402018-01-17 19:19:05 +00002850 // want to change. At the same time, search for SubAdd.
David Greenacb628b22019-02-22 12:23:31 +00002851 bool SubAddIsThumb1 = false;
Joel Galenson1d89cd22018-01-22 17:53:47 +00002852 do {
2853 const MachineInstr &Instr = *--I;
Bill Wendling7de9d522010-08-06 01:32:48 +00002854
Joel Galensonfe7fa402018-01-17 19:19:05 +00002855 // Check whether CmpInstr can be made redundant by the current instruction.
David Greenacb628b22019-02-22 12:23:31 +00002856 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr,
2857 SubAddIsThumb1)) {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002858 SubAdd = &*I;
2859 break;
2860 }
2861
Joel Galenson1d89cd22018-01-22 17:53:47 +00002862 // Allow E (which was initially MI) to be SubAdd but do not search before E.
2863 if (I == E)
2864 break;
2865
Manman Renb1b3db62012-06-29 22:06:19 +00002866 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2867 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002868 // This instruction modifies or uses CPSR after the one we want to
2869 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002870 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002871
Joel Galenson1d89cd22018-01-22 17:53:47 +00002872 } while (I != B);
Bill Wendling7de9d522010-08-06 01:32:48 +00002873
Manman Rendc8ad002012-05-11 01:30:47 +00002874 // Return false if no candidates exist.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002875 if (!MI && !SubAdd)
Manman Rendc8ad002012-05-11 01:30:47 +00002876 return false;
2877
David Green743abf22019-02-14 11:09:24 +00002878 // If we found a SubAdd, use it as it will be closer to the CMP
2879 if (SubAdd) {
2880 MI = SubAdd;
David Greenacb628b22019-02-22 12:23:31 +00002881 IsThumb1 = SubAddIsThumb1;
David Green743abf22019-02-14 11:09:24 +00002882 }
Manman Rendc8ad002012-05-11 01:30:47 +00002883
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002884 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002885 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002886 return false;
2887
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002888 // Scan forward for the use of CPSR
2889 // When checking against MI: if it's a conditional code that requires
2890 // checking of the V bit or C bit, then this is not safe to do.
2891 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2892 // If we are done with the basic block, we need to check whether CPSR is
2893 // live-out.
2894 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2895 OperandsToUpdate;
2896 bool isSafe = false;
2897 I = CmpInstr;
2898 E = CmpInstr.getParent()->end();
2899 while (!isSafe && ++I != E) {
2900 const MachineInstr &Instr = *I;
2901 for (unsigned IO = 0, EO = Instr.getNumOperands();
2902 !isSafe && IO != EO; ++IO) {
2903 const MachineOperand &MO = Instr.getOperand(IO);
2904 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2905 isSafe = true;
2906 break;
2907 }
2908 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2909 continue;
2910 if (MO.isDef()) {
2911 isSafe = true;
2912 break;
2913 }
2914 // Condition code is after the operand before CPSR except for VSELs.
2915 ARMCC::CondCodes CC;
2916 bool IsInstrVSel = true;
2917 switch (Instr.getOpcode()) {
2918 default:
2919 IsInstrVSel = false;
2920 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2921 break;
2922 case ARM::VSELEQD:
2923 case ARM::VSELEQS:
2924 CC = ARMCC::EQ;
2925 break;
2926 case ARM::VSELGTD:
2927 case ARM::VSELGTS:
2928 CC = ARMCC::GT;
2929 break;
2930 case ARM::VSELGED:
2931 case ARM::VSELGES:
2932 CC = ARMCC::GE;
2933 break;
2934 case ARM::VSELVSS:
2935 case ARM::VSELVSD:
2936 CC = ARMCC::VS;
2937 break;
2938 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002939
Joel Galensonfe7fa402018-01-17 19:19:05 +00002940 if (SubAdd) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002941 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2942 // on CMP needs to be updated to be based on SUB.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002943 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
2944 // needs to be modified.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002945 // Push the condition code operands to OperandsToUpdate.
2946 // If it is safe to remove CmpInstr, the condition code of these
2947 // operands will be modified.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002948 unsigned Opc = SubAdd->getOpcode();
2949 bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
David Greenacb628b22019-02-22 12:23:31 +00002950 Opc == ARM::SUBri || Opc == ARM::t2SUBri ||
2951 Opc == ARM::tSUBrr || Opc == ARM::tSUBi3 ||
2952 Opc == ARM::tSUBi8;
2953 unsigned OpI = Opc != ARM::tSUBrr ? 1 : 2;
2954 if (!IsSub ||
2955 (SrcReg2 != 0 && SubAdd->getOperand(OpI).getReg() == SrcReg2 &&
2956 SubAdd->getOperand(OpI + 1).getReg() == SrcReg)) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002957 // VSel doesn't support condition code update.
2958 if (IsInstrVSel)
Manman Rendc8ad002012-05-11 01:30:47 +00002959 return false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002960 // Ensure we can swap the condition.
2961 ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
2962 if (NewCC == ARMCC::AL)
2963 return false;
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002964 OperandsToUpdate.push_back(
2965 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2966 }
2967 } else {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002968 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002969 switch (CC) {
2970 case ARMCC::EQ: // Z
2971 case ARMCC::NE: // Z
2972 case ARMCC::MI: // N
2973 case ARMCC::PL: // N
2974 case ARMCC::AL: // none
2975 // CPSR can be used multiple times, we should continue.
2976 break;
2977 case ARMCC::HS: // C
2978 case ARMCC::LO: // C
2979 case ARMCC::VS: // V
2980 case ARMCC::VC: // V
2981 case ARMCC::HI: // C Z
2982 case ARMCC::LS: // C Z
2983 case ARMCC::GE: // N V
2984 case ARMCC::LT: // N V
2985 case ARMCC::GT: // Z N V
2986 case ARMCC::LE: // Z N V
2987 // The instruction uses the V bit or C bit which is not safe.
2988 return false;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002989 }
Evan Cheng425489d2011-03-23 22:52:04 +00002990 }
2991 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002992 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002993
2994 // If CPSR is not killed nor re-defined, we should check whether it is
2995 // live-out. If it is live-out, do not optimize.
2996 if (!isSafe) {
2997 MachineBasicBlock *MBB = CmpInstr.getParent();
2998 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2999 SE = MBB->succ_end(); SI != SE; ++SI)
3000 if ((*SI)->isLiveIn(ARM::CPSR))
3001 return false;
Cameron Zwarich0829b302011-04-15 20:45:00 +00003002 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003003
3004 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
3005 // set CPSR so this is represented as an explicit output)
3006 if (!IsThumb1) {
3007 MI->getOperand(5).setReg(ARM::CPSR);
3008 MI->getOperand(5).setIsDef(true);
3009 }
3010 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
3011 CmpInstr.eraseFromParent();
3012
3013 // Modify the condition code of operands in OperandsToUpdate.
3014 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
3015 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
3016 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
3017 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
3018
Eli Friedman2ac11622018-10-26 19:32:24 +00003019 MI->clearRegisterDeads(ARM::CPSR);
3020
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003021 return true;
Bill Wendling7de9d522010-08-06 01:32:48 +00003022}
Evan Cheng367a5df2010-09-09 18:18:55 +00003023
Joel Galensonfe7fa402018-01-17 19:19:05 +00003024bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
3025 // Do not sink MI if it might be used to optimize a redundant compare.
3026 // We heuristically only look at the instruction immediately following MI to
3027 // avoid potentially searching the entire basic block.
3028 if (isPredicated(MI))
3029 return true;
3030 MachineBasicBlock::const_iterator Next = &MI;
3031 ++Next;
3032 unsigned SrcReg, SrcReg2;
3033 int CmpMask, CmpValue;
David Greenacb628b22019-02-22 12:23:31 +00003034 bool IsThumb1;
Joel Galensonfe7fa402018-01-17 19:19:05 +00003035 if (Next != MI.getParent()->end() &&
3036 analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
David Greenacb628b22019-02-22 12:23:31 +00003037 isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI, IsThumb1))
Joel Galensonfe7fa402018-01-17 19:19:05 +00003038 return false;
3039 return true;
3040}
3041
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003042bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
3043 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003044 MachineRegisterInfo *MRI) const {
3045 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003046 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003047 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
3048 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003049 if (!DefMI.getOperand(1).isImm())
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00003050 // Could be t2MOVi32imm @xx
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003051 return false;
3052
3053 if (!MRI->hasOneNonDBGUse(Reg))
3054 return false;
3055
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003056 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00003057 if (DefMCID.hasOptionalDef()) {
3058 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003059 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00003060 if (MO.getReg() == ARM::CPSR && !MO.isDead())
3061 // If DefMI defines CPSR and it is not dead, it's obviously not safe
3062 // to delete DefMI.
3063 return false;
3064 }
3065
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003066 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00003067 if (UseMCID.hasOptionalDef()) {
3068 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003069 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00003070 // If the instruction sets the flag, do not attempt this optimization
3071 // since it may change the semantics of the code.
3072 return false;
3073 }
3074
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003075 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003076 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003077 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003078 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003079 bool Commute = false;
3080 switch (UseOpc) {
3081 default: return false;
3082 case ARM::SUBrr:
3083 case ARM::ADDrr:
3084 case ARM::ORRrr:
3085 case ARM::EORrr:
3086 case ARM::t2SUBrr:
3087 case ARM::t2ADDrr:
3088 case ARM::t2ORRrr:
3089 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003090 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003091 switch (UseOpc) {
3092 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003093 case ARM::ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003094 case ARM::SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003095 if (UseOpc == ARM::SUBrr && Commute)
3096 return false;
3097
3098 // ADD/SUB are special because they're essentially the same operation, so
3099 // we can handle a larger range of immediates.
3100 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
3101 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
3102 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
3103 ImmVal = -ImmVal;
3104 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
3105 } else
3106 return false;
3107 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3108 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3109 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003110 case ARM::ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003111 case ARM::EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003112 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
3113 return false;
3114 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3115 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3116 switch (UseOpc) {
3117 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003118 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
3119 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
3120 }
3121 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003122 case ARM::t2ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003123 case ARM::t2SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003124 if (UseOpc == ARM::t2SUBrr && Commute)
3125 return false;
3126
3127 // ADD/SUB are special because they're essentially the same operation, so
3128 // we can handle a larger range of immediates.
3129 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3130 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
3131 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
3132 ImmVal = -ImmVal;
3133 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
3134 } else
3135 return false;
3136 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3137 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3138 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003139 case ARM::t2ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003140 case ARM::t2EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003141 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3142 return false;
3143 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3144 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3145 switch (UseOpc) {
3146 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003147 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
3148 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
3149 }
3150 break;
3151 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003152 }
3153 }
3154
3155 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003156 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
3157 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003158 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Diana Picus8a73f552017-01-13 10:18:01 +00003159 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
3160 NewReg)
3161 .addReg(Reg1, getKillRegState(isKill))
3162 .addImm(SOImmValV1)
3163 .add(predOps(ARMCC::AL))
3164 .add(condCodeOp());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003165 UseMI.setDesc(get(NewUseOpc));
3166 UseMI.getOperand(1).setReg(NewReg);
3167 UseMI.getOperand(1).setIsKill();
3168 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
3169 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003170 return true;
3171}
3172
Bob Wilsone8a549c2012-09-29 21:43:49 +00003173static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003174 const MachineInstr &MI) {
3175 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003176 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003177 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003178 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
3179 assert(UOps >= 0 && "bad # UOps");
3180 return UOps;
3181 }
3182
3183 case ARM::LDRrs:
3184 case ARM::LDRBrs:
3185 case ARM::STRrs:
3186 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003187 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003188 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3189 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3190 if (!isSub &&
3191 (ShImm == 0 ||
3192 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3193 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3194 return 1;
3195 return 2;
3196 }
3197
3198 case ARM::LDRH:
3199 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003200 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00003201 return 1;
3202
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003203 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003204 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3205 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3206 if (!isSub &&
3207 (ShImm == 0 ||
3208 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3209 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3210 return 1;
3211 return 2;
3212 }
3213
3214 case ARM::LDRSB:
3215 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003216 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003217
3218 case ARM::LDRSB_POST:
3219 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003220 unsigned Rt = MI.getOperand(0).getReg();
3221 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003222 return (Rt == Rm) ? 4 : 3;
3223 }
3224
3225 case ARM::LDR_PRE_REG:
3226 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003227 unsigned Rt = MI.getOperand(0).getReg();
3228 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003229 if (Rt == Rm)
3230 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003231 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003232 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3233 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3234 if (!isSub &&
3235 (ShImm == 0 ||
3236 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3237 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3238 return 2;
3239 return 3;
3240 }
3241
3242 case ARM::STR_PRE_REG:
3243 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003244 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003245 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3246 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3247 if (!isSub &&
3248 (ShImm == 0 ||
3249 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3250 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3251 return 2;
3252 return 3;
3253 }
3254
3255 case ARM::LDRH_PRE:
3256 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003257 unsigned Rt = MI.getOperand(0).getReg();
3258 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003259 if (!Rm)
3260 return 2;
3261 if (Rt == Rm)
3262 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003263 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003264 }
3265
3266 case ARM::LDR_POST_REG:
3267 case ARM::LDRB_POST_REG:
3268 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003269 unsigned Rt = MI.getOperand(0).getReg();
3270 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003271 return (Rt == Rm) ? 3 : 2;
3272 }
3273
3274 case ARM::LDR_PRE_IMM:
3275 case ARM::LDRB_PRE_IMM:
3276 case ARM::LDR_POST_IMM:
3277 case ARM::LDRB_POST_IMM:
3278 case ARM::STRB_POST_IMM:
3279 case ARM::STRB_POST_REG:
3280 case ARM::STRB_PRE_IMM:
3281 case ARM::STRH_POST:
3282 case ARM::STR_POST_IMM:
3283 case ARM::STR_POST_REG:
3284 case ARM::STR_PRE_IMM:
3285 return 2;
3286
3287 case ARM::LDRSB_PRE:
3288 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003289 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003290 if (Rm == 0)
3291 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003292 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003293 if (Rt == Rm)
3294 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003295 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003296 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3297 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3298 if (!isSub &&
3299 (ShImm == 0 ||
3300 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3301 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3302 return 3;
3303 return 4;
3304 }
3305
3306 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003307 unsigned Rt = MI.getOperand(0).getReg();
3308 unsigned Rn = MI.getOperand(2).getReg();
3309 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003310 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003311 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3312 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003313 return (Rt == Rn) ? 3 : 2;
3314 }
3315
3316 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003317 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003318 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003319 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3320 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003321 return 2;
3322 }
3323
3324 case ARM::LDRD_POST:
3325 case ARM::t2LDRD_POST:
3326 return 3;
3327
3328 case ARM::STRD_POST:
3329 case ARM::t2STRD_POST:
3330 return 4;
3331
3332 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003333 unsigned Rt = MI.getOperand(0).getReg();
3334 unsigned Rn = MI.getOperand(3).getReg();
3335 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003336 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003337 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3338 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003339 return (Rt == Rn) ? 4 : 3;
3340 }
3341
3342 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003343 unsigned Rt = MI.getOperand(0).getReg();
3344 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003345 return (Rt == Rn) ? 4 : 3;
3346 }
3347
3348 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003349 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003350 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003351 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3352 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003353 return 3;
3354 }
3355
3356 case ARM::t2STRD_PRE:
3357 return 3;
3358
3359 case ARM::t2LDR_POST:
3360 case ARM::t2LDRB_POST:
3361 case ARM::t2LDRB_PRE:
3362 case ARM::t2LDRSBi12:
3363 case ARM::t2LDRSBi8:
3364 case ARM::t2LDRSBpci:
3365 case ARM::t2LDRSBs:
3366 case ARM::t2LDRH_POST:
3367 case ARM::t2LDRH_PRE:
3368 case ARM::t2LDRSBT:
3369 case ARM::t2LDRSB_POST:
3370 case ARM::t2LDRSB_PRE:
3371 case ARM::t2LDRSH_POST:
3372 case ARM::t2LDRSH_PRE:
3373 case ARM::t2LDRSHi12:
3374 case ARM::t2LDRSHi8:
3375 case ARM::t2LDRSHpci:
3376 case ARM::t2LDRSHs:
3377 return 2;
3378
3379 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003380 unsigned Rt = MI.getOperand(0).getReg();
3381 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003382 return (Rt == Rn) ? 3 : 2;
3383 }
3384
3385 case ARM::t2STRB_POST:
3386 case ARM::t2STRB_PRE:
3387 case ARM::t2STRBs:
3388 case ARM::t2STRDi8:
3389 case ARM::t2STRH_POST:
3390 case ARM::t2STRH_PRE:
3391 case ARM::t2STRHs:
3392 case ARM::t2STR_POST:
3393 case ARM::t2STR_PRE:
3394 case ARM::t2STRs:
3395 return 2;
3396 }
3397}
3398
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003399// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3400// can't be easily determined return 0 (missing MachineMemOperand).
3401//
3402// FIXME: The current MachineInstr design does not support relying on machine
3403// mem operands to determine the width of a memory access. Instead, we expect
3404// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003405// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003406// two reasons:
3407//
3408// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3409// operands. This is much more dangerous than using the MachineMemOperand
3410// sizes because CodeGen passes can insert/remove optional machine operands. In
3411// fact, it's totally incorrect for preRA passes and appears to be wrong for
3412// postRA passes as well.
3413//
3414// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3415// machine model that calls this should handle the unknown (zero size) case.
3416//
3417// Long term, we should require a target hook that verifies MachineMemOperand
3418// sizes during MC lowering. That target hook should be local to MC lowering
3419// because we can't ensure that it is aware of other MI forms. Doing this will
3420// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003421unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003422 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003423 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3424 E = MI.memoperands_end();
3425 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003426 Size += (*I)->getSize();
3427 }
3428 return Size / 4;
3429}
3430
Diana Picus92423ce2016-06-27 09:08:23 +00003431static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3432 unsigned NumRegs) {
3433 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3434 switch (Opc) {
3435 default:
3436 break;
3437 case ARM::VLDMDIA_UPD:
3438 case ARM::VLDMDDB_UPD:
3439 case ARM::VLDMSIA_UPD:
3440 case ARM::VLDMSDB_UPD:
3441 case ARM::VSTMDIA_UPD:
3442 case ARM::VSTMDDB_UPD:
3443 case ARM::VSTMSIA_UPD:
3444 case ARM::VSTMSDB_UPD:
3445 case ARM::LDMIA_UPD:
3446 case ARM::LDMDA_UPD:
3447 case ARM::LDMDB_UPD:
3448 case ARM::LDMIB_UPD:
3449 case ARM::STMIA_UPD:
3450 case ARM::STMDA_UPD:
3451 case ARM::STMDB_UPD:
3452 case ARM::STMIB_UPD:
3453 case ARM::tLDMIA_UPD:
3454 case ARM::tSTMIA_UPD:
3455 case ARM::t2LDMIA_UPD:
3456 case ARM::t2LDMDB_UPD:
3457 case ARM::t2STMIA_UPD:
3458 case ARM::t2STMDB_UPD:
3459 ++UOps; // One for base register writeback.
3460 break;
3461 case ARM::LDMIA_RET:
3462 case ARM::tPOP_RET:
3463 case ARM::t2LDMIA_RET:
3464 UOps += 2; // One for base reg wb, one for write to pc.
3465 break;
3466 }
3467 return UOps;
3468}
3469
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003470unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3471 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003472 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003473 return 1;
3474
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003475 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003476 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003477 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003478 if (ItinUOps >= 0) {
3479 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3480 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3481
Andrew Trickf161e392012-07-02 18:10:42 +00003482 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003483 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003484
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003485 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003486 switch (Opc) {
3487 default:
3488 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003489 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003490 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003491 return 2;
3492
3493 // The number of uOps for load / store multiple are determined by the number
3494 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003495 //
Evan Chengbf407072010-09-10 01:29:16 +00003496 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3497 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003498 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003499 //
Evan Chengbf407072010-09-10 01:29:16 +00003500 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003501 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3502 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3503 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003504 case ARM::VLDMDIA_UPD:
3505 case ARM::VLDMDDB_UPD:
3506 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003507 case ARM::VLDMSIA_UPD:
3508 case ARM::VLDMSDB_UPD:
3509 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003510 case ARM::VSTMDIA_UPD:
3511 case ARM::VSTMDDB_UPD:
3512 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003513 case ARM::VSTMSIA_UPD:
3514 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003515 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003516 return (NumRegs / 2) + (NumRegs % 2) + 1;
3517 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003518
3519 case ARM::LDMIA_RET:
3520 case ARM::LDMIA:
3521 case ARM::LDMDA:
3522 case ARM::LDMDB:
3523 case ARM::LDMIB:
3524 case ARM::LDMIA_UPD:
3525 case ARM::LDMDA_UPD:
3526 case ARM::LDMDB_UPD:
3527 case ARM::LDMIB_UPD:
3528 case ARM::STMIA:
3529 case ARM::STMDA:
3530 case ARM::STMDB:
3531 case ARM::STMIB:
3532 case ARM::STMIA_UPD:
3533 case ARM::STMDA_UPD:
3534 case ARM::STMDB_UPD:
3535 case ARM::STMIB_UPD:
3536 case ARM::tLDMIA:
3537 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003538 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003539 case ARM::tPOP_RET:
3540 case ARM::tPOP:
3541 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003542 case ARM::t2LDMIA_RET:
3543 case ARM::t2LDMIA:
3544 case ARM::t2LDMDB:
3545 case ARM::t2LDMIA_UPD:
3546 case ARM::t2LDMDB_UPD:
3547 case ARM::t2STMIA:
3548 case ARM::t2STMDB:
3549 case ARM::t2STMIA_UPD:
3550 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003551 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003552 switch (Subtarget.getLdStMultipleTiming()) {
3553 case ARMSubtarget::SingleIssuePlusExtras:
3554 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3555 case ARMSubtarget::SingleIssue:
3556 // Assume the worst.
3557 return NumRegs;
3558 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003559 if (NumRegs < 4)
3560 return 2;
3561 // 4 registers would be issued: 2, 2.
3562 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003563 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003564 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003565 ++UOps;
3566 return UOps;
3567 }
3568 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3569 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003570 // If there are odd number of registers or if it's not 64-bit aligned,
3571 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003572 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3573 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003574 ++UOps;
3575 return UOps;
3576 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003577 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003578 }
3579 }
Diana Picus92423ce2016-06-27 09:08:23 +00003580 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003581}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003582
3583int
Evan Cheng412e37b2010-10-07 23:12:15 +00003584ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003585 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003586 unsigned DefClass,
3587 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003588 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003589 if (RegNo <= 0)
3590 // Def is the address writeback.
3591 return ItinData->getOperandCycle(DefClass, DefIdx);
3592
3593 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003594 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003595 // (regno / 2) + (regno % 2) + 1
3596 DefCycle = RegNo / 2 + 1;
3597 if (RegNo % 2)
3598 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003599 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003600 DefCycle = RegNo;
3601 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003602
Evan Cheng6cc775f2011-06-28 19:10:37 +00003603 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003604 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003605 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003606 case ARM::VLDMSIA_UPD:
3607 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003608 isSLoad = true;
3609 break;
3610 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003611
Evan Cheng412e37b2010-10-07 23:12:15 +00003612 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3613 // then it takes an extra cycle.
3614 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3615 ++DefCycle;
3616 } else {
3617 // Assume the worst.
3618 DefCycle = RegNo + 2;
3619 }
3620
3621 return DefCycle;
3622}
3623
Javed Absar4ae7e8122017-06-02 08:53:19 +00003624bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3625 unsigned BaseReg = MI.getOperand(0).getReg();
3626 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3627 const auto &Op = MI.getOperand(i);
3628 if (Op.isReg() && Op.getReg() == BaseReg)
3629 return true;
3630 }
3631 return false;
3632}
3633unsigned
3634ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00003635 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops
3636 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops)
Javed Absar4ae7e8122017-06-02 08:53:19 +00003637 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3638}
3639
Evan Cheng412e37b2010-10-07 23:12:15 +00003640int
3641ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003642 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003643 unsigned DefClass,
3644 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003645 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003646 if (RegNo <= 0)
3647 // Def is the address writeback.
3648 return ItinData->getOperandCycle(DefClass, DefIdx);
3649
3650 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003651 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003652 // 4 registers would be issued: 1, 2, 1.
3653 // 5 registers would be issued: 1, 2, 2.
3654 DefCycle = RegNo / 2;
3655 if (DefCycle < 1)
3656 DefCycle = 1;
3657 // Result latency is issue cycle + 2: E2.
3658 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003659 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003660 DefCycle = (RegNo / 2);
3661 // If there are odd number of registers or if it's not 64-bit aligned,
3662 // then it takes an extra AGU (Address Generation Unit) cycle.
3663 if ((RegNo % 2) || DefAlign < 8)
3664 ++DefCycle;
3665 // Result latency is AGU cycles + 2.
3666 DefCycle += 2;
3667 } else {
3668 // Assume the worst.
3669 DefCycle = RegNo + 2;
3670 }
3671
3672 return DefCycle;
3673}
3674
3675int
3676ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003677 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003678 unsigned UseClass,
3679 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003680 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003681 if (RegNo <= 0)
3682 return ItinData->getOperandCycle(UseClass, UseIdx);
3683
3684 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003685 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003686 // (regno / 2) + (regno % 2) + 1
3687 UseCycle = RegNo / 2 + 1;
3688 if (RegNo % 2)
3689 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003690 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003691 UseCycle = RegNo;
3692 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003693
Evan Cheng6cc775f2011-06-28 19:10:37 +00003694 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003695 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003696 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003697 case ARM::VSTMSIA_UPD:
3698 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003699 isSStore = true;
3700 break;
3701 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003702
Evan Cheng412e37b2010-10-07 23:12:15 +00003703 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3704 // then it takes an extra cycle.
3705 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3706 ++UseCycle;
3707 } else {
3708 // Assume the worst.
3709 UseCycle = RegNo + 2;
3710 }
3711
3712 return UseCycle;
3713}
3714
3715int
3716ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003717 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003718 unsigned UseClass,
3719 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003720 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003721 if (RegNo <= 0)
3722 return ItinData->getOperandCycle(UseClass, UseIdx);
3723
3724 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003725 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003726 UseCycle = RegNo / 2;
3727 if (UseCycle < 2)
3728 UseCycle = 2;
3729 // Read in E3.
3730 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003731 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003732 UseCycle = (RegNo / 2);
3733 // If there are odd number of registers or if it's not 64-bit aligned,
3734 // then it takes an extra AGU (Address Generation Unit) cycle.
3735 if ((RegNo % 2) || UseAlign < 8)
3736 ++UseCycle;
3737 } else {
3738 // Assume the worst.
3739 UseCycle = 1;
3740 }
3741 return UseCycle;
3742}
3743
3744int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003745ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003746 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003747 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003748 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003749 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003750 unsigned DefClass = DefMCID.getSchedClass();
3751 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003752
Evan Cheng6cc775f2011-06-28 19:10:37 +00003753 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003754 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3755
3756 // This may be a def / use of a variable_ops instruction, the operand
3757 // latency might be determinable dynamically. Let the target try to
3758 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003759 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003760 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003761 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003762 default:
3763 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3764 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003765
3766 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003767 case ARM::VLDMDIA_UPD:
3768 case ARM::VLDMDDB_UPD:
3769 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003770 case ARM::VLDMSIA_UPD:
3771 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003772 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003773 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003774
3775 case ARM::LDMIA_RET:
3776 case ARM::LDMIA:
3777 case ARM::LDMDA:
3778 case ARM::LDMDB:
3779 case ARM::LDMIB:
3780 case ARM::LDMIA_UPD:
3781 case ARM::LDMDA_UPD:
3782 case ARM::LDMDB_UPD:
3783 case ARM::LDMIB_UPD:
3784 case ARM::tLDMIA:
3785 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003786 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003787 case ARM::t2LDMIA_RET:
3788 case ARM::t2LDMIA:
3789 case ARM::t2LDMDB:
3790 case ARM::t2LDMIA_UPD:
3791 case ARM::t2LDMDB_UPD:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003792 LdmBypass = true;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003793 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003794 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003795 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003796
3797 if (DefCycle == -1)
3798 // We can't seem to determine the result latency of the def, assume it's 2.
3799 DefCycle = 2;
3800
3801 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003802 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003803 default:
3804 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3805 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003806
3807 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003808 case ARM::VSTMDIA_UPD:
3809 case ARM::VSTMDDB_UPD:
3810 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003811 case ARM::VSTMSIA_UPD:
3812 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003813 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003814 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003815
3816 case ARM::STMIA:
3817 case ARM::STMDA:
3818 case ARM::STMDB:
3819 case ARM::STMIB:
3820 case ARM::STMIA_UPD:
3821 case ARM::STMDA_UPD:
3822 case ARM::STMDB_UPD:
3823 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003824 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003825 case ARM::tPOP_RET:
3826 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003827 case ARM::t2STMIA:
3828 case ARM::t2STMDB:
3829 case ARM::t2STMIA_UPD:
3830 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003831 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003832 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003833 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003834
3835 if (UseCycle == -1)
3836 // Assume it's read in the first stage.
3837 UseCycle = 1;
3838
3839 UseCycle = DefCycle - UseCycle + 1;
3840 if (UseCycle > 0) {
3841 if (LdmBypass) {
3842 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3843 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003844 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003845 UseClass, UseIdx))
3846 --UseCycle;
3847 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003848 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003849 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003850 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003851 }
3852
3853 return UseCycle;
3854}
3855
Evan Cheng7fae11b2011-12-14 02:11:42 +00003856static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003857 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003858 unsigned &DefIdx, unsigned &Dist) {
3859 Dist = 0;
3860
3861 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003862 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003863 assert(II->isInsideBundle() && "Empty bundle?");
3864
3865 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003866 while (II->isInsideBundle()) {
3867 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3868 if (Idx != -1)
3869 break;
3870 --II;
3871 ++Dist;
3872 }
3873
3874 assert(Idx != -1 && "Cannot find bundled definition!");
3875 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003876 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003877}
3878
3879static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003880 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003881 unsigned &UseIdx, unsigned &Dist) {
3882 Dist = 0;
3883
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003884 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003885 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003886 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003887
3888 // FIXME: This doesn't properly handle multiple uses.
3889 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003890 while (II != E && II->isInsideBundle()) {
3891 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3892 if (Idx != -1)
3893 break;
3894 if (II->getOpcode() != ARM::t2IT)
3895 ++Dist;
3896 ++II;
3897 }
3898
Evan Chengda103bf2011-12-14 20:00:08 +00003899 if (Idx == -1) {
3900 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003901 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003902 }
3903
Evan Cheng7fae11b2011-12-14 02:11:42 +00003904 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003905 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003906}
3907
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003908/// Return the number of cycles to add to (or subtract from) the static
3909/// itinerary based on the def opcode and alignment. The caller will ensure that
3910/// adjusted latency is at least one cycle.
3911static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003912 const MachineInstr &DefMI,
3913 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003914 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003915 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003916 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3917 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003918 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003919 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003920 case ARM::LDRrs:
3921 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003922 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003923 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3924 if (ShImm == 0 ||
3925 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003926 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003927 break;
3928 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003929 case ARM::t2LDRs:
3930 case ARM::t2LDRBs:
3931 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003932 case ARM::t2LDRSHs: {
3933 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003934 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003935 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003936 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003937 break;
3938 }
3939 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003940 } else if (Subtarget.isSwift()) {
3941 // FIXME: Properly handle all of the latency adjustments for address
3942 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003943 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003944 default: break;
3945 case ARM::LDRrs:
3946 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003947 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003948 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3949 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3950 if (!isSub &&
3951 (ShImm == 0 ||
3952 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3953 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3954 Adjust -= 2;
3955 else if (!isSub &&
3956 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3957 --Adjust;
3958 break;
3959 }
3960 case ARM::t2LDRs:
3961 case ARM::t2LDRBs:
3962 case ARM::t2LDRHs:
3963 case ARM::t2LDRSHs: {
3964 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003965 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003966 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3967 Adjust -= 2;
3968 break;
3969 }
3970 }
Evan Chengff310732010-10-28 06:47:08 +00003971 }
3972
Diana Picus92423ce2016-06-27 09:08:23 +00003973 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003974 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003975 default: break;
3976 case ARM::VLD1q8:
3977 case ARM::VLD1q16:
3978 case ARM::VLD1q32:
3979 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003980 case ARM::VLD1q8wb_fixed:
3981 case ARM::VLD1q16wb_fixed:
3982 case ARM::VLD1q32wb_fixed:
3983 case ARM::VLD1q64wb_fixed:
3984 case ARM::VLD1q8wb_register:
3985 case ARM::VLD1q16wb_register:
3986 case ARM::VLD1q32wb_register:
3987 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003988 case ARM::VLD2d8:
3989 case ARM::VLD2d16:
3990 case ARM::VLD2d32:
3991 case ARM::VLD2q8:
3992 case ARM::VLD2q16:
3993 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003994 case ARM::VLD2d8wb_fixed:
3995 case ARM::VLD2d16wb_fixed:
3996 case ARM::VLD2d32wb_fixed:
3997 case ARM::VLD2q8wb_fixed:
3998 case ARM::VLD2q16wb_fixed:
3999 case ARM::VLD2q32wb_fixed:
4000 case ARM::VLD2d8wb_register:
4001 case ARM::VLD2d16wb_register:
4002 case ARM::VLD2d32wb_register:
4003 case ARM::VLD2q8wb_register:
4004 case ARM::VLD2q16wb_register:
4005 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004006 case ARM::VLD3d8:
4007 case ARM::VLD3d16:
4008 case ARM::VLD3d32:
4009 case ARM::VLD1d64T:
4010 case ARM::VLD3d8_UPD:
4011 case ARM::VLD3d16_UPD:
4012 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00004013 case ARM::VLD1d64Twb_fixed:
4014 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004015 case ARM::VLD3q8_UPD:
4016 case ARM::VLD3q16_UPD:
4017 case ARM::VLD3q32_UPD:
4018 case ARM::VLD4d8:
4019 case ARM::VLD4d16:
4020 case ARM::VLD4d32:
4021 case ARM::VLD1d64Q:
4022 case ARM::VLD4d8_UPD:
4023 case ARM::VLD4d16_UPD:
4024 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00004025 case ARM::VLD1d64Qwb_fixed:
4026 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004027 case ARM::VLD4q8_UPD:
4028 case ARM::VLD4q16_UPD:
4029 case ARM::VLD4q32_UPD:
4030 case ARM::VLD1DUPq8:
4031 case ARM::VLD1DUPq16:
4032 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00004033 case ARM::VLD1DUPq8wb_fixed:
4034 case ARM::VLD1DUPq16wb_fixed:
4035 case ARM::VLD1DUPq32wb_fixed:
4036 case ARM::VLD1DUPq8wb_register:
4037 case ARM::VLD1DUPq16wb_register:
4038 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004039 case ARM::VLD2DUPd8:
4040 case ARM::VLD2DUPd16:
4041 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00004042 case ARM::VLD2DUPd8wb_fixed:
4043 case ARM::VLD2DUPd16wb_fixed:
4044 case ARM::VLD2DUPd32wb_fixed:
4045 case ARM::VLD2DUPd8wb_register:
4046 case ARM::VLD2DUPd16wb_register:
4047 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004048 case ARM::VLD4DUPd8:
4049 case ARM::VLD4DUPd16:
4050 case ARM::VLD4DUPd32:
4051 case ARM::VLD4DUPd8_UPD:
4052 case ARM::VLD4DUPd16_UPD:
4053 case ARM::VLD4DUPd32_UPD:
4054 case ARM::VLD1LNd8:
4055 case ARM::VLD1LNd16:
4056 case ARM::VLD1LNd32:
4057 case ARM::VLD1LNd8_UPD:
4058 case ARM::VLD1LNd16_UPD:
4059 case ARM::VLD1LNd32_UPD:
4060 case ARM::VLD2LNd8:
4061 case ARM::VLD2LNd16:
4062 case ARM::VLD2LNd32:
4063 case ARM::VLD2LNq16:
4064 case ARM::VLD2LNq32:
4065 case ARM::VLD2LNd8_UPD:
4066 case ARM::VLD2LNd16_UPD:
4067 case ARM::VLD2LNd32_UPD:
4068 case ARM::VLD2LNq16_UPD:
4069 case ARM::VLD2LNq32_UPD:
4070 case ARM::VLD4LNd8:
4071 case ARM::VLD4LNd16:
4072 case ARM::VLD4LNd32:
4073 case ARM::VLD4LNq16:
4074 case ARM::VLD4LNq32:
4075 case ARM::VLD4LNd8_UPD:
4076 case ARM::VLD4LNd16_UPD:
4077 case ARM::VLD4LNd32_UPD:
4078 case ARM::VLD4LNq16_UPD:
4079 case ARM::VLD4LNq32_UPD:
4080 // If the address is not 64-bit aligned, the latencies of these
4081 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004082 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004083 break;
4084 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004085 }
4086 return Adjust;
4087}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004088
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004089int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4090 const MachineInstr &DefMI,
4091 unsigned DefIdx,
4092 const MachineInstr &UseMI,
4093 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004094 // No operand latency. The caller may fall back to getInstrLatency.
4095 if (!ItinData || ItinData->isEmpty())
4096 return -1;
4097
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004098 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004099 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004100
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004101 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004102 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004103 if (DefMI.isBundle())
4104 ResolvedDefMI =
4105 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
4106 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
4107 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004108 return 1;
4109 }
4110
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004111 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004112 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004113 if (UseMI.isBundle()) {
4114 ResolvedUseMI =
4115 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
4116 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00004117 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004118 }
4119
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004120 return getOperandLatencyImpl(
4121 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
4122 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
4123}
4124
4125int ARMBaseInstrInfo::getOperandLatencyImpl(
4126 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4127 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
4128 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
4129 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004130 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004131 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004132 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00004133 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004134 }
4135
4136 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004137 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004138 return 0;
4139
4140 // Otherwise it takes the instruction latency (generally one).
4141 unsigned Latency = getInstrLatency(ItinData, DefMI);
4142
4143 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4144 // its uses. Instructions which are otherwise scheduled between them may
4145 // incur a code size penalty (not able to use the CPSR setting 16-bit
4146 // instructions).
4147 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004148 const MachineFunction *MF = DefMI.getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00004149 // FIXME: Use Function::optForSize().
Matthias Braunf1caa282017-12-15 22:22:58 +00004150 if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004151 --Latency;
4152 }
4153 return Latency;
4154 }
4155
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004156 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00004157 return -1;
4158
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004159 unsigned DefAlign = DefMI.hasOneMemOperand()
4160 ? (*DefMI.memoperands_begin())->getAlignment()
4161 : 0;
4162 unsigned UseAlign = UseMI.hasOneMemOperand()
4163 ? (*UseMI.memoperands_begin())->getAlignment()
4164 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004165
4166 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004167 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
4168 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004169 // Unable to find operand latency. The caller may resort to getInstrLatency.
4170 if (Latency < 0)
4171 return Latency;
4172
4173 // Adjust for IT block position.
4174 int Adj = DefAdj + UseAdj;
4175
4176 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4177 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4178 if (Adj >= 0 || (int)Latency > -Adj) {
4179 return Latency + Adj;
4180 }
4181 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00004182 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004183}
4184
4185int
4186ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4187 SDNode *DefNode, unsigned DefIdx,
4188 SDNode *UseNode, unsigned UseIdx) const {
4189 if (!DefNode->isMachineOpcode())
4190 return 1;
4191
Evan Cheng6cc775f2011-06-28 19:10:37 +00004192 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00004193
Evan Cheng6cc775f2011-06-28 19:10:37 +00004194 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00004195 return 0;
4196
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004197 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004198 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004199
Evan Cheng6c1414f2010-10-29 18:09:28 +00004200 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00004201 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00004202 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4203 int Threshold = 1 + Adj;
4204 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00004205 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004206
Evan Cheng6cc775f2011-06-28 19:10:37 +00004207 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004208 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4209 unsigned DefAlign = !DefMN->memoperands_empty()
4210 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4211 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4212 unsigned UseAlign = !UseMN->memoperands_empty()
4213 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00004214 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4215 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00004216
4217 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00004218 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4219 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00004220 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4221 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00004222 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00004223 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004224 case ARM::LDRrs:
4225 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00004226 unsigned ShOpVal =
4227 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4228 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4229 if (ShImm == 0 ||
4230 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4231 --Latency;
4232 break;
4233 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004234 case ARM::t2LDRs:
4235 case ARM::t2LDRBs:
4236 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00004237 case ARM::t2LDRSHs: {
4238 // Thumb2 mode: lsl only.
4239 unsigned ShAmt =
4240 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4241 if (ShAmt == 0 || ShAmt == 2)
4242 --Latency;
4243 break;
4244 }
4245 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00004246 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4247 // FIXME: Properly handle all of the latency adjustments for address
4248 // writeback.
4249 switch (DefMCID.getOpcode()) {
4250 default: break;
4251 case ARM::LDRrs:
4252 case ARM::LDRBrs: {
4253 unsigned ShOpVal =
4254 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4255 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4256 if (ShImm == 0 ||
4257 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4258 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4259 Latency -= 2;
4260 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4261 --Latency;
4262 break;
4263 }
4264 case ARM::t2LDRs:
4265 case ARM::t2LDRBs:
4266 case ARM::t2LDRHs:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004267 case ARM::t2LDRSHs:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004268 // Thumb2 mode: lsl 0-3 only.
4269 Latency -= 2;
4270 break;
4271 }
Evan Chengff310732010-10-28 06:47:08 +00004272 }
4273
Diana Picus92423ce2016-06-27 09:08:23 +00004274 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004275 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004276 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004277 case ARM::VLD1q8:
4278 case ARM::VLD1q16:
4279 case ARM::VLD1q32:
4280 case ARM::VLD1q64:
4281 case ARM::VLD1q8wb_register:
4282 case ARM::VLD1q16wb_register:
4283 case ARM::VLD1q32wb_register:
4284 case ARM::VLD1q64wb_register:
4285 case ARM::VLD1q8wb_fixed:
4286 case ARM::VLD1q16wb_fixed:
4287 case ARM::VLD1q32wb_fixed:
4288 case ARM::VLD1q64wb_fixed:
4289 case ARM::VLD2d8:
4290 case ARM::VLD2d16:
4291 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004292 case ARM::VLD2q8Pseudo:
4293 case ARM::VLD2q16Pseudo:
4294 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004295 case ARM::VLD2d8wb_fixed:
4296 case ARM::VLD2d16wb_fixed:
4297 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00004298 case ARM::VLD2q8PseudoWB_fixed:
4299 case ARM::VLD2q16PseudoWB_fixed:
4300 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004301 case ARM::VLD2d8wb_register:
4302 case ARM::VLD2d16wb_register:
4303 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00004304 case ARM::VLD2q8PseudoWB_register:
4305 case ARM::VLD2q16PseudoWB_register:
4306 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004307 case ARM::VLD3d8Pseudo:
4308 case ARM::VLD3d16Pseudo:
4309 case ARM::VLD3d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004310 case ARM::VLD1d8TPseudo:
4311 case ARM::VLD1d16TPseudo:
4312 case ARM::VLD1d32TPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004313 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004314 case ARM::VLD1d64TPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004315 case ARM::VLD1d64TPseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004316 case ARM::VLD3d8Pseudo_UPD:
4317 case ARM::VLD3d16Pseudo_UPD:
4318 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004319 case ARM::VLD3q8Pseudo_UPD:
4320 case ARM::VLD3q16Pseudo_UPD:
4321 case ARM::VLD3q32Pseudo_UPD:
4322 case ARM::VLD3q8oddPseudo:
4323 case ARM::VLD3q16oddPseudo:
4324 case ARM::VLD3q32oddPseudo:
4325 case ARM::VLD3q8oddPseudo_UPD:
4326 case ARM::VLD3q16oddPseudo_UPD:
4327 case ARM::VLD3q32oddPseudo_UPD:
4328 case ARM::VLD4d8Pseudo:
4329 case ARM::VLD4d16Pseudo:
4330 case ARM::VLD4d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004331 case ARM::VLD1d8QPseudo:
4332 case ARM::VLD1d16QPseudo:
4333 case ARM::VLD1d32QPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004334 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004335 case ARM::VLD1d64QPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004336 case ARM::VLD1d64QPseudoWB_register:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004337 case ARM::VLD1q8HighQPseudo:
4338 case ARM::VLD1q8LowQPseudo_UPD:
4339 case ARM::VLD1q8HighTPseudo:
4340 case ARM::VLD1q8LowTPseudo_UPD:
4341 case ARM::VLD1q16HighQPseudo:
4342 case ARM::VLD1q16LowQPseudo_UPD:
4343 case ARM::VLD1q16HighTPseudo:
4344 case ARM::VLD1q16LowTPseudo_UPD:
4345 case ARM::VLD1q32HighQPseudo:
4346 case ARM::VLD1q32LowQPseudo_UPD:
4347 case ARM::VLD1q32HighTPseudo:
4348 case ARM::VLD1q32LowTPseudo_UPD:
4349 case ARM::VLD1q64HighQPseudo:
4350 case ARM::VLD1q64LowQPseudo_UPD:
4351 case ARM::VLD1q64HighTPseudo:
4352 case ARM::VLD1q64LowTPseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004353 case ARM::VLD4d8Pseudo_UPD:
4354 case ARM::VLD4d16Pseudo_UPD:
4355 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004356 case ARM::VLD4q8Pseudo_UPD:
4357 case ARM::VLD4q16Pseudo_UPD:
4358 case ARM::VLD4q32Pseudo_UPD:
4359 case ARM::VLD4q8oddPseudo:
4360 case ARM::VLD4q16oddPseudo:
4361 case ARM::VLD4q32oddPseudo:
4362 case ARM::VLD4q8oddPseudo_UPD:
4363 case ARM::VLD4q16oddPseudo_UPD:
4364 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00004365 case ARM::VLD1DUPq8:
4366 case ARM::VLD1DUPq16:
4367 case ARM::VLD1DUPq32:
4368 case ARM::VLD1DUPq8wb_fixed:
4369 case ARM::VLD1DUPq16wb_fixed:
4370 case ARM::VLD1DUPq32wb_fixed:
4371 case ARM::VLD1DUPq8wb_register:
4372 case ARM::VLD1DUPq16wb_register:
4373 case ARM::VLD1DUPq32wb_register:
4374 case ARM::VLD2DUPd8:
4375 case ARM::VLD2DUPd16:
4376 case ARM::VLD2DUPd32:
4377 case ARM::VLD2DUPd8wb_fixed:
4378 case ARM::VLD2DUPd16wb_fixed:
4379 case ARM::VLD2DUPd32wb_fixed:
4380 case ARM::VLD2DUPd8wb_register:
4381 case ARM::VLD2DUPd16wb_register:
4382 case ARM::VLD2DUPd32wb_register:
Ivan A. Kosarev72315982018-06-27 13:57:52 +00004383 case ARM::VLD2DUPq8EvenPseudo:
4384 case ARM::VLD2DUPq8OddPseudo:
4385 case ARM::VLD2DUPq16EvenPseudo:
4386 case ARM::VLD2DUPq16OddPseudo:
4387 case ARM::VLD2DUPq32EvenPseudo:
4388 case ARM::VLD2DUPq32OddPseudo:
4389 case ARM::VLD3DUPq8EvenPseudo:
4390 case ARM::VLD3DUPq8OddPseudo:
4391 case ARM::VLD3DUPq16EvenPseudo:
4392 case ARM::VLD3DUPq16OddPseudo:
4393 case ARM::VLD3DUPq32EvenPseudo:
4394 case ARM::VLD3DUPq32OddPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004395 case ARM::VLD4DUPd8Pseudo:
4396 case ARM::VLD4DUPd16Pseudo:
4397 case ARM::VLD4DUPd32Pseudo:
4398 case ARM::VLD4DUPd8Pseudo_UPD:
4399 case ARM::VLD4DUPd16Pseudo_UPD:
4400 case ARM::VLD4DUPd32Pseudo_UPD:
Ivan A. Kosarev72315982018-06-27 13:57:52 +00004401 case ARM::VLD4DUPq8EvenPseudo:
4402 case ARM::VLD4DUPq8OddPseudo:
4403 case ARM::VLD4DUPq16EvenPseudo:
4404 case ARM::VLD4DUPq16OddPseudo:
4405 case ARM::VLD4DUPq32EvenPseudo:
4406 case ARM::VLD4DUPq32OddPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004407 case ARM::VLD1LNq8Pseudo:
4408 case ARM::VLD1LNq16Pseudo:
4409 case ARM::VLD1LNq32Pseudo:
4410 case ARM::VLD1LNq8Pseudo_UPD:
4411 case ARM::VLD1LNq16Pseudo_UPD:
4412 case ARM::VLD1LNq32Pseudo_UPD:
4413 case ARM::VLD2LNd8Pseudo:
4414 case ARM::VLD2LNd16Pseudo:
4415 case ARM::VLD2LNd32Pseudo:
4416 case ARM::VLD2LNq16Pseudo:
4417 case ARM::VLD2LNq32Pseudo:
4418 case ARM::VLD2LNd8Pseudo_UPD:
4419 case ARM::VLD2LNd16Pseudo_UPD:
4420 case ARM::VLD2LNd32Pseudo_UPD:
4421 case ARM::VLD2LNq16Pseudo_UPD:
4422 case ARM::VLD2LNq32Pseudo_UPD:
4423 case ARM::VLD4LNd8Pseudo:
4424 case ARM::VLD4LNd16Pseudo:
4425 case ARM::VLD4LNd32Pseudo:
4426 case ARM::VLD4LNq16Pseudo:
4427 case ARM::VLD4LNq32Pseudo:
4428 case ARM::VLD4LNd8Pseudo_UPD:
4429 case ARM::VLD4LNd16Pseudo_UPD:
4430 case ARM::VLD4LNd32Pseudo_UPD:
4431 case ARM::VLD4LNq16Pseudo_UPD:
4432 case ARM::VLD4LNq32Pseudo_UPD:
4433 // If the address is not 64-bit aligned, the latencies of these
4434 // instructions increases by one.
4435 ++Latency;
4436 break;
4437 }
4438
Evan Chengff310732010-10-28 06:47:08 +00004439 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004440}
Evan Cheng63c76082010-10-19 18:58:51 +00004441
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004442unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4443 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4444 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004445 return 0;
4446
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004447 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004448 return 0;
4449
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004450 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004451
Javed Absar4ae7e8122017-06-02 08:53:19 +00004452 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4453 !Subtarget.cheapPredicableCPSRDef())) {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004454 // When predicated, CPSR is an additional source operand for CPSR updating
4455 // instructions, this apparently increases their latencies.
4456 return 1;
4457 }
4458 return 0;
4459}
4460
Andrew Trick45446062012-06-05 21:11:27 +00004461unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004462 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004463 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004464 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4465 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004466 return 1;
4467
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004468 // An instruction scheduler typically runs on unbundled instructions, however
4469 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004470 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004471 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004472 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4473 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004474 while (++I != E && I->isInsideBundle()) {
4475 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004476 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004477 }
4478 return Latency;
4479 }
4480
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004481 const MCInstrDesc &MCID = MI.getDesc();
Javed Absar4ae7e8122017-06-02 08:53:19 +00004482 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4483 !Subtarget.cheapPredicableCPSRDef()))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004484 // When predicated, CPSR is an additional source operand for CPSR updating
4485 // instructions, this apparently increases their latencies.
4486 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004487 }
4488 // Be sure to call getStageLatency for an empty itinerary in case it has a
4489 // valid MinLatency property.
4490 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004491 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004492
4493 unsigned Class = MCID.getSchedClass();
4494
4495 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004496 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004497 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004498
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004499 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004500 unsigned Latency = ItinData->getStageLatency(Class);
4501
4502 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004503 unsigned DefAlign =
4504 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4505 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004506 if (Adj >= 0 || (int)Latency > -Adj) {
4507 return Latency + Adj;
4508 }
4509 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004510}
4511
4512int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4513 SDNode *Node) const {
4514 if (!Node->isMachineOpcode())
4515 return 1;
4516
4517 if (!ItinData || ItinData->isEmpty())
4518 return 1;
4519
4520 unsigned Opcode = Node->getMachineOpcode();
4521 switch (Opcode) {
4522 default:
4523 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004524 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004525 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004526 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004527 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004528}
4529
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004530bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4531 const MachineRegisterInfo *MRI,
4532 const MachineInstr &DefMI,
4533 unsigned DefIdx,
4534 const MachineInstr &UseMI,
4535 unsigned UseIdx) const {
4536 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4537 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004538 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004539 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004540 return true;
4541
4542 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004543 unsigned Latency =
4544 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004545 if (Latency <= 3)
4546 return false;
4547 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4548 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4549}
Evan Chenge96b8d72010-10-26 02:08:50 +00004550
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004551bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4552 const MachineInstr &DefMI,
4553 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004554 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004555 if (!ItinData || ItinData->isEmpty())
4556 return false;
4557
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004558 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004559 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004560 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004561 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4562 return (DefCycle != -1 && DefCycle <= 2);
4563 }
4564 return false;
4565}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004566
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004567bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004568 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004569 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004570 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4571 return false;
4572 }
4573 return true;
4574}
4575
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004576// LoadStackGuard has so far only been implemented for MachO. Different code
4577// sequence is needed for other targets.
4578void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4579 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004580 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004581 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4582 "ROPI/RWPI not currently supported with stack guard");
4583
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004584 MachineBasicBlock &MBB = *MI->getParent();
4585 DebugLoc DL = MI->getDebugLoc();
4586 unsigned Reg = MI->getOperand(0).getReg();
4587 const GlobalValue *GV =
4588 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4589 MachineInstrBuilder MIB;
4590
4591 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4592 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4593
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004594 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004595 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4596 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004597 auto Flags = MachineMemOperand::MOLoad |
4598 MachineMemOperand::MODereferenceable |
4599 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004600 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004601 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004602 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004603 }
4604
4605 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004606 MIB.addReg(Reg, RegState::Kill)
Chandler Carruthc73c0302018-08-16 21:30:05 +00004607 .addImm(0)
4608 .cloneMemRefs(*MI)
4609 .add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004610}
4611
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004612bool
4613ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4614 unsigned &AddSubOpc,
4615 bool &NegAcc, bool &HasLane) const {
4616 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4617 if (I == MLxEntryMap.end())
4618 return false;
4619
4620 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4621 MulOpc = Entry.MulOpc;
4622 AddSubOpc = Entry.AddSubOpc;
4623 NegAcc = Entry.NegAcc;
4624 HasLane = Entry.HasLane;
4625 return true;
4626}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004627
4628//===----------------------------------------------------------------------===//
4629// Execution domains.
4630//===----------------------------------------------------------------------===//
4631//
4632// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4633// and some can go down both. The vmov instructions go down the VFP pipeline,
4634// but they can be changed to vorr equivalents that are executed by the NEON
4635// pipeline.
4636//
4637// We use the following execution domain numbering:
4638//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004639enum ARMExeDomain {
4640 ExeGeneric = 0,
4641 ExeVFP = 1,
4642 ExeNEON = 2
4643};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004644
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004645//
4646// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4647//
4648std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004649ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004650 // If we don't have access to NEON instructions then we won't be able
4651 // to swizzle anything to the NEON domain. Check to make sure.
4652 if (Subtarget.hasNEON()) {
4653 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4654 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004655 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004656 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004657
Eric Christopher7e70aba2015-03-07 00:12:22 +00004658 // CortexA9 is particularly picky about mixing the two and wants these
4659 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004660 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4661 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4662 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004663 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4664 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004665 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004666 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004667
4668 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004669 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004670
4671 // Certain instructions can go either way on Cortex-A8.
4672 // Treat them as NEON instructions.
4673 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004674 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004675
4676 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004677 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004678
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004679 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004680}
4681
Tim Northover771f1602012-08-29 16:36:07 +00004682static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4683 unsigned SReg, unsigned &Lane) {
4684 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4685 Lane = 0;
4686
4687 if (DReg != ARM::NoRegister)
4688 return DReg;
4689
4690 Lane = 1;
4691 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4692
4693 assert(DReg && "S-register with no D super-register?");
4694 return DReg;
4695}
4696
Andrew Trickd9296ec2012-10-10 05:43:01 +00004697/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004698/// set ImplicitSReg to a register number that must be marked as implicit-use or
4699/// zero if no register needs to be defined as implicit-use.
4700///
4701/// If the function cannot determine if an SPR should be marked implicit use or
4702/// not, it returns false.
4703///
4704/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004705/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004706/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4707/// lane of the DPR).
4708///
4709/// If the other SPR is defined, an implicit-use of it should be added. Else,
4710/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004711///
James Molloyea052562012-09-18 08:31:15 +00004712static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004713 MachineInstr &MI, unsigned DReg,
4714 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004715 // If the DPR is defined or used already, the other SPR lane will be chained
4716 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004717 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004718 ImplicitSReg = 0;
4719 return true;
4720 }
4721
4722 // Otherwise we need to go searching to see if the SPR is set explicitly.
4723 ImplicitSReg = TRI->getSubReg(DReg,
4724 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4725 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004726 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004727
4728 if (LQR == MachineBasicBlock::LQR_Live)
4729 return true;
4730 else if (LQR == MachineBasicBlock::LQR_Unknown)
4731 return false;
4732
4733 // If the register is known not to be live, there is no need to add an
4734 // implicit-use.
4735 ImplicitSReg = 0;
4736 return true;
4737}
Tim Northover771f1602012-08-29 16:36:07 +00004738
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004739void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4740 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004741 unsigned DstReg, SrcReg, DReg;
4742 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004743 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004744 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004745 switch (MI.getOpcode()) {
4746 default:
4747 llvm_unreachable("cannot handle opcode!");
4748 break;
4749 case ARM::VMOVD:
4750 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004751 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004752
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004753 // Zap the predicate operands.
4754 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004755
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004756 // Make sure we've got NEON instructions.
4757 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004758
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004759 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4760 DstReg = MI.getOperand(0).getReg();
4761 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004762
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004763 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4764 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004765
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004766 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4767 MI.setDesc(get(ARM::VORRd));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004768 MIB.addReg(DstReg, RegState::Define)
4769 .addReg(SrcReg)
4770 .addReg(SrcReg)
4771 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004772 break;
4773 case ARM::VMOVRS:
4774 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004775 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004776 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004777
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004778 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4779 DstReg = MI.getOperand(0).getReg();
4780 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004781
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004782 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4783 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004784
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004785 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004786
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004787 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4788 // Note that DSrc has been widened and the other lane may be undef, which
4789 // contaminates the entire register.
4790 MI.setDesc(get(ARM::VGETLNi32));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004791 MIB.addReg(DstReg, RegState::Define)
4792 .addReg(DReg, RegState::Undef)
4793 .addImm(Lane)
4794 .add(predOps(ARMCC::AL));
Tim Northoverf6618152012-08-17 11:32:52 +00004795
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004796 // The old source should be an implicit use, otherwise we might think it
4797 // was dead before here.
4798 MIB.addReg(SrcReg, RegState::Implicit);
4799 break;
4800 case ARM::VMOVSR: {
4801 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004802 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004803 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004804
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004805 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4806 DstReg = MI.getOperand(0).getReg();
4807 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004808
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004809 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004810
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004811 unsigned ImplicitSReg;
4812 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004813 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004814
4815 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4816 MI.RemoveOperand(i - 1);
4817
4818 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4819 // Again DDst may be undefined at the beginning of this instruction.
4820 MI.setDesc(get(ARM::VSETLNi32));
4821 MIB.addReg(DReg, RegState::Define)
4822 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4823 .addReg(SrcReg)
Diana Picus4f8c3e12017-01-13 09:37:56 +00004824 .addImm(Lane)
4825 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004826
4827 // The narrower destination must be marked as set to keep previous chains
4828 // in place.
4829 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4830 if (ImplicitSReg != 0)
4831 MIB.addReg(ImplicitSReg, RegState::Implicit);
4832 break;
James Molloyea052562012-09-18 08:31:15 +00004833 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004834 case ARM::VMOVS: {
4835 if (Domain != ExeNEON)
4836 break;
4837
4838 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004839 DstReg = MI.getOperand(0).getReg();
4840 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004841
Tim Northoverca9f3842012-08-30 10:17:45 +00004842 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4843 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4844 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4845
James Molloyea052562012-09-18 08:31:15 +00004846 unsigned ImplicitSReg;
4847 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4848 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004849
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004850 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4851 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004852
Tim Northoverca9f3842012-08-30 10:17:45 +00004853 if (DSrc == DDst) {
4854 // Destination can be:
4855 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004856 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004857 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004858 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
Diana Picus4f8c3e12017-01-13 09:37:56 +00004859 .addImm(SrcLane)
4860 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004861
4862 // Neither the source or the destination are naturally represented any
4863 // more, so add them in manually.
4864 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4865 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004866 if (ImplicitSReg != 0)
4867 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004868 break;
4869 }
4870
4871 // In general there's no single instruction that can perform an S <-> S
4872 // move in NEON space, but a pair of VEXT instructions *can* do the
4873 // job. It turns out that the VEXTs needed will only use DSrc once, with
4874 // the position based purely on the combination of lane-0 and lane-1
4875 // involved. For example
4876 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4877 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4878 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4879 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4880 //
4881 // Pattern of the MachineInstrs is:
4882 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4883 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004884 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4885 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004886
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004887 // On the first instruction, both DSrc and DDst may be undef if present.
Tim Northover726d32c2012-09-01 18:07:29 +00004888 // Specifically when the original instruction didn't have them as an
4889 // <imp-use>.
4890 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004891 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004892 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4893
4894 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004895 CurUndef = !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004896 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4897 .addImm(1)
4898 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004899
4900 if (SrcLane == DstLane)
4901 NewMIB.addReg(SrcReg, RegState::Implicit);
4902
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004903 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004904 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004905
4906 // On the second instruction, DDst has definitely been defined above, so
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004907 // it is not undef. DSrc, if present, can be undef as above.
Tim Northover726d32c2012-09-01 18:07:29 +00004908 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004909 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004910 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4911
4912 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004913 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004914 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4915 .addImm(1)
4916 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004917
4918 if (SrcLane != DstLane)
4919 MIB.addReg(SrcReg, RegState::Implicit);
4920
4921 // As before, the original destination is no longer represented, add it
4922 // implicitly.
4923 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004924 if (ImplicitSReg != 0)
4925 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004926 break;
4927 }
Tim Northoverf6618152012-08-17 11:32:52 +00004928 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004929}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004930
Bob Wilsone8a549c2012-09-29 21:43:49 +00004931//===----------------------------------------------------------------------===//
4932// Partial register updates
4933//===----------------------------------------------------------------------===//
4934//
4935// Swift renames NEON registers with 64-bit granularity. That means any
4936// instruction writing an S-reg implicitly reads the containing D-reg. The
4937// problem is mostly avoided by translating f32 operations to v2f32 operations
4938// on D-registers, but f32 loads are still a problem.
4939//
4940// These instructions can load an f32 into a NEON register:
4941//
4942// VLDRS - Only writes S, partial D update.
4943// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4944// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4945//
4946// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004947unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4948 const MachineInstr &MI, unsigned OpNum,
4949 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004950 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4951 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004952 return 0;
4953
4954 assert(TRI && "Need TRI instance");
4955
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004956 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004957 if (MO.readsReg())
4958 return 0;
4959 unsigned Reg = MO.getReg();
4960 int UseOp = -1;
4961
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004962 switch (MI.getOpcode()) {
4963 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004964 case ARM::VLDRS:
4965 case ARM::FCONSTS:
4966 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004967 case ARM::VMOVv8i8:
4968 case ARM::VMOVv4i16:
4969 case ARM::VMOVv2i32:
4970 case ARM::VMOVv2f32:
4971 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004972 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004973 break;
4974
4975 // Explicitly reads the dependency.
4976 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004977 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004978 break;
4979 default:
4980 return 0;
4981 }
4982
4983 // If this instruction actually reads a value from Reg, there is no unwanted
4984 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004985 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00004986 return 0;
4987
4988 // We must be able to clobber the whole D-reg.
4989 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004990 // Virtual register must be a def undef foo:ssub_0 operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004991 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004992 return 0;
4993 } else if (ARM::SPRRegClass.contains(Reg)) {
4994 // Physical register: MI must define the full D-reg.
4995 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4996 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004997 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004998 return 0;
4999 }
5000
5001 // MI has an unwanted D-register dependency.
5002 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00005003 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00005004}
5005
5006// Break a partial register dependency after getPartialRegUpdateClearance
5007// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005008void ARMBaseInstrInfo::breakPartialRegDependency(
5009 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
5010 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00005011 assert(TRI && "Need TRI instance");
5012
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005013 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005014 unsigned Reg = MO.getReg();
5015 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
5016 "Can't break virtual register dependencies.");
5017 unsigned DReg = Reg;
5018
5019 // If MI defines an S-reg, find the corresponding D super-register.
5020 if (ARM::SPRRegClass.contains(Reg)) {
5021 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
5022 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
5023 }
5024
5025 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005026 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00005027
5028 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
5029 // the full D-register by loading the same value to both lanes. The
5030 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00005031 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00005032 // too big regressions.
5033
5034 // Insert the dependency-breaking FCONSTD before MI.
5035 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Diana Picus4f8c3e12017-01-13 09:37:56 +00005036 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
5037 .addImm(96)
5038 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005039 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005040}
5041
Jim Grosbach617f84dd2012-02-28 23:53:30 +00005042bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00005043 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00005044}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00005045
5046bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00005047 if (MI->getNumOperands() < 4)
5048 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00005049 unsigned ShOpVal = MI->getOperand(3).getImm();
5050 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
5051 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
5052 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
5053 ((ShImm == 1 || ShImm == 2) &&
5054 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
5055 return true;
5056
5057 return false;
5058}
Quentin Colombetd358e842014-08-22 18:05:22 +00005059
5060bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
5061 const MachineInstr &MI, unsigned DefIdx,
5062 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
5063 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5064 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
5065
5066 switch (MI.getOpcode()) {
5067 case ARM::VMOVDRR:
5068 // dX = VMOVDRR rY, rZ
5069 // is the same as:
5070 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
5071 // Populate the InputRegs accordingly.
5072 // rY
5073 const MachineOperand *MOReg = &MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00005074 if (!MOReg->isUndef())
5075 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5076 MOReg->getSubReg(), ARM::ssub_0));
Quentin Colombetd358e842014-08-22 18:05:22 +00005077 // rZ
5078 MOReg = &MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005079 if (!MOReg->isUndef())
5080 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5081 MOReg->getSubReg(), ARM::ssub_1));
Quentin Colombetd358e842014-08-22 18:05:22 +00005082 return true;
5083 }
5084 llvm_unreachable("Target dependent opcode missing");
5085}
5086
5087bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
5088 const MachineInstr &MI, unsigned DefIdx,
5089 RegSubRegPairAndIdx &InputReg) const {
5090 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5091 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
5092
5093 switch (MI.getOpcode()) {
5094 case ARM::VMOVRRD:
5095 // rX, rY = VMOVRRD dZ
5096 // is the same as:
5097 // rX = EXTRACT_SUBREG dZ, ssub_0
5098 // rY = EXTRACT_SUBREG dZ, ssub_1
5099 const MachineOperand &MOReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005100 if (MOReg.isUndef())
5101 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005102 InputReg.Reg = MOReg.getReg();
5103 InputReg.SubReg = MOReg.getSubReg();
5104 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
5105 return true;
5106 }
5107 llvm_unreachable("Target dependent opcode missing");
5108}
5109
5110bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5111 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
5112 RegSubRegPairAndIdx &InsertedReg) const {
5113 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5114 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
5115
5116 switch (MI.getOpcode()) {
5117 case ARM::VSETLNi32:
5118 // dX = VSETLNi32 dY, rZ, imm
5119 const MachineOperand &MOBaseReg = MI.getOperand(1);
5120 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005121 if (MOInsertedReg.isUndef())
5122 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005123 const MachineOperand &MOIndex = MI.getOperand(3);
5124 BaseReg.Reg = MOBaseReg.getReg();
5125 BaseReg.SubReg = MOBaseReg.getSubReg();
5126
5127 InsertedReg.Reg = MOInsertedReg.getReg();
5128 InsertedReg.SubReg = MOInsertedReg.getSubReg();
5129 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
5130 return true;
5131 }
5132 llvm_unreachable("Target dependent opcode missing");
5133}
Martin Storsjod3b29222018-08-22 20:34:06 +00005134
5135std::pair<unsigned, unsigned>
5136ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
5137 const unsigned Mask = ARMII::MO_OPTION_MASK;
5138 return std::make_pair(TF & Mask, TF & ~Mask);
5139}
5140
5141ArrayRef<std::pair<unsigned, const char *>>
5142ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5143 using namespace ARMII;
5144
5145 static const std::pair<unsigned, const char *> TargetFlags[] = {
5146 {MO_LO16, "arm-lo16"}, {MO_HI16, "arm-hi16"}};
5147 return makeArrayRef(TargetFlags);
5148}
5149
5150ArrayRef<std::pair<unsigned, const char *>>
5151ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5152 using namespace ARMII;
5153
5154 static const std::pair<unsigned, const char *> TargetFlags[] = {
Martin Storsjo2dcaa412018-08-31 08:00:25 +00005155 {MO_COFFSTUB, "arm-coffstub"},
Martin Storsjod3b29222018-08-22 20:34:06 +00005156 {MO_GOT, "arm-got"},
5157 {MO_SBREL, "arm-sbrel"},
5158 {MO_DLLIMPORT, "arm-dllimport"},
5159 {MO_SECREL, "arm-secrel"},
5160 {MO_NONLAZY, "arm-nonlazy"}};
5161 return makeArrayRef(TargetFlags);
5162}