blob: fbef5d790a4403cb2365d676e021ee5382fef77c [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 {
Simon Tatham760df472019-05-28 16:13:20 +0000136 if (Subtarget.isThumb2() || Subtarget.hasVFP2Base())
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
Nick Desaulniers9f7bd712019-05-24 18:58:21 +0000709 switch (MI.getOpcode()) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000710 default:
711 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000712 return 0;
713 case TargetOpcode::BUNDLE:
714 return getInstBundleLength(MI);
715 case ARM::MOVi16_ga_pcrel:
716 case ARM::MOVTi16_ga_pcrel:
717 case ARM::t2MOVi16_ga_pcrel:
718 case ARM::t2MOVTi16_ga_pcrel:
719 return 4;
720 case ARM::MOVi32imm:
721 case ARM::t2MOVi32imm:
722 return 8;
723 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000724 case ARM::JUMPTABLE_INSTS:
725 case ARM::JUMPTABLE_ADDRS:
726 case ARM::JUMPTABLE_TBB:
727 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000728 // If this machine instr is a constant pool entry, its size is recorded as
729 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000730 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000731 case ARM::Int_eh_sjlj_longjmp:
732 return 16;
733 case ARM::tInt_eh_sjlj_longjmp:
734 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000735 case ARM::tInt_WIN_eh_sjlj_longjmp:
736 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000737 case ARM::Int_eh_sjlj_setjmp:
738 case ARM::Int_eh_sjlj_setjmp_nofp:
739 return 20;
740 case ARM::tInt_eh_sjlj_setjmp:
741 case ARM::t2Int_eh_sjlj_setjmp:
742 case ARM::t2Int_eh_sjlj_setjmp_nofp:
743 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000744 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000745 return MI.getOperand(1).getImm();
Nick Desaulniers9f7bd712019-05-24 18:58:21 +0000746 case ARM::INLINEASM:
747 case ARM::INLINEASM_BR: {
748 // If this machine instr is an inline asm, measure it.
749 unsigned Size = getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
750 if (!MF->getInfo<ARMFunctionInfo>()->isThumbFunction())
751 Size = alignTo(Size, 4);
752 return Size;
753 }
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;
Simon Tatham760df472019-05-28 16:13:20 +0000833 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.hasFP64())
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;
Simon Tatham760df472019-05-28 16:13:20 +0000893 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) &&
894 !Subtarget.hasFP64()) {
Oliver Stannard51b1d462014-08-21 12:50:31 +0000895 Opc = ARM::VMOVS;
896 BeginIdx = ARM::ssub_0;
897 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000898 } else if (SrcReg == ARM::CPSR) {
899 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
900 return;
901 } else if (DestReg == ARM::CPSR) {
902 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
903 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000904 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000905
Andrew Trickb57e2252012-08-29 04:41:37 +0000906 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000907
Andrew Trick4cc69492012-08-29 01:58:52 +0000908 const TargetRegisterInfo *TRI = &getRegisterInfo();
909 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000910
911 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
912 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000913 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000914 Spacing = -Spacing;
915 }
916#ifndef NDEBUG
917 SmallSet<unsigned, 4> DstRegs;
918#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000919 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000920 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
921 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000922 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000923#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000924 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000925 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000926#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000927 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000928 // VORR takes two source operands.
929 if (Opc == ARM::VORRq)
930 Mov.addReg(Src);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000931 Mov = Mov.add(predOps(ARMCC::AL));
JF Bastien583db652013-07-12 23:33:03 +0000932 // MOVr can set CC.
933 if (Opc == ARM::MOVr)
Diana Picus8a73f552017-01-13 10:18:01 +0000934 Mov = Mov.add(condCodeOp());
Andrew Trick4cc69492012-08-29 01:58:52 +0000935 }
936 // Add implicit super-register defs and kills to the last instruction.
937 Mov->addRegisterDefined(DestReg, TRI);
938 if (KillSrc)
939 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000940}
941
Alexander Ivchenkoaf961122018-08-30 14:32:47 +0000942bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
943 const MachineOperand *&Src,
944 const MachineOperand *&Dest) const {
Petar Jovanovicc0510002018-05-23 15:28:28 +0000945 // VMOVRRD is also a copy instruction but it requires
946 // special way of handling. It is more complex copy version
947 // and since that we are not considering it. For recognition
948 // of such instruction isExtractSubregLike MI interface fuction
949 // could be used.
950 // VORRq is considered as a move only if two inputs are
951 // the same register.
952 if (!MI.isMoveReg() ||
953 (MI.getOpcode() == ARM::VORRq &&
954 MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
955 return false;
Petar Jovanovic8cb6a522018-06-06 16:36:30 +0000956 Dest = &MI.getOperand(0);
957 Src = &MI.getOperand(1);
Petar Jovanovicc0510002018-05-23 15:28:28 +0000958 return true;
959}
960
Tim Northover798697d2013-04-21 11:57:07 +0000961const MachineInstrBuilder &
962ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
963 unsigned SubIdx, unsigned State,
964 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000965 if (!SubIdx)
966 return MIB.addReg(Reg, State);
967
968 if (TargetRegisterInfo::isPhysicalRegister(Reg))
969 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
970 return MIB.addReg(Reg, State, SubIdx);
971}
972
David Goodwinaf7451b2009-07-08 16:09:28 +0000973void ARMBaseInstrInfo::
974storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
975 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000976 const TargetRegisterClass *RC,
977 const TargetRegisterInfo *TRI) const {
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000978 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000979 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000980 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000981
Alex Lorenze40c8a22015-08-11 23:09:45 +0000982 MachineMemOperand *MMO = MF.getMachineMemOperand(
983 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
984 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000985
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000986 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000987 case 2:
988 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +0000989 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRH))
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000990 .addReg(SrcReg, getKillRegState(isKill))
991 .addFrameIndex(FI)
992 .addImm(0)
993 .addMemOperand(MMO)
994 .add(predOps(ARMCC::AL));
995 } else
996 llvm_unreachable("Unknown reg class!");
997 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000998 case 4:
999 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001000 BuildMI(MBB, I, DebugLoc(), get(ARM::STRi12))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001001 .addReg(SrcReg, getKillRegState(isKill))
1002 .addFrameIndex(FI)
1003 .addImm(0)
1004 .addMemOperand(MMO)
1005 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001006 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001007 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRS))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001008 .addReg(SrcReg, getKillRegState(isKill))
1009 .addFrameIndex(FI)
1010 .addImm(0)
1011 .addMemOperand(MMO)
1012 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001013 } else
1014 llvm_unreachable("Unknown reg class!");
1015 break;
1016 case 8:
1017 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001018 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRD))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001019 .addReg(SrcReg, getKillRegState(isKill))
1020 .addFrameIndex(FI)
1021 .addImm(0)
1022 .addMemOperand(MMO)
1023 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001024 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001025 if (Subtarget.hasV5TEOps()) {
Matthias Braun81578e92018-10-05 22:00:13 +00001026 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STRD));
Tim Northover798697d2013-04-21 11:57:07 +00001027 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1028 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001029 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1030 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001031 } else {
1032 // Fallback to STM instruction, which has existed since the dawn of
1033 // time.
Matthias Braun81578e92018-10-05 22:00:13 +00001034 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STMIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001035 .addFrameIndex(FI)
1036 .addMemOperand(MMO)
1037 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001038 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1039 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1040 }
Owen Anderson732f82c2011-08-10 17:21:20 +00001041 } else
1042 llvm_unreachable("Unknown reg class!");
1043 break;
1044 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001045 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001046 // Use aligned spills if the stack can be realigned.
1047 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001048 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1q64))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001049 .addFrameIndex(FI)
1050 .addImm(16)
1051 .addReg(SrcReg, getKillRegState(isKill))
1052 .addMemOperand(MMO)
1053 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001054 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001055 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMQIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001056 .addReg(SrcReg, getKillRegState(isKill))
1057 .addFrameIndex(FI)
1058 .addMemOperand(MMO)
1059 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001060 }
1061 } else
1062 llvm_unreachable("Unknown reg class!");
1063 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001064 case 24:
1065 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1066 // Use aligned spills if the stack can be realigned.
1067 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001068 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64TPseudo))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001069 .addFrameIndex(FI)
1070 .addImm(16)
1071 .addReg(SrcReg, getKillRegState(isKill))
1072 .addMemOperand(MMO)
1073 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001074 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001075 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1076 get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001077 .addFrameIndex(FI)
1078 .add(predOps(ARMCC::AL))
1079 .addMemOperand(MMO);
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001080 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1081 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1082 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1083 }
1084 } else
1085 llvm_unreachable("Unknown reg class!");
1086 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001087 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001088 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001089 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1090 // FIXME: It's possible to only store part of the QQ register if the
1091 // spilled def has a sub-register index.
Matthias Braun81578e92018-10-05 22:00:13 +00001092 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64QPseudo))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001093 .addFrameIndex(FI)
1094 .addImm(16)
1095 .addReg(SrcReg, getKillRegState(isKill))
1096 .addMemOperand(MMO)
1097 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001098 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001099 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1100 get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001101 .addFrameIndex(FI)
1102 .add(predOps(ARMCC::AL))
1103 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001104 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1105 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1106 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1107 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1108 }
1109 } else
1110 llvm_unreachable("Unknown reg class!");
1111 break;
1112 case 64:
1113 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001114 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001115 .addFrameIndex(FI)
1116 .add(predOps(ARMCC::AL))
1117 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001118 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1119 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1120 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1121 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1122 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1123 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1124 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1125 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1126 } else
1127 llvm_unreachable("Unknown reg class!");
1128 break;
1129 default:
1130 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001131 }
1132}
1133
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001134unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1135 int &FrameIndex) const {
1136 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001137 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001138 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001139 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001140 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1141 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1142 MI.getOperand(3).getImm() == 0) {
1143 FrameIndex = MI.getOperand(1).getIndex();
1144 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001145 }
1146 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001147 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001148 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001149 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001150 case ARM::VSTRD:
1151 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001152 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1153 MI.getOperand(2).getImm() == 0) {
1154 FrameIndex = MI.getOperand(1).getIndex();
1155 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001156 }
1157 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001158 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001159 case ARM::VST1d64TPseudo:
1160 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001161 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1162 FrameIndex = MI.getOperand(0).getIndex();
1163 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001164 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001165 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001166 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001167 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1168 FrameIndex = MI.getOperand(1).getIndex();
1169 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001170 }
1171 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001172 }
1173
1174 return 0;
1175}
1176
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001177unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001178 int &FrameIndex) const {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001179 SmallVector<const MachineMemOperand *, 1> Accesses;
Eli Friedman1e5d5692019-03-25 22:42:30 +00001180 if (MI.mayStore() && hasStoreToStackSlot(MI, Accesses) &&
1181 Accesses.size() == 1) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001182 FrameIndex =
1183 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1184 ->getFrameIndex();
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001185 return true;
1186 }
1187 return false;
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001188}
1189
David Goodwinaf7451b2009-07-08 16:09:28 +00001190void ARMBaseInstrInfo::
1191loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1192 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001193 const TargetRegisterClass *RC,
1194 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001195 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001196 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001197 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001198 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001199 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001200 MachineMemOperand *MMO = MF.getMachineMemOperand(
1201 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1202 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001203
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001204 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +00001205 case 2:
1206 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
1207 BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
1208 .addFrameIndex(FI)
1209 .addImm(0)
1210 .addMemOperand(MMO)
1211 .add(predOps(ARMCC::AL));
1212 } else
1213 llvm_unreachable("Unknown reg class!");
1214 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001215 case 4:
1216 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001217 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1218 .addFrameIndex(FI)
1219 .addImm(0)
1220 .addMemOperand(MMO)
1221 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001222 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001223 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1224 .addFrameIndex(FI)
1225 .addImm(0)
1226 .addMemOperand(MMO)
1227 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001228 } else
1229 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001230 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001231 case 8:
1232 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001233 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1234 .addFrameIndex(FI)
1235 .addImm(0)
1236 .addMemOperand(MMO)
1237 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001238 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001239 MachineInstrBuilder MIB;
1240
1241 if (Subtarget.hasV5TEOps()) {
1242 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1243 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1244 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001245 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1246 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001247 } else {
1248 // Fallback to LDM instruction, which has existed since the dawn of
1249 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001250 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1251 .addFrameIndex(FI)
1252 .addMemOperand(MMO)
1253 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001254 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1255 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1256 }
1257
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001258 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1259 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001260 } else
1261 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001262 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001263 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001264 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001265 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001266 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1267 .addFrameIndex(FI)
1268 .addImm(16)
1269 .addMemOperand(MMO)
1270 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001271 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001272 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1273 .addFrameIndex(FI)
1274 .addMemOperand(MMO)
1275 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001276 }
1277 } else
1278 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001279 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001280 case 24:
1281 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1282 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001283 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1284 .addFrameIndex(FI)
1285 .addImm(16)
1286 .addMemOperand(MMO)
1287 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001288 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001289 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1290 .addFrameIndex(FI)
1291 .addMemOperand(MMO)
1292 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001293 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1294 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1295 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1296 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1297 MIB.addReg(DestReg, RegState::ImplicitDefine);
1298 }
1299 } else
1300 llvm_unreachable("Unknown reg class!");
1301 break;
1302 case 32:
1303 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001304 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001305 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1306 .addFrameIndex(FI)
1307 .addImm(16)
1308 .addMemOperand(MMO)
1309 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001310 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001311 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1312 .addFrameIndex(FI)
1313 .add(predOps(ARMCC::AL))
1314 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001315 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1316 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1317 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1318 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001319 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1320 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001321 }
1322 } else
1323 llvm_unreachable("Unknown reg class!");
1324 break;
1325 case 64:
1326 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001327 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1328 .addFrameIndex(FI)
1329 .add(predOps(ARMCC::AL))
1330 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001331 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1332 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1333 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1334 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1335 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1336 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1337 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1338 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001339 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1340 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001341 } else
1342 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001343 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001344 default:
1345 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001346 }
1347}
1348
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001349unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1350 int &FrameIndex) const {
1351 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001352 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001353 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001354 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001355 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1356 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1357 MI.getOperand(3).getImm() == 0) {
1358 FrameIndex = MI.getOperand(1).getIndex();
1359 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001360 }
1361 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001362 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001363 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001364 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001365 case ARM::VLDRD:
1366 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001367 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1368 MI.getOperand(2).getImm() == 0) {
1369 FrameIndex = MI.getOperand(1).getIndex();
1370 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001371 }
1372 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001373 case ARM::VLD1q64:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001374 case ARM::VLD1d8TPseudo:
1375 case ARM::VLD1d16TPseudo:
1376 case ARM::VLD1d32TPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001377 case ARM::VLD1d64TPseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001378 case ARM::VLD1d8QPseudo:
1379 case ARM::VLD1d16QPseudo:
1380 case ARM::VLD1d32QPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001381 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001382 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1383 FrameIndex = MI.getOperand(1).getIndex();
1384 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001385 }
1386 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001387 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001388 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1389 FrameIndex = MI.getOperand(1).getIndex();
1390 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001391 }
1392 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001393 }
1394
1395 return 0;
1396}
1397
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001398unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1399 int &FrameIndex) const {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001400 SmallVector<const MachineMemOperand *, 1> Accesses;
Eli Friedman1e5d5692019-03-25 22:42:30 +00001401 if (MI.mayLoad() && hasLoadFromStackSlot(MI, Accesses) &&
1402 Accesses.size() == 1) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001403 FrameIndex =
1404 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1405 ->getFrameIndex();
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001406 return true;
1407 }
1408 return false;
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001409}
1410
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001411/// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
Scott Douglass953f9082015-10-05 14:49:54 +00001412/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001413void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001414 bool isThumb1 = Subtarget.isThumb1Only();
1415 bool isThumb2 = Subtarget.isThumb2();
1416 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1417
Scott Douglass953f9082015-10-05 14:49:54 +00001418 DebugLoc dl = MI->getDebugLoc();
1419 MachineBasicBlock *BB = MI->getParent();
1420
1421 MachineInstrBuilder LDM, STM;
1422 if (isThumb1 || !MI->getOperand(1).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001423 MachineOperand LDWb(MI->getOperand(1));
Scott Douglass953f9082015-10-05 14:49:54 +00001424 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1425 : isThumb1 ? ARM::tLDMIA_UPD
1426 : ARM::LDMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001427 .add(LDWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001428 } else {
1429 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1430 }
1431
1432 if (isThumb1 || !MI->getOperand(0).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001433 MachineOperand STWb(MI->getOperand(0));
Scott Douglass953f9082015-10-05 14:49:54 +00001434 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1435 : isThumb1 ? ARM::tSTMIA_UPD
1436 : ARM::STMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001437 .add(STWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001438 } else {
1439 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1440 }
1441
Geoff Berry60c43102017-12-12 17:53:59 +00001442 MachineOperand LDBase(MI->getOperand(3));
Geoff Berry60c43102017-12-12 17:53:59 +00001443 LDM.add(LDBase).add(predOps(ARMCC::AL));
1444
1445 MachineOperand STBase(MI->getOperand(2));
Geoff Berry60c43102017-12-12 17:53:59 +00001446 STM.add(STBase).add(predOps(ARMCC::AL));
Scott Douglass953f9082015-10-05 14:49:54 +00001447
1448 // Sort the scratch registers into ascending order.
1449 const TargetRegisterInfo &TRI = getRegisterInfo();
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001450 SmallVector<unsigned, 6> ScratchRegs;
Scott Douglass953f9082015-10-05 14:49:54 +00001451 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1452 ScratchRegs.push_back(MI->getOperand(I).getReg());
Fangrui Song0cac7262018-09-27 02:13:45 +00001453 llvm::sort(ScratchRegs,
1454 [&TRI](const unsigned &Reg1, const unsigned &Reg2) -> bool {
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001455 return TRI.getEncodingValue(Reg1) <
1456 TRI.getEncodingValue(Reg2);
1457 });
Scott Douglass953f9082015-10-05 14:49:54 +00001458
1459 for (const auto &Reg : ScratchRegs) {
1460 LDM.addReg(Reg, RegState::Define);
1461 STM.addReg(Reg, RegState::Kill);
1462 }
1463
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001464 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001465}
1466
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001467bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1468 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001469 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001470 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001471 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001472 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001473 return true;
1474 }
1475
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001476 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001477 expandMEMCPY(MI);
1478 return true;
1479 }
1480
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001481 // This hook gets to expand COPY instructions before they become
1482 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1483 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1484 // changed into a VORR that can go down the NEON pipeline.
Simon Tatham760df472019-05-28 16:13:20 +00001485 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || !Subtarget.hasFP64())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001486 return false;
1487
1488 // Look for a copy between even S-registers. That is where we keep floats
1489 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001490 unsigned DstRegS = MI.getOperand(0).getReg();
1491 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001492 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1493 return false;
1494
1495 const TargetRegisterInfo *TRI = &getRegisterInfo();
1496 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1497 &ARM::DPRRegClass);
1498 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1499 &ARM::DPRRegClass);
1500 if (!DstRegD || !SrcRegD)
1501 return false;
1502
1503 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1504 // legal if the COPY already defines the full DstRegD, and it isn't a
1505 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001506 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001507 return false;
1508
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001509 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001510 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001511 return false;
1512
1513 // All clear, widen the COPY.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001514 LLVM_DEBUG(dbgs() << "widening: " << MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001515 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001516
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001517 // 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 +00001518 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001519 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001520 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001521 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001522
1523 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001524 MI.setDesc(get(ARM::VMOVD));
1525 MI.getOperand(0).setReg(DstRegD);
1526 MI.getOperand(1).setReg(SrcRegD);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001527 MIB.add(predOps(ARMCC::AL));
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001528
1529 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1530 // register scavenger and machine verifier, so we need to indicate that we
1531 // are reading an undefined value from SrcRegD, but a proper value from
1532 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001533 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001534 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001535
1536 // SrcRegD may actually contain an unrelated value in the ssub_1
1537 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001538 if (MI.getOperand(1).isKill()) {
1539 MI.getOperand(1).setIsKill(false);
1540 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001541 }
1542
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001543 LLVM_DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001544 return true;
1545}
1546
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001547/// Create a copy of a const pool value. Update CPI to the new index and return
1548/// the label UID.
1549static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1550 MachineConstantPool *MCP = MF.getConstantPool();
1551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1552
1553 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1554 assert(MCPE.isMachineConstantPoolEntry() &&
1555 "Expecting a machine constantpool entry!");
1556 ARMConstantPoolValue *ACPV =
1557 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1558
Evan Chengdfce83c2011-01-17 08:03:18 +00001559 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001560 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001561
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001562 // FIXME: The below assumes PIC relocation model and that the function
1563 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1564 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1565 // instructions, so that's probably OK, but is PIC always correct when
1566 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001567 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001568 NewCPV = ARMConstantPoolConstant::Create(
1569 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1570 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001571 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001572 NewCPV = ARMConstantPoolSymbol::
Matthias Braunf1caa282017-12-15 22:22:58 +00001573 Create(MF.getFunction().getContext(),
Bill Wendlingc214cb02011-10-01 08:58:29 +00001574 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001575 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001576 NewCPV = ARMConstantPoolConstant::
1577 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1578 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001579 else if (ACPV->isLSDA())
Matthias Braunf1caa282017-12-15 22:22:58 +00001580 NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
Bill Wendling7753d662011-10-01 08:00:54 +00001581 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001582 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001583 NewCPV = ARMConstantPoolMBB::
Matthias Braunf1caa282017-12-15 22:22:58 +00001584 Create(MF.getFunction().getContext(),
Bill Wendling4a4772f2011-10-01 09:30:42 +00001585 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001586 else
1587 llvm_unreachable("Unexpected ARM constantpool value type!!");
1588 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1589 return PCLabelId;
1590}
1591
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001592void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1593 MachineBasicBlock::iterator I,
1594 unsigned DestReg, unsigned SubIdx,
1595 const MachineInstr &Orig,
1596 const TargetRegisterInfo &TRI) const {
1597 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001598 switch (Opcode) {
1599 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001600 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1601 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001602 MBB.insert(I, MI);
1603 break;
1604 }
1605 case ARM::tLDRpci_pic:
1606 case ARM::t2LDRpci_pic: {
1607 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001608 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001609 unsigned PCLabelId = duplicateCPV(MF, CPI);
Chandler Carruthc73c0302018-08-16 21:30:05 +00001610 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1611 .addConstantPoolIndex(CPI)
1612 .addImm(PCLabelId)
1613 .cloneMemRefs(Orig);
Evan Chengfe864422009-11-08 00:15:23 +00001614 break;
1615 }
1616 }
Evan Chengfe864422009-11-08 00:15:23 +00001617}
1618
Matthias Braun55bc9b32017-08-22 23:56:30 +00001619MachineInstr &
1620ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1621 MachineBasicBlock::iterator InsertBefore,
1622 const MachineInstr &Orig) const {
1623 MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1624 MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1625 for (;;) {
1626 switch (I->getOpcode()) {
1627 case ARM::tLDRpci_pic:
1628 case ARM::t2LDRpci_pic: {
1629 MachineFunction &MF = *MBB.getParent();
1630 unsigned CPI = I->getOperand(1).getIndex();
1631 unsigned PCLabelId = duplicateCPV(MF, CPI);
1632 I->getOperand(1).setIndex(CPI);
1633 I->getOperand(2).setImm(PCLabelId);
1634 break;
1635 }
1636 }
1637 if (!I->isBundledWithSucc())
1638 break;
1639 ++I;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001640 }
Matthias Braun55bc9b32017-08-22 23:56:30 +00001641 return Cloned;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001642}
1643
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001644bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1645 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001646 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001647 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001648 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001649 Opcode == ARM::t2LDRpci_pic ||
1650 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001651 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001652 Opcode == ARM::LDRLIT_ga_pcrel ||
1653 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1654 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001655 Opcode == ARM::MOV_ga_pcrel ||
1656 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001657 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001658 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001659 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001660 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001661 return false;
1662
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001663 const MachineOperand &MO0 = MI0.getOperand(1);
1664 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001665 if (MO0.getOffset() != MO1.getOffset())
1666 return false;
1667
Tim Northover72360d22013-12-02 10:35:41 +00001668 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1669 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1670 Opcode == ARM::tLDRLIT_ga_pcrel ||
1671 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001672 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001673 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001674 // Ignore the PC labels.
1675 return MO0.getGlobal() == MO1.getGlobal();
1676
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001677 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001678 const MachineConstantPool *MCP = MF->getConstantPool();
1679 int CPI0 = MO0.getIndex();
1680 int CPI1 = MO1.getIndex();
1681 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1682 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001683 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1684 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1685 if (isARMCP0 && isARMCP1) {
1686 ARMConstantPoolValue *ACPV0 =
1687 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1688 ARMConstantPoolValue *ACPV1 =
1689 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1690 return ACPV0->hasSameValue(ACPV1);
1691 } else if (!isARMCP0 && !isARMCP1) {
1692 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1693 }
1694 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001695 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001696 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001697 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001698 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001699 return false;
1700
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001701 unsigned Addr0 = MI0.getOperand(1).getReg();
1702 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001703 if (Addr0 != Addr1) {
1704 if (!MRI ||
1705 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1706 !TargetRegisterInfo::isVirtualRegister(Addr1))
1707 return false;
1708
1709 // This assumes SSA form.
1710 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1711 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1712 // Check if the loaded value, e.g. a constantpool of a global address, are
1713 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001714 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001715 return false;
1716 }
1717
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001718 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00001719 // %12 = PICLDR %11, 0, 14, %noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001720 const MachineOperand &MO0 = MI0.getOperand(i);
1721 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001722 if (!MO0.isIdenticalTo(MO1))
1723 return false;
1724 }
1725 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001726 }
1727
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001728 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001729}
1730
Bill Wendlingf4707472010-06-23 23:00:16 +00001731/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1732/// determine if two loads are loading from the same base address. It should
1733/// only return true if the base pointers are the same and the only differences
1734/// between the two addresses is the offset. It also returns the offsets by
1735/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001736///
1737/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1738/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001739bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1740 int64_t &Offset1,
1741 int64_t &Offset2) const {
1742 // Don't worry about Thumb: just ARM and Thumb2.
1743 if (Subtarget.isThumb1Only()) return false;
1744
1745 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1746 return false;
1747
1748 switch (Load1->getMachineOpcode()) {
1749 default:
1750 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001751 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001752 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001753 case ARM::LDRD:
1754 case ARM::LDRH:
1755 case ARM::LDRSB:
1756 case ARM::LDRSH:
1757 case ARM::VLDRD:
1758 case ARM::VLDRS:
1759 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001760 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001761 case ARM::t2LDRDi8:
1762 case ARM::t2LDRSHi8:
1763 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001764 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001765 case ARM::t2LDRSHi12:
1766 break;
1767 }
1768
1769 switch (Load2->getMachineOpcode()) {
1770 default:
1771 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001772 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001773 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001774 case ARM::LDRD:
1775 case ARM::LDRH:
1776 case ARM::LDRSB:
1777 case ARM::LDRSH:
1778 case ARM::VLDRD:
1779 case ARM::VLDRS:
1780 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001781 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001782 case ARM::t2LDRSHi8:
1783 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001784 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001785 case ARM::t2LDRSHi12:
1786 break;
1787 }
1788
1789 // Check if base addresses and chain operands match.
1790 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1791 Load1->getOperand(4) != Load2->getOperand(4))
1792 return false;
1793
1794 // Index should be Reg0.
1795 if (Load1->getOperand(3) != Load2->getOperand(3))
1796 return false;
1797
1798 // Determine the offsets.
1799 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1800 isa<ConstantSDNode>(Load2->getOperand(1))) {
1801 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1802 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1803 return true;
1804 }
1805
1806 return false;
1807}
1808
1809/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001810/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001811/// be scheduled togther. On some targets if two loads are loading from
1812/// addresses in the same cache line, it's better if they are scheduled
1813/// together. This function takes two integers that represent the load offsets
1814/// from the common base address. It returns true if it decides it's desirable
1815/// to schedule the two loads together. "NumLoads" is the number of loads that
1816/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001817///
1818/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1819/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001820bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1821 int64_t Offset1, int64_t Offset2,
1822 unsigned NumLoads) const {
1823 // Don't worry about Thumb: just ARM and Thumb2.
1824 if (Subtarget.isThumb1Only()) return false;
1825
1826 assert(Offset2 > Offset1);
1827
1828 if ((Offset2 - Offset1) / 8 > 64)
1829 return false;
1830
Renato Golinb184cd92013-08-14 16:35:29 +00001831 // Check if the machine opcodes are different. If they are different
1832 // then we consider them to not be of the same base address,
1833 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1834 // In this case, they are considered to be the same because they are different
1835 // encoding forms of the same basic instruction.
1836 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1837 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1838 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1839 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1840 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001841 return false; // FIXME: overly conservative?
1842
1843 // Four loads in a row should be sufficient.
1844 if (NumLoads >= 3)
1845 return false;
1846
1847 return true;
1848}
1849
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001850bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001851 const MachineBasicBlock *MBB,
1852 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001853 // Debug info is never a scheduling boundary. It's necessary to be explicit
1854 // due to the special treatment of IT instructions below, otherwise a
1855 // dbg_value followed by an IT will result in the IT instruction being
1856 // considered a scheduling hazard, which is wrong. It should be the actual
1857 // instruction preceding the dbg_value instruction(s), just like it is
1858 // when debug info is not present.
Shiva Chen801bf7e2018-05-09 02:42:00 +00001859 if (MI.isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001860 return false;
1861
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001862 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001863 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001864 return true;
1865
1866 // Treat the start of the IT block as a scheduling boundary, but schedule
1867 // t2IT along with all instructions following it.
1868 // FIXME: This is a big hammer. But the alternative is to add all potential
1869 // true and anti dependencies to IT block instructions as implicit operands
1870 // to the t2IT instruction. The added compile time and complexity does not
1871 // seem worth it.
1872 MachineBasicBlock::const_iterator I = MI;
Shiva Chen801bf7e2018-05-09 02:42:00 +00001873 // Make sure to skip any debug instructions
1874 while (++I != MBB->end() && I->isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001875 ;
1876 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001877 return true;
1878
1879 // Don't attempt to schedule around any instruction that defines
1880 // a stack-oriented pointer, as it's unlikely to be profitable. This
1881 // saves compile time, because it doesn't require every single
1882 // stack slot reference to depend on the instruction that does the
1883 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001884 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001885 // No ARM calling conventions change the stack pointer. (X86 calling
1886 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001887 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001888 return true;
1889
1890 return false;
1891}
1892
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001893bool ARMBaseInstrInfo::
1894isProfitableToIfCvt(MachineBasicBlock &MBB,
1895 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001896 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001897 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001898 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001899
Peter Collingbourne65295232015-04-23 20:31:30 +00001900 // If we are optimizing for size, see if the branch in the predecessor can be
1901 // lowered to cbn?z by the constant island lowering pass, and return false if
1902 // so. This results in a shorter instruction sequence.
Evandro Menezes85bd3972019-04-04 22:40:06 +00001903 if (MBB.getParent()->getFunction().hasOptSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001904 MachineBasicBlock *Pred = *MBB.pred_begin();
1905 if (!Pred->empty()) {
1906 MachineInstr *LastMI = &*Pred->rbegin();
1907 if (LastMI->getOpcode() == ARM::t2Bcc) {
David Greenc519d3c2019-04-23 12:11:26 +00001908 const TargetRegisterInfo *TRI = &getRegisterInfo();
1909 MachineInstr *CmpMI = findCMPToFoldIntoCBZ(LastMI, TRI);
1910 if (CmpMI)
1911 return false;
Peter Collingbourne65295232015-04-23 20:31:30 +00001912 }
1913 }
1914 }
Artyom Skrobov283316b2017-03-14 13:38:45 +00001915 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1916 MBB, 0, 0, Probability);
Evan Cheng02b184d2010-06-25 22:42:03 +00001917}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001918
Evan Cheng02b184d2010-06-25 22:42:03 +00001919bool ARMBaseInstrInfo::
John Brawn75d76e52017-06-28 14:11:15 +00001920isProfitableToIfCvt(MachineBasicBlock &TBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001921 unsigned TCycles, unsigned TExtra,
John Brawn75d76e52017-06-28 14:11:15 +00001922 MachineBasicBlock &FBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001923 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001924 BranchProbability Probability) const {
Artyom Skrobov283316b2017-03-14 13:38:45 +00001925 if (!TCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001926 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001927
David Green2f9eed62019-04-23 11:46:58 +00001928 // In thumb code we often end up trading one branch for a IT block, and
1929 // if we are cloning the instruction can increase code size. Prevent
1930 // blocks with multiple predecesors from being ifcvted to prevent this
1931 // cloning.
1932 if (Subtarget.isThumb2() && TBB.getParent()->getFunction().hasMinSize()) {
1933 if (TBB.pred_size() != 1 || FBB.pred_size() != 1)
1934 return false;
1935 }
1936
Owen Anderson88af7d02010-09-28 18:32:13 +00001937 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001938 // Here we scale up each component of UnpredCost to avoid precision issue when
1939 // scaling TCycles/FCycles by Probability.
1940 const unsigned ScalingUpFactor = 1024;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001941
John Brawn75d76e52017-06-28 14:11:15 +00001942 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1943 unsigned UnpredCost;
1944 if (!Subtarget.hasBranchPredictor()) {
1945 // When we don't have a branch predictor it's always cheaper to not take a
1946 // branch than take it, so we have to take that into account.
1947 unsigned NotTakenBranchCost = 1;
1948 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1949 unsigned TUnpredCycles, FUnpredCycles;
1950 if (!FCycles) {
1951 // Triangle: TBB is the fallthrough
1952 TUnpredCycles = TCycles + NotTakenBranchCost;
1953 FUnpredCycles = TakenBranchCost;
1954 } else {
1955 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1956 TUnpredCycles = TCycles + TakenBranchCost;
1957 FUnpredCycles = FCycles + NotTakenBranchCost;
John Brawn97cc2832017-07-12 13:23:10 +00001958 // The branch at the end of FBB will disappear when it's predicated, so
1959 // discount it from PredCost.
1960 PredCost -= 1 * ScalingUpFactor;
John Brawn75d76e52017-06-28 14:11:15 +00001961 }
1962 // The total cost is the cost of each path scaled by their probabilites
1963 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1964 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1965 UnpredCost = TUnpredCost + FUnpredCost;
1966 // When predicating assume that the first IT can be folded away but later
1967 // ones cost one cycle each
1968 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1969 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1970 }
1971 } else {
1972 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1973 unsigned FUnpredCost =
1974 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1975 UnpredCost = TUnpredCost + FUnpredCost;
1976 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1977 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1978 }
1979
1980 return PredCost <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001981}
1982
Bob Wilsone8a549c2012-09-29 21:43:49 +00001983bool
1984ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1985 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001986 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001987 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001988 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001989}
1990
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001991/// getInstrPredicate - If instruction is predicated, returns its predicate
1992/// condition, otherwise returns AL. It also returns the condition code
1993/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001994ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1995 unsigned &PredReg) {
1996 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001997 if (PIdx == -1) {
1998 PredReg = 0;
1999 return ARMCC::AL;
2000 }
2001
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002002 PredReg = MI.getOperand(PIdx+1).getReg();
2003 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00002004}
2005
Matthias Braunfa3872e2015-05-18 20:27:55 +00002006unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00002007 if (Opc == ARM::B)
2008 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00002009 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00002010 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00002011 if (Opc == ARM::t2B)
2012 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00002013
2014 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00002015}
2016
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002017MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002018 bool NewMI,
2019 unsigned OpIdx1,
2020 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002021 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002022 case ARM::MOVCCr:
2023 case ARM::t2MOVCCr: {
2024 // MOVCC can be commuted by inverting the condition.
2025 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002026 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002027 // MOVCC AL can't be inverted. Shouldn't happen.
2028 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00002029 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002030 MachineInstr *CommutedMI =
2031 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2032 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002033 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002034 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002035 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
2036 .setImm(ARMCC::getOppositeCondition(CC));
2037 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002038 }
2039 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002040 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002041}
Evan Cheng780748d2009-07-28 05:48:47 +00002042
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002043/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002044/// return the defining instruction.
2045static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
2046 const MachineRegisterInfo &MRI,
2047 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002048 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002049 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002050 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002051 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002052 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002053 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002054 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002055 // MI is folded into the MOVCC by predicating it.
2056 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00002057 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002058 // Check if MI has any non-dead defs or physreg uses. This also detects
2059 // predicated instructions which will be reading CPSR.
2060 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
2061 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00002062 // Reject frame index operands, PEI can't handle the predicated pseudos.
2063 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00002064 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002065 if (!MO.isReg())
2066 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002067 // MI can't have any tied operands, that would conflict with predication.
2068 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00002069 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002070 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00002071 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002072 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00002073 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002074 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002075 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00002076 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00002077 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002078 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002079}
2080
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002081bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002082 SmallVectorImpl<MachineOperand> &Cond,
2083 unsigned &TrueOp, unsigned &FalseOp,
2084 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002085 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002086 "Unknown select instruction");
2087 // MOVCC operands:
2088 // 0: Def.
2089 // 1: True use.
2090 // 2: False use.
2091 // 3: Condition code.
2092 // 4: CPSR use.
2093 TrueOp = 1;
2094 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002095 Cond.push_back(MI.getOperand(3));
2096 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002097 // We can always fold a def.
2098 Optimizable = true;
2099 return false;
2100}
2101
Mehdi Amini22e59742015-01-13 07:07:13 +00002102MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002103ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00002104 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2105 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002106 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002107 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002108 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2109 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002110 bool Invert = !DefMI;
2111 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002112 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002113 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002114 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002115
Matthias Braun2f169f92013-10-04 16:52:56 +00002116 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002117 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2118 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00002119 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2120 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00002121 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00002122
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002123 // Create a new predicated version of DefMI.
2124 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002125 MachineInstrBuilder NewMI =
2126 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002127
2128 // Copy all the DefMI operands, excluding its (null) predicate.
2129 const MCInstrDesc &DefDesc = DefMI->getDesc();
2130 for (unsigned i = 1, e = DefDesc.getNumOperands();
2131 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00002132 NewMI.add(DefMI->getOperand(i));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002133
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002134 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002135 if (Invert)
2136 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2137 else
2138 NewMI.addImm(CondCode);
Diana Picus116bbab2017-01-13 09:58:52 +00002139 NewMI.add(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002140
2141 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2142 if (NewMI->hasOptionalDef())
Diana Picus8a73f552017-01-13 10:18:01 +00002143 NewMI.add(condCodeOp());
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002144
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002145 // The output register value when the predicate is false is an implicit
2146 // register operand tied to the first def.
2147 // The tie makes the register allocator ensure the FalseReg is allocated the
2148 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002149 FalseReg.setImplicit();
Diana Picus116bbab2017-01-13 09:58:52 +00002150 NewMI.add(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002151 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2152
Mehdi Amini22e59742015-01-13 07:07:13 +00002153 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2154 SeenMIs.insert(NewMI);
2155 SeenMIs.erase(DefMI);
2156
Pete Cooper2127b002015-04-30 23:57:47 +00002157 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2158 // DefMI would be invalid when tranferred inside the loop. Checking for a
2159 // loop is expensive, but at least remove kill flags if they are in different
2160 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002161 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00002162 NewMI->clearKillInfo();
2163
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002164 // The caller will erase MI, but not DefMI.
2165 DefMI->eraseFromParent();
2166 return NewMI;
2167}
2168
Andrew Trick924123a2011-09-21 02:20:46 +00002169/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2170/// instruction is encoded with an 'S' bit is determined by the optional CPSR
2171/// def operand.
2172///
2173/// This will go away once we can teach tblgen how to set the optional CPSR def
2174/// operand itself.
2175struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00002176 uint16_t PseudoOpc;
2177 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002178};
2179
Craig Topper2fbd1302012-05-24 03:59:11 +00002180static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00002181 {ARM::ADDSri, ARM::ADDri},
2182 {ARM::ADDSrr, ARM::ADDrr},
2183 {ARM::ADDSrsi, ARM::ADDrsi},
2184 {ARM::ADDSrsr, ARM::ADDrsr},
2185
2186 {ARM::SUBSri, ARM::SUBri},
2187 {ARM::SUBSrr, ARM::SUBrr},
2188 {ARM::SUBSrsi, ARM::SUBrsi},
2189 {ARM::SUBSrsr, ARM::SUBrsr},
2190
2191 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00002192 {ARM::RSBSrsi, ARM::RSBrsi},
2193 {ARM::RSBSrsr, ARM::RSBrsr},
2194
Artyom Skrobov92c06532017-03-22 23:35:51 +00002195 {ARM::tADDSi3, ARM::tADDi3},
2196 {ARM::tADDSi8, ARM::tADDi8},
2197 {ARM::tADDSrr, ARM::tADDrr},
2198 {ARM::tADCS, ARM::tADC},
2199
2200 {ARM::tSUBSi3, ARM::tSUBi3},
2201 {ARM::tSUBSi8, ARM::tSUBi8},
2202 {ARM::tSUBSrr, ARM::tSUBrr},
2203 {ARM::tSBCS, ARM::tSBC},
Eli Friedman063fd982018-10-31 21:45:48 +00002204 {ARM::tRSBS, ARM::tRSB},
Artyom Skrobov92c06532017-03-22 23:35:51 +00002205
Andrew Trick924123a2011-09-21 02:20:46 +00002206 {ARM::t2ADDSri, ARM::t2ADDri},
2207 {ARM::t2ADDSrr, ARM::t2ADDrr},
2208 {ARM::t2ADDSrs, ARM::t2ADDrs},
2209
2210 {ARM::t2SUBSri, ARM::t2SUBri},
2211 {ARM::t2SUBSrr, ARM::t2SUBrr},
2212 {ARM::t2SUBSrs, ARM::t2SUBrs},
2213
2214 {ARM::t2RSBSri, ARM::t2RSBri},
2215 {ARM::t2RSBSrs, ARM::t2RSBrs},
2216};
2217
2218unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00002219 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2220 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2221 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002222 return 0;
2223}
2224
Evan Cheng780748d2009-07-28 05:48:47 +00002225void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002226 MachineBasicBlock::iterator &MBBI,
2227 const DebugLoc &dl, unsigned DestReg,
2228 unsigned BaseReg, int NumBytes,
2229 ARMCC::CondCodes Pred, unsigned PredReg,
2230 const ARMBaseInstrInfo &TII,
2231 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002232 if (NumBytes == 0 && DestReg != BaseReg) {
2233 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002234 .addReg(BaseReg, RegState::Kill)
2235 .add(predOps(Pred, PredReg))
2236 .add(condCodeOp())
2237 .setMIFlags(MIFlags);
Tim Northoverc9432eb2013-11-04 23:04:15 +00002238 return;
2239 }
2240
Evan Cheng780748d2009-07-28 05:48:47 +00002241 bool isSub = NumBytes < 0;
2242 if (isSub) NumBytes = -NumBytes;
2243
2244 while (NumBytes) {
2245 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2246 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2247 assert(ThisVal && "Didn't extract field correctly");
2248
2249 // We will handle these bits from offset, clear them.
2250 NumBytes &= ~ThisVal;
2251
2252 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2253
2254 // Build the new ADD / SUB.
2255 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2256 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002257 .addReg(BaseReg, RegState::Kill)
2258 .addImm(ThisVal)
2259 .add(predOps(Pred, PredReg))
2260 .add(condCodeOp())
2261 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002262 BaseReg = DestReg;
2263 }
2264}
2265
Tim Northoverdee86042013-12-02 14:46:26 +00002266bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2267 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002268 unsigned NumBytes) {
2269 // This optimisation potentially adds lots of load and store
2270 // micro-operations, it's only really a great benefit to code-size.
Evandro Menezes85bd3972019-04-04 22:40:06 +00002271 if (!Subtarget.hasMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002272 return false;
2273
2274 // If only one register is pushed/popped, LLVM can use an LDR/STR
2275 // instead. We can't modify those so make sure we're dealing with an
2276 // instruction we understand.
2277 bool IsPop = isPopOpcode(MI->getOpcode());
2278 bool IsPush = isPushOpcode(MI->getOpcode());
2279 if (!IsPush && !IsPop)
2280 return false;
2281
2282 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2283 MI->getOpcode() == ARM::VLDMDIA_UPD;
2284 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2285 MI->getOpcode() == ARM::tPOP ||
2286 MI->getOpcode() == ARM::tPOP_RET;
2287
2288 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2289 MI->getOperand(1).getReg() == ARM::SP)) &&
2290 "trying to fold sp update into non-sp-updating push/pop");
2291
2292 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2293 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2294 // if this is violated.
2295 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2296 return false;
2297
2298 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2299 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2300 int RegListIdx = IsT1PushPop ? 2 : 4;
2301
2302 // Calculate the space we'll need in terms of registers.
Tim Northovera9cc3852016-10-26 20:01:00 +00002303 unsigned RegsNeeded;
2304 const TargetRegisterClass *RegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002305 if (IsVFPPushPop) {
Tim Northover93bcc662013-11-08 17:18:07 +00002306 RegsNeeded = NumBytes / 8;
Tim Northovera9cc3852016-10-26 20:01:00 +00002307 RegClass = &ARM::DPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002308 } else {
Tim Northover93bcc662013-11-08 17:18:07 +00002309 RegsNeeded = NumBytes / 4;
Tim Northovera9cc3852016-10-26 20:01:00 +00002310 RegClass = &ARM::GPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002311 }
2312
2313 // We're going to have to strip all list operands off before
2314 // re-adding them since the order matters, so save the existing ones
2315 // for later.
2316 SmallVector<MachineOperand, 4> RegList;
Tim Northovera9cc3852016-10-26 20:01:00 +00002317
2318 // We're also going to need the first register transferred by this
2319 // instruction, which won't necessarily be the first register in the list.
2320 unsigned FirstRegEnc = -1;
Tim Northover93bcc662013-11-08 17:18:07 +00002321
Tim Northover93bcc662013-11-08 17:18:07 +00002322 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northovera9cc3852016-10-26 20:01:00 +00002323 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2324 MachineOperand &MO = MI->getOperand(i);
2325 RegList.push_back(MO);
2326
2327 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2328 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2329 }
2330
Tim Northover45479dc2013-12-01 14:16:24 +00002331 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002332
2333 // Now try to find enough space in the reglist to allocate NumBytes.
Tim Northovera9cc3852016-10-26 20:01:00 +00002334 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2335 --CurRegEnc) {
2336 unsigned CurReg = RegClass->getRegister(CurRegEnc);
Eli Friedman73af6ef2019-04-01 23:55:57 +00002337 if (IsT1PushPop && CurReg > ARM::R7)
2338 continue;
Tim Northover93bcc662013-11-08 17:18:07 +00002339 if (!IsPop) {
Momchil Velikovac7c5c12018-01-08 14:47:19 +00002340 // Pushing any register is completely harmless, mark the register involved
2341 // as undef since we don't care about its value and must not restore it
2342 // during stack unwinding.
Tim Northover93bcc662013-11-08 17:18:07 +00002343 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2344 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002345 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002346 continue;
2347 }
2348
Tim Northover45479dc2013-12-01 14:16:24 +00002349 // However, we can only pop an extra register if it's not live. For
2350 // registers live within the function we might clobber a return value
2351 // register; the other way a register can be live here is if it's
2352 // callee-saved.
2353 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002354 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2355 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002356 // VFP pops don't allow holes in the register list, so any skip is fatal
2357 // for our transformation. GPR pops do, so we should just keep looking.
2358 if (IsVFPPushPop)
2359 return false;
2360 else
2361 continue;
2362 }
Tim Northover93bcc662013-11-08 17:18:07 +00002363
2364 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002365 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2366 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002367 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002368 }
2369
2370 if (RegsNeeded > 0)
2371 return false;
2372
2373 // Finally we know we can profitably perform the optimisation so go
2374 // ahead: strip all existing registers off and add them back again
2375 // in the right order.
2376 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2377 MI->RemoveOperand(i);
2378
2379 // Add the complete list back in.
2380 MachineInstrBuilder MIB(MF, &*MI);
2381 for (int i = RegList.size() - 1; i >= 0; --i)
Diana Picus116bbab2017-01-13 09:58:52 +00002382 MIB.add(RegList[i]);
Tim Northover93bcc662013-11-08 17:18:07 +00002383
2384 return true;
2385}
2386
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002387bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2388 unsigned FrameReg, int &Offset,
2389 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002390 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002391 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002392 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2393 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002394
Evan Cheng780748d2009-07-28 05:48:47 +00002395 // Memory operands in inline assembly always use AddrMode2.
Nick Desaulniers9f7bd712019-05-24 18:58:21 +00002396 if (Opcode == ARM::INLINEASM || Opcode == ARM::INLINEASM_BR)
Evan Cheng780748d2009-07-28 05:48:47 +00002397 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002398
Evan Cheng780748d2009-07-28 05:48:47 +00002399 if (Opcode == ARM::ADDri) {
2400 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2401 if (Offset == 0) {
2402 // Turn it into a move.
2403 MI.setDesc(TII.get(ARM::MOVr));
2404 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2405 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002406 Offset = 0;
2407 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002408 } else if (Offset < 0) {
2409 Offset = -Offset;
2410 isSub = true;
2411 MI.setDesc(TII.get(ARM::SUBri));
2412 }
2413
2414 // Common case: small offset, fits into instruction.
2415 if (ARM_AM::getSOImmVal(Offset) != -1) {
2416 // Replace the FrameIndex with sp / fp
2417 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2418 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002419 Offset = 0;
2420 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002421 }
2422
2423 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2424 // as possible.
2425 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2426 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2427
2428 // We will handle these bits from offset, clear them.
2429 Offset &= ~ThisImmVal;
2430
2431 // Get the properly encoded SOImmVal field.
2432 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2433 "Bit extraction didn't work?");
2434 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2435 } else {
2436 unsigned ImmIdx = 0;
2437 int InstrOffs = 0;
2438 unsigned NumBits = 0;
2439 unsigned Scale = 1;
2440 switch (AddrMode) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002441 case ARMII::AddrMode_i12:
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002442 ImmIdx = FrameRegIdx + 1;
2443 InstrOffs = MI.getOperand(ImmIdx).getImm();
2444 NumBits = 12;
2445 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002446 case ARMII::AddrMode2:
Evan Cheng780748d2009-07-28 05:48:47 +00002447 ImmIdx = FrameRegIdx+2;
2448 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2449 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2450 InstrOffs *= -1;
2451 NumBits = 12;
2452 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002453 case ARMII::AddrMode3:
Evan Cheng780748d2009-07-28 05:48:47 +00002454 ImmIdx = FrameRegIdx+2;
2455 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2456 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2457 InstrOffs *= -1;
2458 NumBits = 8;
2459 break;
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002460 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002461 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002462 // Can't fold any offset even if it's zero.
2463 return false;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002464 case ARMII::AddrMode5:
Evan Cheng780748d2009-07-28 05:48:47 +00002465 ImmIdx = FrameRegIdx+1;
2466 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2467 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2468 InstrOffs *= -1;
2469 NumBits = 8;
2470 Scale = 4;
2471 break;
Sjoerd Meijer011de9c2018-01-26 09:26:40 +00002472 case ARMII::AddrMode5FP16:
2473 ImmIdx = FrameRegIdx+1;
2474 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2475 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2476 InstrOffs *= -1;
2477 NumBits = 8;
2478 Scale = 2;
2479 break;
Evan Cheng780748d2009-07-28 05:48:47 +00002480 default:
2481 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002482 }
2483
2484 Offset += InstrOffs * Scale;
2485 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2486 if (Offset < 0) {
2487 Offset = -Offset;
2488 isSub = true;
2489 }
2490
2491 // Attempt to fold address comp. if opcode has offset bits
2492 if (NumBits > 0) {
2493 // Common case: small offset, fits into instruction.
2494 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2495 int ImmedOffset = Offset / Scale;
2496 unsigned Mask = (1 << NumBits) - 1;
2497 if ((unsigned)Offset <= Mask * Scale) {
2498 // Replace the FrameIndex with sp
2499 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002500 // FIXME: When addrmode2 goes away, this will simplify (like the
2501 // T2 version), as the LDR.i12 versions don't need the encoding
2502 // tricks for the offset value.
2503 if (isSub) {
2504 if (AddrMode == ARMII::AddrMode_i12)
2505 ImmedOffset = -ImmedOffset;
2506 else
2507 ImmedOffset |= 1 << NumBits;
2508 }
Evan Cheng780748d2009-07-28 05:48:47 +00002509 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002510 Offset = 0;
2511 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002512 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002513
Evan Cheng780748d2009-07-28 05:48:47 +00002514 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2515 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002516 if (isSub) {
2517 if (AddrMode == ARMII::AddrMode_i12)
2518 ImmedOffset = -ImmedOffset;
2519 else
2520 ImmedOffset |= 1 << NumBits;
2521 }
Evan Cheng780748d2009-07-28 05:48:47 +00002522 ImmOp.ChangeToImmediate(ImmedOffset);
2523 Offset &= ~(Mask*Scale);
2524 }
2525 }
2526
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002527 Offset = (isSub) ? -Offset : Offset;
2528 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002529}
Bill Wendling7de9d522010-08-06 01:32:48 +00002530
Manman Ren6fa76dc2012-06-29 21:33:59 +00002531/// analyzeCompare - For a comparison instruction, return the source registers
2532/// in SrcReg and SrcReg2 if having two register operands, and the value it
2533/// compares against in CmpValue. Return true if the comparison instruction
2534/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002535bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2536 unsigned &SrcReg2, int &CmpMask,
2537 int &CmpValue) const {
2538 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002539 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002540 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002541 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002542 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002543 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002544 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002545 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002546 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002547 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002548 case ARM::CMPrr:
2549 case ARM::t2CMPrr:
David Greenacb628b22019-02-22 12:23:31 +00002550 case ARM::tCMPr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002551 SrcReg = MI.getOperand(0).getReg();
2552 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002553 CmpMask = ~0;
2554 CmpValue = 0;
2555 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002556 case ARM::TSTri:
2557 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002558 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002559 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002560 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002561 CmpValue = 0;
2562 return true;
2563 }
2564
2565 return false;
2566}
2567
Gabor Greifd36e3e82010-09-29 10:12:08 +00002568/// isSuitableForMask - Identify a suitable 'and' instruction that
2569/// operates on the given source register and applies the same mask
2570/// as a 'tst' instruction. Provide a limited look-through for copies.
2571/// When successful, MI will hold the found instruction.
2572static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002573 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002574 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002575 case ARM::ANDri:
2576 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002577 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002578 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002579 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002580 return true;
2581 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002582 }
2583
2584 return false;
2585}
2586
Manman Renb1b3db62012-06-29 22:06:19 +00002587/// getSwappedCondition - assume the flags are set by MI(a,b), return
2588/// the condition code if we modify the instructions such that flags are
2589/// set by MI(b,a).
2590inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2591 switch (CC) {
2592 default: return ARMCC::AL;
2593 case ARMCC::EQ: return ARMCC::EQ;
2594 case ARMCC::NE: return ARMCC::NE;
2595 case ARMCC::HS: return ARMCC::LS;
2596 case ARMCC::LO: return ARMCC::HI;
2597 case ARMCC::HI: return ARMCC::LO;
2598 case ARMCC::LS: return ARMCC::HS;
2599 case ARMCC::GE: return ARMCC::LE;
2600 case ARMCC::LT: return ARMCC::GT;
2601 case ARMCC::GT: return ARMCC::LT;
2602 case ARMCC::LE: return ARMCC::GE;
2603 }
2604}
2605
Joel Galensonfe7fa402018-01-17 19:19:05 +00002606/// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2607/// the condition code if we modify the instructions such that flags are
2608/// set by ADD(a,b,X).
2609inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
2610 switch (CC) {
2611 default: return ARMCC::AL;
2612 case ARMCC::HS: return ARMCC::LO;
2613 case ARMCC::LO: return ARMCC::HS;
2614 case ARMCC::VS: return ARMCC::VS;
2615 case ARMCC::VC: return ARMCC::VC;
2616 }
2617}
2618
Manman Renb1b3db62012-06-29 22:06:19 +00002619/// isRedundantFlagInstr - check whether the first instruction, whose only
2620/// purpose is to update flags, can be made redundant.
2621/// CMPrr can be made redundant by SUBrr if the operands are the same.
2622/// CMPri can be made redundant by SUBri if the operands are the same.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002623/// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
Manman Renb1b3db62012-06-29 22:06:19 +00002624/// This function can be extended later on.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002625inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
2626 unsigned SrcReg, unsigned SrcReg2,
David Greenacb628b22019-02-22 12:23:31 +00002627 int ImmValue, const MachineInstr *OI,
2628 bool &IsThumb1) {
David Green7e6da812019-02-07 10:51:04 +00002629 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2630 (OI->getOpcode() == ARM::SUBrr || OI->getOpcode() == ARM::t2SUBrr) &&
Manman Renb1b3db62012-06-29 22:06:19 +00002631 ((OI->getOperand(1).getReg() == SrcReg &&
2632 OI->getOperand(2).getReg() == SrcReg2) ||
2633 (OI->getOperand(1).getReg() == SrcReg2 &&
David Greenacb628b22019-02-22 12:23:31 +00002634 OI->getOperand(2).getReg() == SrcReg))) {
2635 IsThumb1 = false;
Manman Renb1b3db62012-06-29 22:06:19 +00002636 return true;
David Greenacb628b22019-02-22 12:23:31 +00002637 }
2638
2639 if (CmpI->getOpcode() == ARM::tCMPr && OI->getOpcode() == ARM::tSUBrr &&
2640 ((OI->getOperand(2).getReg() == SrcReg &&
2641 OI->getOperand(3).getReg() == SrcReg2) ||
2642 (OI->getOperand(2).getReg() == SrcReg2 &&
2643 OI->getOperand(3).getReg() == SrcReg))) {
2644 IsThumb1 = true;
2645 return true;
2646 }
Manman Renb1b3db62012-06-29 22:06:19 +00002647
David Green7e6da812019-02-07 10:51:04 +00002648 if ((CmpI->getOpcode() == ARM::CMPri || CmpI->getOpcode() == ARM::t2CMPri) &&
2649 (OI->getOpcode() == ARM::SUBri || OI->getOpcode() == ARM::t2SUBri) &&
Manman Renb1b3db62012-06-29 22:06:19 +00002650 OI->getOperand(1).getReg() == SrcReg &&
David Greenacb628b22019-02-22 12:23:31 +00002651 OI->getOperand(2).getImm() == ImmValue) {
2652 IsThumb1 = false;
Manman Renb1b3db62012-06-29 22:06:19 +00002653 return true;
David Greenacb628b22019-02-22 12:23:31 +00002654 }
2655
2656 if (CmpI->getOpcode() == ARM::tCMPi8 &&
2657 (OI->getOpcode() == ARM::tSUBi8 || OI->getOpcode() == ARM::tSUBi3) &&
2658 OI->getOperand(2).getReg() == SrcReg &&
2659 OI->getOperand(3).getImm() == ImmValue) {
2660 IsThumb1 = true;
2661 return true;
2662 }
Joel Galensonfe7fa402018-01-17 19:19:05 +00002663
2664 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2665 (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
2666 OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
David Green7a183a82019-02-21 11:03:13 +00002667 OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
Joel Galensonfe7fa402018-01-17 19:19:05 +00002668 OI->getOperand(0).getReg() == SrcReg &&
David Greenacb628b22019-02-22 12:23:31 +00002669 OI->getOperand(1).getReg() == SrcReg2) {
2670 IsThumb1 = false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002671 return true;
David Greenacb628b22019-02-22 12:23:31 +00002672 }
2673
2674 if (CmpI->getOpcode() == ARM::tCMPr &&
2675 (OI->getOpcode() == ARM::tADDi3 || OI->getOpcode() == ARM::tADDi8 ||
2676 OI->getOpcode() == ARM::tADDrr) &&
2677 OI->getOperand(0).getReg() == SrcReg &&
2678 OI->getOperand(2).getReg() == SrcReg2) {
2679 IsThumb1 = true;
2680 return true;
2681 }
2682
Manman Renb1b3db62012-06-29 22:06:19 +00002683 return false;
2684}
2685
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002686static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2687 switch (MI->getOpcode()) {
2688 default: return false;
2689 case ARM::tLSLri:
2690 case ARM::tLSRri:
2691 case ARM::tLSLrr:
2692 case ARM::tLSRrr:
2693 case ARM::tSUBrr:
2694 case ARM::tADDrr:
2695 case ARM::tADDi3:
2696 case ARM::tADDi8:
2697 case ARM::tSUBi3:
2698 case ARM::tSUBi8:
2699 case ARM::tMUL:
David Greenb504f102019-02-25 15:50:54 +00002700 case ARM::tADC:
2701 case ARM::tSBC:
2702 case ARM::tRSB:
2703 case ARM::tAND:
2704 case ARM::tORR:
2705 case ARM::tEOR:
2706 case ARM::tBIC:
2707 case ARM::tMVN:
2708 case ARM::tASRri:
2709 case ARM::tASRrr:
2710 case ARM::tROR:
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002711 IsThumb1 = true;
2712 LLVM_FALLTHROUGH;
2713 case ARM::RSBrr:
2714 case ARM::RSBri:
2715 case ARM::RSCrr:
2716 case ARM::RSCri:
2717 case ARM::ADDrr:
2718 case ARM::ADDri:
2719 case ARM::ADCrr:
2720 case ARM::ADCri:
2721 case ARM::SUBrr:
2722 case ARM::SUBri:
2723 case ARM::SBCrr:
2724 case ARM::SBCri:
2725 case ARM::t2RSBri:
2726 case ARM::t2ADDrr:
2727 case ARM::t2ADDri:
2728 case ARM::t2ADCrr:
2729 case ARM::t2ADCri:
2730 case ARM::t2SUBrr:
2731 case ARM::t2SUBri:
2732 case ARM::t2SBCrr:
2733 case ARM::t2SBCri:
2734 case ARM::ANDrr:
2735 case ARM::ANDri:
2736 case ARM::t2ANDrr:
2737 case ARM::t2ANDri:
2738 case ARM::ORRrr:
2739 case ARM::ORRri:
2740 case ARM::t2ORRrr:
2741 case ARM::t2ORRri:
2742 case ARM::EORrr:
2743 case ARM::EORri:
2744 case ARM::t2EORrr:
2745 case ARM::t2EORri:
2746 case ARM::t2LSRri:
2747 case ARM::t2LSRrr:
2748 case ARM::t2LSLri:
2749 case ARM::t2LSLrr:
2750 return true;
2751 }
2752}
2753
Manman Ren6fa76dc2012-06-29 21:33:59 +00002754/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2755/// comparison into one that sets the zero bit in the flags register;
2756/// Remove a redundant Compare instruction if an earlier instruction can set the
2757/// flags in the same way as Compare.
2758/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2759/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2760/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002761bool ARMBaseInstrInfo::optimizeCompareInstr(
2762 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2763 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002764 // Get the unique definition of SrcReg.
2765 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2766 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002767
Gabor Greifadbbb932010-09-21 12:01:15 +00002768 // Masked compares sometimes use the same register as the corresponding 'and'.
2769 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002770 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002771 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002772 for (MachineRegisterInfo::use_instr_iterator
2773 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2774 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002775 if (UI->getParent() != CmpInstr.getParent())
2776 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002777 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002778 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002779 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002780 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002781 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002782 break;
2783 }
2784 if (!MI) return false;
2785 }
2786 }
2787
Manman Rendc8ad002012-05-11 01:30:47 +00002788 // Get ready to iterate backward from CmpInstr.
2789 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002790 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002791
2792 // Early exit if CmpInstr is at the beginning of the BB.
2793 if (I == B) return false;
2794
Manman Rendc8ad002012-05-11 01:30:47 +00002795 // There are two possible candidates which can be changed to set CPSR:
Joel Galensonfe7fa402018-01-17 19:19:05 +00002796 // One is MI, the other is a SUB or ADD instruction.
2797 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
2798 // ADDr[ri](r1, r2, X).
Manman Rendc8ad002012-05-11 01:30:47 +00002799 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Joel Galensonfe7fa402018-01-17 19:19:05 +00002800 MachineInstr *SubAdd = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002801 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002802 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002803 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002804 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002805 // Conservatively refuse to convert an instruction which isn't in the same
2806 // BB as the comparison.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002807 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002808 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002809 if (CmpInstr.getOpcode() == ARM::CMPri ||
David Greenacb628b22019-02-22 12:23:31 +00002810 CmpInstr.getOpcode() == ARM::t2CMPri ||
2811 CmpInstr.getOpcode() == ARM::tCMPi8)
Craig Topper062a2ba2014-04-25 05:30:21 +00002812 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002813 else
2814 return false;
2815 }
2816
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002817 bool IsThumb1 = false;
2818 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2819 return false;
2820
2821 // We also want to do this peephole for cases like this: if (a*b == 0),
2822 // and optimise away the CMP instruction from the generated code sequence:
2823 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2824 // resulting from the select instruction, but these MOVS instructions for
2825 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2826 // However, if we only have MOVS instructions in between the CMP and the
2827 // other instruction (the MULS in this example), then the CPSR is dead so we
2828 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2829 // reordering and then continue the analysis hoping we can eliminate the
2830 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2831 // consequence, the movs won't redefine/kill the MUL operands which would
2832 // make this reordering illegal.
David Greenb504f102019-02-25 15:50:54 +00002833 const TargetRegisterInfo *TRI = &getRegisterInfo();
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002834 if (MI && IsThumb1) {
2835 --I;
David Greenb504f102019-02-25 15:50:54 +00002836 if (I != E && !MI->readsRegister(ARM::CPSR, TRI)) {
2837 bool CanReorder = true;
2838 for (; I != E; --I) {
2839 if (I->getOpcode() != ARM::tMOVi8) {
2840 CanReorder = false;
2841 break;
2842 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002843 }
David Greenb504f102019-02-25 15:50:54 +00002844 if (CanReorder) {
2845 MI = MI->removeFromParent();
2846 E = CmpInstr;
2847 CmpInstr.getParent()->insert(E, MI);
2848 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002849 }
2850 I = CmpInstr;
2851 E = MI;
2852 }
2853
Manman Rendc8ad002012-05-11 01:30:47 +00002854 // Check that CPSR isn't set between the comparison instruction and the one we
Joel Galensonfe7fa402018-01-17 19:19:05 +00002855 // want to change. At the same time, search for SubAdd.
David Greenacb628b22019-02-22 12:23:31 +00002856 bool SubAddIsThumb1 = false;
Joel Galenson1d89cd22018-01-22 17:53:47 +00002857 do {
2858 const MachineInstr &Instr = *--I;
Bill Wendling7de9d522010-08-06 01:32:48 +00002859
Joel Galensonfe7fa402018-01-17 19:19:05 +00002860 // Check whether CmpInstr can be made redundant by the current instruction.
David Greenacb628b22019-02-22 12:23:31 +00002861 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr,
2862 SubAddIsThumb1)) {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002863 SubAdd = &*I;
2864 break;
2865 }
2866
Joel Galenson1d89cd22018-01-22 17:53:47 +00002867 // Allow E (which was initially MI) to be SubAdd but do not search before E.
2868 if (I == E)
2869 break;
2870
Manman Renb1b3db62012-06-29 22:06:19 +00002871 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2872 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002873 // This instruction modifies or uses CPSR after the one we want to
2874 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002875 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002876
Eli Friedmanb906bba2019-03-22 20:49:15 +00002877 if (I == B) {
2878 // In some cases, we scan the use-list of an instruction for an AND;
2879 // that AND is in the same BB, but may not be scheduled before the
2880 // corresponding TST. In that case, bail out.
2881 //
2882 // FIXME: We could try to reschedule the AND.
2883 return false;
2884 }
2885 } while (true);
Bill Wendling7de9d522010-08-06 01:32:48 +00002886
Manman Rendc8ad002012-05-11 01:30:47 +00002887 // Return false if no candidates exist.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002888 if (!MI && !SubAdd)
Manman Rendc8ad002012-05-11 01:30:47 +00002889 return false;
2890
David Green743abf22019-02-14 11:09:24 +00002891 // If we found a SubAdd, use it as it will be closer to the CMP
2892 if (SubAdd) {
2893 MI = SubAdd;
David Greenacb628b22019-02-22 12:23:31 +00002894 IsThumb1 = SubAddIsThumb1;
David Green743abf22019-02-14 11:09:24 +00002895 }
Manman Rendc8ad002012-05-11 01:30:47 +00002896
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002897 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002898 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002899 return false;
2900
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002901 // Scan forward for the use of CPSR
2902 // When checking against MI: if it's a conditional code that requires
2903 // checking of the V bit or C bit, then this is not safe to do.
2904 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2905 // If we are done with the basic block, we need to check whether CPSR is
2906 // live-out.
2907 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2908 OperandsToUpdate;
2909 bool isSafe = false;
2910 I = CmpInstr;
2911 E = CmpInstr.getParent()->end();
2912 while (!isSafe && ++I != E) {
2913 const MachineInstr &Instr = *I;
2914 for (unsigned IO = 0, EO = Instr.getNumOperands();
2915 !isSafe && IO != EO; ++IO) {
2916 const MachineOperand &MO = Instr.getOperand(IO);
2917 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2918 isSafe = true;
2919 break;
2920 }
2921 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2922 continue;
2923 if (MO.isDef()) {
2924 isSafe = true;
2925 break;
2926 }
2927 // Condition code is after the operand before CPSR except for VSELs.
2928 ARMCC::CondCodes CC;
2929 bool IsInstrVSel = true;
2930 switch (Instr.getOpcode()) {
2931 default:
2932 IsInstrVSel = false;
2933 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2934 break;
2935 case ARM::VSELEQD:
2936 case ARM::VSELEQS:
2937 CC = ARMCC::EQ;
2938 break;
2939 case ARM::VSELGTD:
2940 case ARM::VSELGTS:
2941 CC = ARMCC::GT;
2942 break;
2943 case ARM::VSELGED:
2944 case ARM::VSELGES:
2945 CC = ARMCC::GE;
2946 break;
2947 case ARM::VSELVSS:
2948 case ARM::VSELVSD:
2949 CC = ARMCC::VS;
2950 break;
2951 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002952
Joel Galensonfe7fa402018-01-17 19:19:05 +00002953 if (SubAdd) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002954 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2955 // on CMP needs to be updated to be based on SUB.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002956 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
2957 // needs to be modified.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002958 // Push the condition code operands to OperandsToUpdate.
2959 // If it is safe to remove CmpInstr, the condition code of these
2960 // operands will be modified.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002961 unsigned Opc = SubAdd->getOpcode();
2962 bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
David Greenacb628b22019-02-22 12:23:31 +00002963 Opc == ARM::SUBri || Opc == ARM::t2SUBri ||
2964 Opc == ARM::tSUBrr || Opc == ARM::tSUBi3 ||
2965 Opc == ARM::tSUBi8;
2966 unsigned OpI = Opc != ARM::tSUBrr ? 1 : 2;
2967 if (!IsSub ||
2968 (SrcReg2 != 0 && SubAdd->getOperand(OpI).getReg() == SrcReg2 &&
2969 SubAdd->getOperand(OpI + 1).getReg() == SrcReg)) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002970 // VSel doesn't support condition code update.
2971 if (IsInstrVSel)
Manman Rendc8ad002012-05-11 01:30:47 +00002972 return false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002973 // Ensure we can swap the condition.
2974 ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
2975 if (NewCC == ARMCC::AL)
2976 return false;
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002977 OperandsToUpdate.push_back(
2978 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2979 }
2980 } else {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002981 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002982 switch (CC) {
2983 case ARMCC::EQ: // Z
2984 case ARMCC::NE: // Z
2985 case ARMCC::MI: // N
2986 case ARMCC::PL: // N
2987 case ARMCC::AL: // none
2988 // CPSR can be used multiple times, we should continue.
2989 break;
2990 case ARMCC::HS: // C
2991 case ARMCC::LO: // C
2992 case ARMCC::VS: // V
2993 case ARMCC::VC: // V
2994 case ARMCC::HI: // C Z
2995 case ARMCC::LS: // C Z
2996 case ARMCC::GE: // N V
2997 case ARMCC::LT: // N V
2998 case ARMCC::GT: // Z N V
2999 case ARMCC::LE: // Z N V
3000 // The instruction uses the V bit or C bit which is not safe.
3001 return false;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00003002 }
Evan Cheng425489d2011-03-23 22:52:04 +00003003 }
3004 }
Bill Wendling7de9d522010-08-06 01:32:48 +00003005 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003006
3007 // If CPSR is not killed nor re-defined, we should check whether it is
3008 // live-out. If it is live-out, do not optimize.
3009 if (!isSafe) {
3010 MachineBasicBlock *MBB = CmpInstr.getParent();
3011 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
3012 SE = MBB->succ_end(); SI != SE; ++SI)
3013 if ((*SI)->isLiveIn(ARM::CPSR))
3014 return false;
Cameron Zwarich0829b302011-04-15 20:45:00 +00003015 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003016
3017 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
3018 // set CPSR so this is represented as an explicit output)
3019 if (!IsThumb1) {
3020 MI->getOperand(5).setReg(ARM::CPSR);
3021 MI->getOperand(5).setIsDef(true);
3022 }
3023 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
3024 CmpInstr.eraseFromParent();
3025
3026 // Modify the condition code of operands in OperandsToUpdate.
3027 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
3028 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
3029 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
3030 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
3031
Eli Friedman2ac11622018-10-26 19:32:24 +00003032 MI->clearRegisterDeads(ARM::CPSR);
3033
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003034 return true;
Bill Wendling7de9d522010-08-06 01:32:48 +00003035}
Evan Cheng367a5df2010-09-09 18:18:55 +00003036
Joel Galensonfe7fa402018-01-17 19:19:05 +00003037bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
3038 // Do not sink MI if it might be used to optimize a redundant compare.
3039 // We heuristically only look at the instruction immediately following MI to
3040 // avoid potentially searching the entire basic block.
3041 if (isPredicated(MI))
3042 return true;
3043 MachineBasicBlock::const_iterator Next = &MI;
3044 ++Next;
3045 unsigned SrcReg, SrcReg2;
3046 int CmpMask, CmpValue;
David Greenacb628b22019-02-22 12:23:31 +00003047 bool IsThumb1;
Joel Galensonfe7fa402018-01-17 19:19:05 +00003048 if (Next != MI.getParent()->end() &&
3049 analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
David Greenacb628b22019-02-22 12:23:31 +00003050 isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI, IsThumb1))
Joel Galensonfe7fa402018-01-17 19:19:05 +00003051 return false;
3052 return true;
3053}
3054
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003055bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
3056 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003057 MachineRegisterInfo *MRI) const {
3058 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003059 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003060 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
3061 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003062 if (!DefMI.getOperand(1).isImm())
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00003063 // Could be t2MOVi32imm @xx
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003064 return false;
3065
3066 if (!MRI->hasOneNonDBGUse(Reg))
3067 return false;
3068
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003069 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00003070 if (DefMCID.hasOptionalDef()) {
3071 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003072 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00003073 if (MO.getReg() == ARM::CPSR && !MO.isDead())
3074 // If DefMI defines CPSR and it is not dead, it's obviously not safe
3075 // to delete DefMI.
3076 return false;
3077 }
3078
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003079 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00003080 if (UseMCID.hasOptionalDef()) {
3081 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003082 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00003083 // If the instruction sets the flag, do not attempt this optimization
3084 // since it may change the semantics of the code.
3085 return false;
3086 }
3087
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003088 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003089 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003090 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003091 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003092 bool Commute = false;
3093 switch (UseOpc) {
3094 default: return false;
3095 case ARM::SUBrr:
3096 case ARM::ADDrr:
3097 case ARM::ORRrr:
3098 case ARM::EORrr:
3099 case ARM::t2SUBrr:
3100 case ARM::t2ADDrr:
3101 case ARM::t2ORRrr:
3102 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003103 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003104 switch (UseOpc) {
3105 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003106 case ARM::ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003107 case ARM::SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003108 if (UseOpc == ARM::SUBrr && Commute)
3109 return false;
3110
3111 // ADD/SUB are special because they're essentially the same operation, so
3112 // we can handle a larger range of immediates.
3113 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
3114 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
3115 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
3116 ImmVal = -ImmVal;
3117 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
3118 } else
3119 return false;
3120 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3121 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3122 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003123 case ARM::ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003124 case ARM::EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003125 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
3126 return false;
3127 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3128 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3129 switch (UseOpc) {
3130 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003131 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
3132 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
3133 }
3134 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003135 case ARM::t2ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003136 case ARM::t2SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003137 if (UseOpc == ARM::t2SUBrr && Commute)
3138 return false;
3139
3140 // ADD/SUB are special because they're essentially the same operation, so
3141 // we can handle a larger range of immediates.
3142 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3143 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
3144 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
3145 ImmVal = -ImmVal;
3146 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
3147 } else
3148 return false;
3149 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3150 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3151 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003152 case ARM::t2ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003153 case ARM::t2EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003154 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3155 return false;
3156 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3157 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3158 switch (UseOpc) {
3159 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003160 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
3161 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
3162 }
3163 break;
3164 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003165 }
3166 }
3167
3168 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003169 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
3170 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003171 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Diana Picus8a73f552017-01-13 10:18:01 +00003172 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
3173 NewReg)
3174 .addReg(Reg1, getKillRegState(isKill))
3175 .addImm(SOImmValV1)
3176 .add(predOps(ARMCC::AL))
3177 .add(condCodeOp());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003178 UseMI.setDesc(get(NewUseOpc));
3179 UseMI.getOperand(1).setReg(NewReg);
3180 UseMI.getOperand(1).setIsKill();
3181 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
3182 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003183 return true;
3184}
3185
Bob Wilsone8a549c2012-09-29 21:43:49 +00003186static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003187 const MachineInstr &MI) {
3188 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003189 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003190 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003191 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
3192 assert(UOps >= 0 && "bad # UOps");
3193 return UOps;
3194 }
3195
3196 case ARM::LDRrs:
3197 case ARM::LDRBrs:
3198 case ARM::STRrs:
3199 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003200 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003201 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3202 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3203 if (!isSub &&
3204 (ShImm == 0 ||
3205 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3206 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3207 return 1;
3208 return 2;
3209 }
3210
3211 case ARM::LDRH:
3212 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003213 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00003214 return 1;
3215
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003216 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003217 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3218 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3219 if (!isSub &&
3220 (ShImm == 0 ||
3221 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3222 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3223 return 1;
3224 return 2;
3225 }
3226
3227 case ARM::LDRSB:
3228 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003229 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003230
3231 case ARM::LDRSB_POST:
3232 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003233 unsigned Rt = MI.getOperand(0).getReg();
3234 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003235 return (Rt == Rm) ? 4 : 3;
3236 }
3237
3238 case ARM::LDR_PRE_REG:
3239 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003240 unsigned Rt = MI.getOperand(0).getReg();
3241 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003242 if (Rt == Rm)
3243 return 3;
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::STR_PRE_REG:
3256 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003257 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003258 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3259 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3260 if (!isSub &&
3261 (ShImm == 0 ||
3262 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3263 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3264 return 2;
3265 return 3;
3266 }
3267
3268 case ARM::LDRH_PRE:
3269 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003270 unsigned Rt = MI.getOperand(0).getReg();
3271 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003272 if (!Rm)
3273 return 2;
3274 if (Rt == Rm)
3275 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003276 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003277 }
3278
3279 case ARM::LDR_POST_REG:
3280 case ARM::LDRB_POST_REG:
3281 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003282 unsigned Rt = MI.getOperand(0).getReg();
3283 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003284 return (Rt == Rm) ? 3 : 2;
3285 }
3286
3287 case ARM::LDR_PRE_IMM:
3288 case ARM::LDRB_PRE_IMM:
3289 case ARM::LDR_POST_IMM:
3290 case ARM::LDRB_POST_IMM:
3291 case ARM::STRB_POST_IMM:
3292 case ARM::STRB_POST_REG:
3293 case ARM::STRB_PRE_IMM:
3294 case ARM::STRH_POST:
3295 case ARM::STR_POST_IMM:
3296 case ARM::STR_POST_REG:
3297 case ARM::STR_PRE_IMM:
3298 return 2;
3299
3300 case ARM::LDRSB_PRE:
3301 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003302 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003303 if (Rm == 0)
3304 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003305 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003306 if (Rt == Rm)
3307 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003308 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003309 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3310 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3311 if (!isSub &&
3312 (ShImm == 0 ||
3313 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3314 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3315 return 3;
3316 return 4;
3317 }
3318
3319 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003320 unsigned Rt = MI.getOperand(0).getReg();
3321 unsigned Rn = MI.getOperand(2).getReg();
3322 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003323 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003324 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3325 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003326 return (Rt == Rn) ? 3 : 2;
3327 }
3328
3329 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003330 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003331 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003332 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3333 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003334 return 2;
3335 }
3336
3337 case ARM::LDRD_POST:
3338 case ARM::t2LDRD_POST:
3339 return 3;
3340
3341 case ARM::STRD_POST:
3342 case ARM::t2STRD_POST:
3343 return 4;
3344
3345 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003346 unsigned Rt = MI.getOperand(0).getReg();
3347 unsigned Rn = MI.getOperand(3).getReg();
3348 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003349 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003350 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3351 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003352 return (Rt == Rn) ? 4 : 3;
3353 }
3354
3355 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003356 unsigned Rt = MI.getOperand(0).getReg();
3357 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003358 return (Rt == Rn) ? 4 : 3;
3359 }
3360
3361 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003362 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003363 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003364 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3365 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003366 return 3;
3367 }
3368
3369 case ARM::t2STRD_PRE:
3370 return 3;
3371
3372 case ARM::t2LDR_POST:
3373 case ARM::t2LDRB_POST:
3374 case ARM::t2LDRB_PRE:
3375 case ARM::t2LDRSBi12:
3376 case ARM::t2LDRSBi8:
3377 case ARM::t2LDRSBpci:
3378 case ARM::t2LDRSBs:
3379 case ARM::t2LDRH_POST:
3380 case ARM::t2LDRH_PRE:
3381 case ARM::t2LDRSBT:
3382 case ARM::t2LDRSB_POST:
3383 case ARM::t2LDRSB_PRE:
3384 case ARM::t2LDRSH_POST:
3385 case ARM::t2LDRSH_PRE:
3386 case ARM::t2LDRSHi12:
3387 case ARM::t2LDRSHi8:
3388 case ARM::t2LDRSHpci:
3389 case ARM::t2LDRSHs:
3390 return 2;
3391
3392 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003393 unsigned Rt = MI.getOperand(0).getReg();
3394 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003395 return (Rt == Rn) ? 3 : 2;
3396 }
3397
3398 case ARM::t2STRB_POST:
3399 case ARM::t2STRB_PRE:
3400 case ARM::t2STRBs:
3401 case ARM::t2STRDi8:
3402 case ARM::t2STRH_POST:
3403 case ARM::t2STRH_PRE:
3404 case ARM::t2STRHs:
3405 case ARM::t2STR_POST:
3406 case ARM::t2STR_PRE:
3407 case ARM::t2STRs:
3408 return 2;
3409 }
3410}
3411
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003412// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3413// can't be easily determined return 0 (missing MachineMemOperand).
3414//
3415// FIXME: The current MachineInstr design does not support relying on machine
3416// mem operands to determine the width of a memory access. Instead, we expect
3417// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003418// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003419// two reasons:
3420//
3421// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3422// operands. This is much more dangerous than using the MachineMemOperand
3423// sizes because CodeGen passes can insert/remove optional machine operands. In
3424// fact, it's totally incorrect for preRA passes and appears to be wrong for
3425// postRA passes as well.
3426//
3427// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3428// machine model that calls this should handle the unknown (zero size) case.
3429//
3430// Long term, we should require a target hook that verifies MachineMemOperand
3431// sizes during MC lowering. That target hook should be local to MC lowering
3432// because we can't ensure that it is aware of other MI forms. Doing this will
3433// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003434unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003435 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003436 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3437 E = MI.memoperands_end();
3438 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003439 Size += (*I)->getSize();
3440 }
Eli Friedmanc388bfa2019-03-27 18:33:30 +00003441 // FIXME: The scheduler currently can't handle values larger than 16. But
3442 // the values can actually go up to 32 for floating-point load/store
3443 // multiple (VLDMIA etc.). Also, the way this code is reasoning about memory
3444 // operations isn't right; we could end up with "extra" memory operands for
3445 // various reasons, like tail merge merging two memory operations.
3446 return std::min(Size / 4, 16U);
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003447}
3448
Diana Picus92423ce2016-06-27 09:08:23 +00003449static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3450 unsigned NumRegs) {
3451 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3452 switch (Opc) {
3453 default:
3454 break;
3455 case ARM::VLDMDIA_UPD:
3456 case ARM::VLDMDDB_UPD:
3457 case ARM::VLDMSIA_UPD:
3458 case ARM::VLDMSDB_UPD:
3459 case ARM::VSTMDIA_UPD:
3460 case ARM::VSTMDDB_UPD:
3461 case ARM::VSTMSIA_UPD:
3462 case ARM::VSTMSDB_UPD:
3463 case ARM::LDMIA_UPD:
3464 case ARM::LDMDA_UPD:
3465 case ARM::LDMDB_UPD:
3466 case ARM::LDMIB_UPD:
3467 case ARM::STMIA_UPD:
3468 case ARM::STMDA_UPD:
3469 case ARM::STMDB_UPD:
3470 case ARM::STMIB_UPD:
3471 case ARM::tLDMIA_UPD:
3472 case ARM::tSTMIA_UPD:
3473 case ARM::t2LDMIA_UPD:
3474 case ARM::t2LDMDB_UPD:
3475 case ARM::t2STMIA_UPD:
3476 case ARM::t2STMDB_UPD:
3477 ++UOps; // One for base register writeback.
3478 break;
3479 case ARM::LDMIA_RET:
3480 case ARM::tPOP_RET:
3481 case ARM::t2LDMIA_RET:
3482 UOps += 2; // One for base reg wb, one for write to pc.
3483 break;
3484 }
3485 return UOps;
3486}
3487
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003488unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3489 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003490 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003491 return 1;
3492
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003493 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003494 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003495 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003496 if (ItinUOps >= 0) {
3497 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3498 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3499
Andrew Trickf161e392012-07-02 18:10:42 +00003500 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003501 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003502
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003503 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003504 switch (Opc) {
3505 default:
3506 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003507 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003508 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003509 return 2;
3510
3511 // The number of uOps for load / store multiple are determined by the number
3512 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003513 //
Evan Chengbf407072010-09-10 01:29:16 +00003514 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3515 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003516 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003517 //
Evan Chengbf407072010-09-10 01:29:16 +00003518 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003519 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3520 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3521 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003522 case ARM::VLDMDIA_UPD:
3523 case ARM::VLDMDDB_UPD:
3524 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003525 case ARM::VLDMSIA_UPD:
3526 case ARM::VLDMSDB_UPD:
3527 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003528 case ARM::VSTMDIA_UPD:
3529 case ARM::VSTMDDB_UPD:
3530 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003531 case ARM::VSTMSIA_UPD:
3532 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003533 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003534 return (NumRegs / 2) + (NumRegs % 2) + 1;
3535 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003536
3537 case ARM::LDMIA_RET:
3538 case ARM::LDMIA:
3539 case ARM::LDMDA:
3540 case ARM::LDMDB:
3541 case ARM::LDMIB:
3542 case ARM::LDMIA_UPD:
3543 case ARM::LDMDA_UPD:
3544 case ARM::LDMDB_UPD:
3545 case ARM::LDMIB_UPD:
3546 case ARM::STMIA:
3547 case ARM::STMDA:
3548 case ARM::STMDB:
3549 case ARM::STMIB:
3550 case ARM::STMIA_UPD:
3551 case ARM::STMDA_UPD:
3552 case ARM::STMDB_UPD:
3553 case ARM::STMIB_UPD:
3554 case ARM::tLDMIA:
3555 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003556 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003557 case ARM::tPOP_RET:
3558 case ARM::tPOP:
3559 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003560 case ARM::t2LDMIA_RET:
3561 case ARM::t2LDMIA:
3562 case ARM::t2LDMDB:
3563 case ARM::t2LDMIA_UPD:
3564 case ARM::t2LDMDB_UPD:
3565 case ARM::t2STMIA:
3566 case ARM::t2STMDB:
3567 case ARM::t2STMIA_UPD:
3568 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003569 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003570 switch (Subtarget.getLdStMultipleTiming()) {
3571 case ARMSubtarget::SingleIssuePlusExtras:
3572 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3573 case ARMSubtarget::SingleIssue:
3574 // Assume the worst.
3575 return NumRegs;
3576 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003577 if (NumRegs < 4)
3578 return 2;
3579 // 4 registers would be issued: 2, 2.
3580 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003581 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003582 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003583 ++UOps;
3584 return UOps;
3585 }
3586 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3587 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003588 // If there are odd number of registers or if it's not 64-bit aligned,
3589 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003590 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3591 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003592 ++UOps;
3593 return UOps;
3594 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003595 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003596 }
3597 }
Diana Picus92423ce2016-06-27 09:08:23 +00003598 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003599}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003600
3601int
Evan Cheng412e37b2010-10-07 23:12:15 +00003602ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003603 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003604 unsigned DefClass,
3605 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003606 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003607 if (RegNo <= 0)
3608 // Def is the address writeback.
3609 return ItinData->getOperandCycle(DefClass, DefIdx);
3610
3611 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003612 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003613 // (regno / 2) + (regno % 2) + 1
3614 DefCycle = RegNo / 2 + 1;
3615 if (RegNo % 2)
3616 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003617 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003618 DefCycle = RegNo;
3619 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003620
Evan Cheng6cc775f2011-06-28 19:10:37 +00003621 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003622 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003623 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003624 case ARM::VLDMSIA_UPD:
3625 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003626 isSLoad = true;
3627 break;
3628 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003629
Evan Cheng412e37b2010-10-07 23:12:15 +00003630 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3631 // then it takes an extra cycle.
3632 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3633 ++DefCycle;
3634 } else {
3635 // Assume the worst.
3636 DefCycle = RegNo + 2;
3637 }
3638
3639 return DefCycle;
3640}
3641
Javed Absar4ae7e8122017-06-02 08:53:19 +00003642bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3643 unsigned BaseReg = MI.getOperand(0).getReg();
3644 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3645 const auto &Op = MI.getOperand(i);
3646 if (Op.isReg() && Op.getReg() == BaseReg)
3647 return true;
3648 }
3649 return false;
3650}
3651unsigned
3652ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00003653 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops
3654 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops)
Javed Absar4ae7e8122017-06-02 08:53:19 +00003655 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3656}
3657
Evan Cheng412e37b2010-10-07 23:12:15 +00003658int
3659ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003660 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003661 unsigned DefClass,
3662 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003663 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003664 if (RegNo <= 0)
3665 // Def is the address writeback.
3666 return ItinData->getOperandCycle(DefClass, DefIdx);
3667
3668 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003669 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003670 // 4 registers would be issued: 1, 2, 1.
3671 // 5 registers would be issued: 1, 2, 2.
3672 DefCycle = RegNo / 2;
3673 if (DefCycle < 1)
3674 DefCycle = 1;
3675 // Result latency is issue cycle + 2: E2.
3676 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003677 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003678 DefCycle = (RegNo / 2);
3679 // If there are odd number of registers or if it's not 64-bit aligned,
3680 // then it takes an extra AGU (Address Generation Unit) cycle.
3681 if ((RegNo % 2) || DefAlign < 8)
3682 ++DefCycle;
3683 // Result latency is AGU cycles + 2.
3684 DefCycle += 2;
3685 } else {
3686 // Assume the worst.
3687 DefCycle = RegNo + 2;
3688 }
3689
3690 return DefCycle;
3691}
3692
3693int
3694ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003695 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003696 unsigned UseClass,
3697 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003698 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003699 if (RegNo <= 0)
3700 return ItinData->getOperandCycle(UseClass, UseIdx);
3701
3702 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003703 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003704 // (regno / 2) + (regno % 2) + 1
3705 UseCycle = RegNo / 2 + 1;
3706 if (RegNo % 2)
3707 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003708 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003709 UseCycle = RegNo;
3710 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003711
Evan Cheng6cc775f2011-06-28 19:10:37 +00003712 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003713 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003714 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003715 case ARM::VSTMSIA_UPD:
3716 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003717 isSStore = true;
3718 break;
3719 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003720
Evan Cheng412e37b2010-10-07 23:12:15 +00003721 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3722 // then it takes an extra cycle.
3723 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3724 ++UseCycle;
3725 } else {
3726 // Assume the worst.
3727 UseCycle = RegNo + 2;
3728 }
3729
3730 return UseCycle;
3731}
3732
3733int
3734ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003735 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003736 unsigned UseClass,
3737 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003738 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003739 if (RegNo <= 0)
3740 return ItinData->getOperandCycle(UseClass, UseIdx);
3741
3742 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003743 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003744 UseCycle = RegNo / 2;
3745 if (UseCycle < 2)
3746 UseCycle = 2;
3747 // Read in E3.
3748 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003749 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003750 UseCycle = (RegNo / 2);
3751 // If there are odd number of registers or if it's not 64-bit aligned,
3752 // then it takes an extra AGU (Address Generation Unit) cycle.
3753 if ((RegNo % 2) || UseAlign < 8)
3754 ++UseCycle;
3755 } else {
3756 // Assume the worst.
3757 UseCycle = 1;
3758 }
3759 return UseCycle;
3760}
3761
3762int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003763ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003764 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003765 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003766 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003767 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003768 unsigned DefClass = DefMCID.getSchedClass();
3769 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003770
Evan Cheng6cc775f2011-06-28 19:10:37 +00003771 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003772 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3773
3774 // This may be a def / use of a variable_ops instruction, the operand
3775 // latency might be determinable dynamically. Let the target try to
3776 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003777 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003778 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003779 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003780 default:
3781 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3782 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003783
3784 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003785 case ARM::VLDMDIA_UPD:
3786 case ARM::VLDMDDB_UPD:
3787 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003788 case ARM::VLDMSIA_UPD:
3789 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003790 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003791 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003792
3793 case ARM::LDMIA_RET:
3794 case ARM::LDMIA:
3795 case ARM::LDMDA:
3796 case ARM::LDMDB:
3797 case ARM::LDMIB:
3798 case ARM::LDMIA_UPD:
3799 case ARM::LDMDA_UPD:
3800 case ARM::LDMDB_UPD:
3801 case ARM::LDMIB_UPD:
3802 case ARM::tLDMIA:
3803 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003804 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003805 case ARM::t2LDMIA_RET:
3806 case ARM::t2LDMIA:
3807 case ARM::t2LDMDB:
3808 case ARM::t2LDMIA_UPD:
3809 case ARM::t2LDMDB_UPD:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003810 LdmBypass = true;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003811 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003812 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003813 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003814
3815 if (DefCycle == -1)
3816 // We can't seem to determine the result latency of the def, assume it's 2.
3817 DefCycle = 2;
3818
3819 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003820 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003821 default:
3822 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3823 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003824
3825 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003826 case ARM::VSTMDIA_UPD:
3827 case ARM::VSTMDDB_UPD:
3828 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003829 case ARM::VSTMSIA_UPD:
3830 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003831 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003832 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003833
3834 case ARM::STMIA:
3835 case ARM::STMDA:
3836 case ARM::STMDB:
3837 case ARM::STMIB:
3838 case ARM::STMIA_UPD:
3839 case ARM::STMDA_UPD:
3840 case ARM::STMDB_UPD:
3841 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003842 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003843 case ARM::tPOP_RET:
3844 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003845 case ARM::t2STMIA:
3846 case ARM::t2STMDB:
3847 case ARM::t2STMIA_UPD:
3848 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003849 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003850 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003851 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003852
3853 if (UseCycle == -1)
3854 // Assume it's read in the first stage.
3855 UseCycle = 1;
3856
3857 UseCycle = DefCycle - UseCycle + 1;
3858 if (UseCycle > 0) {
3859 if (LdmBypass) {
3860 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3861 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003862 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003863 UseClass, UseIdx))
3864 --UseCycle;
3865 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003866 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003867 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003868 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003869 }
3870
3871 return UseCycle;
3872}
3873
Evan Cheng7fae11b2011-12-14 02:11:42 +00003874static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003875 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003876 unsigned &DefIdx, unsigned &Dist) {
3877 Dist = 0;
3878
3879 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003880 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003881 assert(II->isInsideBundle() && "Empty bundle?");
3882
3883 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003884 while (II->isInsideBundle()) {
3885 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3886 if (Idx != -1)
3887 break;
3888 --II;
3889 ++Dist;
3890 }
3891
3892 assert(Idx != -1 && "Cannot find bundled definition!");
3893 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003894 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003895}
3896
3897static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003898 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003899 unsigned &UseIdx, unsigned &Dist) {
3900 Dist = 0;
3901
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003902 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003903 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003904 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003905
3906 // FIXME: This doesn't properly handle multiple uses.
3907 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003908 while (II != E && II->isInsideBundle()) {
3909 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3910 if (Idx != -1)
3911 break;
3912 if (II->getOpcode() != ARM::t2IT)
3913 ++Dist;
3914 ++II;
3915 }
3916
Evan Chengda103bf2011-12-14 20:00:08 +00003917 if (Idx == -1) {
3918 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003919 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003920 }
3921
Evan Cheng7fae11b2011-12-14 02:11:42 +00003922 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003923 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003924}
3925
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003926/// Return the number of cycles to add to (or subtract from) the static
3927/// itinerary based on the def opcode and alignment. The caller will ensure that
3928/// adjusted latency is at least one cycle.
3929static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003930 const MachineInstr &DefMI,
3931 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003932 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003933 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003934 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3935 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003936 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003937 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003938 case ARM::LDRrs:
3939 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003940 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003941 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3942 if (ShImm == 0 ||
3943 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003944 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003945 break;
3946 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003947 case ARM::t2LDRs:
3948 case ARM::t2LDRBs:
3949 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003950 case ARM::t2LDRSHs: {
3951 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003952 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003953 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003954 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003955 break;
3956 }
3957 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003958 } else if (Subtarget.isSwift()) {
3959 // FIXME: Properly handle all of the latency adjustments for address
3960 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003961 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003962 default: break;
3963 case ARM::LDRrs:
3964 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003965 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003966 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3967 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3968 if (!isSub &&
3969 (ShImm == 0 ||
3970 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3971 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3972 Adjust -= 2;
3973 else if (!isSub &&
3974 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3975 --Adjust;
3976 break;
3977 }
3978 case ARM::t2LDRs:
3979 case ARM::t2LDRBs:
3980 case ARM::t2LDRHs:
3981 case ARM::t2LDRSHs: {
3982 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003983 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003984 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3985 Adjust -= 2;
3986 break;
3987 }
3988 }
Evan Chengff310732010-10-28 06:47:08 +00003989 }
3990
Diana Picus92423ce2016-06-27 09:08:23 +00003991 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003992 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003993 default: break;
3994 case ARM::VLD1q8:
3995 case ARM::VLD1q16:
3996 case ARM::VLD1q32:
3997 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003998 case ARM::VLD1q8wb_fixed:
3999 case ARM::VLD1q16wb_fixed:
4000 case ARM::VLD1q32wb_fixed:
4001 case ARM::VLD1q64wb_fixed:
4002 case ARM::VLD1q8wb_register:
4003 case ARM::VLD1q16wb_register:
4004 case ARM::VLD1q32wb_register:
4005 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004006 case ARM::VLD2d8:
4007 case ARM::VLD2d16:
4008 case ARM::VLD2d32:
4009 case ARM::VLD2q8:
4010 case ARM::VLD2q16:
4011 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00004012 case ARM::VLD2d8wb_fixed:
4013 case ARM::VLD2d16wb_fixed:
4014 case ARM::VLD2d32wb_fixed:
4015 case ARM::VLD2q8wb_fixed:
4016 case ARM::VLD2q16wb_fixed:
4017 case ARM::VLD2q32wb_fixed:
4018 case ARM::VLD2d8wb_register:
4019 case ARM::VLD2d16wb_register:
4020 case ARM::VLD2d32wb_register:
4021 case ARM::VLD2q8wb_register:
4022 case ARM::VLD2q16wb_register:
4023 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004024 case ARM::VLD3d8:
4025 case ARM::VLD3d16:
4026 case ARM::VLD3d32:
4027 case ARM::VLD1d64T:
4028 case ARM::VLD3d8_UPD:
4029 case ARM::VLD3d16_UPD:
4030 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00004031 case ARM::VLD1d64Twb_fixed:
4032 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004033 case ARM::VLD3q8_UPD:
4034 case ARM::VLD3q16_UPD:
4035 case ARM::VLD3q32_UPD:
4036 case ARM::VLD4d8:
4037 case ARM::VLD4d16:
4038 case ARM::VLD4d32:
4039 case ARM::VLD1d64Q:
4040 case ARM::VLD4d8_UPD:
4041 case ARM::VLD4d16_UPD:
4042 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00004043 case ARM::VLD1d64Qwb_fixed:
4044 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004045 case ARM::VLD4q8_UPD:
4046 case ARM::VLD4q16_UPD:
4047 case ARM::VLD4q32_UPD:
4048 case ARM::VLD1DUPq8:
4049 case ARM::VLD1DUPq16:
4050 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00004051 case ARM::VLD1DUPq8wb_fixed:
4052 case ARM::VLD1DUPq16wb_fixed:
4053 case ARM::VLD1DUPq32wb_fixed:
4054 case ARM::VLD1DUPq8wb_register:
4055 case ARM::VLD1DUPq16wb_register:
4056 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004057 case ARM::VLD2DUPd8:
4058 case ARM::VLD2DUPd16:
4059 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00004060 case ARM::VLD2DUPd8wb_fixed:
4061 case ARM::VLD2DUPd16wb_fixed:
4062 case ARM::VLD2DUPd32wb_fixed:
4063 case ARM::VLD2DUPd8wb_register:
4064 case ARM::VLD2DUPd16wb_register:
4065 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004066 case ARM::VLD4DUPd8:
4067 case ARM::VLD4DUPd16:
4068 case ARM::VLD4DUPd32:
4069 case ARM::VLD4DUPd8_UPD:
4070 case ARM::VLD4DUPd16_UPD:
4071 case ARM::VLD4DUPd32_UPD:
4072 case ARM::VLD1LNd8:
4073 case ARM::VLD1LNd16:
4074 case ARM::VLD1LNd32:
4075 case ARM::VLD1LNd8_UPD:
4076 case ARM::VLD1LNd16_UPD:
4077 case ARM::VLD1LNd32_UPD:
4078 case ARM::VLD2LNd8:
4079 case ARM::VLD2LNd16:
4080 case ARM::VLD2LNd32:
4081 case ARM::VLD2LNq16:
4082 case ARM::VLD2LNq32:
4083 case ARM::VLD2LNd8_UPD:
4084 case ARM::VLD2LNd16_UPD:
4085 case ARM::VLD2LNd32_UPD:
4086 case ARM::VLD2LNq16_UPD:
4087 case ARM::VLD2LNq32_UPD:
4088 case ARM::VLD4LNd8:
4089 case ARM::VLD4LNd16:
4090 case ARM::VLD4LNd32:
4091 case ARM::VLD4LNq16:
4092 case ARM::VLD4LNq32:
4093 case ARM::VLD4LNd8_UPD:
4094 case ARM::VLD4LNd16_UPD:
4095 case ARM::VLD4LNd32_UPD:
4096 case ARM::VLD4LNq16_UPD:
4097 case ARM::VLD4LNq32_UPD:
4098 // If the address is not 64-bit aligned, the latencies of these
4099 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004100 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004101 break;
4102 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004103 }
4104 return Adjust;
4105}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004106
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004107int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4108 const MachineInstr &DefMI,
4109 unsigned DefIdx,
4110 const MachineInstr &UseMI,
4111 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004112 // No operand latency. The caller may fall back to getInstrLatency.
4113 if (!ItinData || ItinData->isEmpty())
4114 return -1;
4115
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004116 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004117 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004118
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004119 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004120 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004121 if (DefMI.isBundle())
4122 ResolvedDefMI =
4123 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
4124 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
4125 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004126 return 1;
4127 }
4128
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004129 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004130 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004131 if (UseMI.isBundle()) {
4132 ResolvedUseMI =
4133 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
4134 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00004135 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004136 }
4137
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004138 return getOperandLatencyImpl(
4139 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
4140 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
4141}
4142
4143int ARMBaseInstrInfo::getOperandLatencyImpl(
4144 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4145 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
4146 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
4147 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004148 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004149 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004150 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00004151 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004152 }
4153
4154 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004155 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004156 return 0;
4157
4158 // Otherwise it takes the instruction latency (generally one).
4159 unsigned Latency = getInstrLatency(ItinData, DefMI);
4160
4161 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4162 // its uses. Instructions which are otherwise scheduled between them may
4163 // incur a code size penalty (not able to use the CPSR setting 16-bit
4164 // instructions).
4165 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004166 const MachineFunction *MF = DefMI.getParent()->getParent();
Evandro Menezes85bd3972019-04-04 22:40:06 +00004167 // FIXME: Use Function::hasOptSize().
Matthias Braunf1caa282017-12-15 22:22:58 +00004168 if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004169 --Latency;
4170 }
4171 return Latency;
4172 }
4173
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004174 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00004175 return -1;
4176
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004177 unsigned DefAlign = DefMI.hasOneMemOperand()
4178 ? (*DefMI.memoperands_begin())->getAlignment()
4179 : 0;
4180 unsigned UseAlign = UseMI.hasOneMemOperand()
4181 ? (*UseMI.memoperands_begin())->getAlignment()
4182 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004183
4184 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004185 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
4186 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004187 // Unable to find operand latency. The caller may resort to getInstrLatency.
4188 if (Latency < 0)
4189 return Latency;
4190
4191 // Adjust for IT block position.
4192 int Adj = DefAdj + UseAdj;
4193
4194 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4195 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4196 if (Adj >= 0 || (int)Latency > -Adj) {
4197 return Latency + Adj;
4198 }
4199 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00004200 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004201}
4202
4203int
4204ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4205 SDNode *DefNode, unsigned DefIdx,
4206 SDNode *UseNode, unsigned UseIdx) const {
4207 if (!DefNode->isMachineOpcode())
4208 return 1;
4209
Evan Cheng6cc775f2011-06-28 19:10:37 +00004210 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00004211
Evan Cheng6cc775f2011-06-28 19:10:37 +00004212 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00004213 return 0;
4214
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004215 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004216 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004217
Evan Cheng6c1414f2010-10-29 18:09:28 +00004218 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00004219 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00004220 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4221 int Threshold = 1 + Adj;
4222 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00004223 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004224
Evan Cheng6cc775f2011-06-28 19:10:37 +00004225 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004226 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4227 unsigned DefAlign = !DefMN->memoperands_empty()
4228 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4229 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4230 unsigned UseAlign = !UseMN->memoperands_empty()
4231 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00004232 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4233 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00004234
4235 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00004236 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4237 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00004238 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4239 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00004240 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00004241 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004242 case ARM::LDRrs:
4243 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00004244 unsigned ShOpVal =
4245 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4246 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4247 if (ShImm == 0 ||
4248 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4249 --Latency;
4250 break;
4251 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004252 case ARM::t2LDRs:
4253 case ARM::t2LDRBs:
4254 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00004255 case ARM::t2LDRSHs: {
4256 // Thumb2 mode: lsl only.
4257 unsigned ShAmt =
4258 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4259 if (ShAmt == 0 || ShAmt == 2)
4260 --Latency;
4261 break;
4262 }
4263 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00004264 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4265 // FIXME: Properly handle all of the latency adjustments for address
4266 // writeback.
4267 switch (DefMCID.getOpcode()) {
4268 default: break;
4269 case ARM::LDRrs:
4270 case ARM::LDRBrs: {
4271 unsigned ShOpVal =
4272 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4273 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4274 if (ShImm == 0 ||
4275 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4276 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4277 Latency -= 2;
4278 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4279 --Latency;
4280 break;
4281 }
4282 case ARM::t2LDRs:
4283 case ARM::t2LDRBs:
4284 case ARM::t2LDRHs:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004285 case ARM::t2LDRSHs:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004286 // Thumb2 mode: lsl 0-3 only.
4287 Latency -= 2;
4288 break;
4289 }
Evan Chengff310732010-10-28 06:47:08 +00004290 }
4291
Diana Picus92423ce2016-06-27 09:08:23 +00004292 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004293 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004294 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004295 case ARM::VLD1q8:
4296 case ARM::VLD1q16:
4297 case ARM::VLD1q32:
4298 case ARM::VLD1q64:
4299 case ARM::VLD1q8wb_register:
4300 case ARM::VLD1q16wb_register:
4301 case ARM::VLD1q32wb_register:
4302 case ARM::VLD1q64wb_register:
4303 case ARM::VLD1q8wb_fixed:
4304 case ARM::VLD1q16wb_fixed:
4305 case ARM::VLD1q32wb_fixed:
4306 case ARM::VLD1q64wb_fixed:
4307 case ARM::VLD2d8:
4308 case ARM::VLD2d16:
4309 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004310 case ARM::VLD2q8Pseudo:
4311 case ARM::VLD2q16Pseudo:
4312 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004313 case ARM::VLD2d8wb_fixed:
4314 case ARM::VLD2d16wb_fixed:
4315 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00004316 case ARM::VLD2q8PseudoWB_fixed:
4317 case ARM::VLD2q16PseudoWB_fixed:
4318 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004319 case ARM::VLD2d8wb_register:
4320 case ARM::VLD2d16wb_register:
4321 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00004322 case ARM::VLD2q8PseudoWB_register:
4323 case ARM::VLD2q16PseudoWB_register:
4324 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004325 case ARM::VLD3d8Pseudo:
4326 case ARM::VLD3d16Pseudo:
4327 case ARM::VLD3d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004328 case ARM::VLD1d8TPseudo:
4329 case ARM::VLD1d16TPseudo:
4330 case ARM::VLD1d32TPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004331 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004332 case ARM::VLD1d64TPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004333 case ARM::VLD1d64TPseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004334 case ARM::VLD3d8Pseudo_UPD:
4335 case ARM::VLD3d16Pseudo_UPD:
4336 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004337 case ARM::VLD3q8Pseudo_UPD:
4338 case ARM::VLD3q16Pseudo_UPD:
4339 case ARM::VLD3q32Pseudo_UPD:
4340 case ARM::VLD3q8oddPseudo:
4341 case ARM::VLD3q16oddPseudo:
4342 case ARM::VLD3q32oddPseudo:
4343 case ARM::VLD3q8oddPseudo_UPD:
4344 case ARM::VLD3q16oddPseudo_UPD:
4345 case ARM::VLD3q32oddPseudo_UPD:
4346 case ARM::VLD4d8Pseudo:
4347 case ARM::VLD4d16Pseudo:
4348 case ARM::VLD4d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004349 case ARM::VLD1d8QPseudo:
4350 case ARM::VLD1d16QPseudo:
4351 case ARM::VLD1d32QPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004352 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004353 case ARM::VLD1d64QPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004354 case ARM::VLD1d64QPseudoWB_register:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004355 case ARM::VLD1q8HighQPseudo:
4356 case ARM::VLD1q8LowQPseudo_UPD:
4357 case ARM::VLD1q8HighTPseudo:
4358 case ARM::VLD1q8LowTPseudo_UPD:
4359 case ARM::VLD1q16HighQPseudo:
4360 case ARM::VLD1q16LowQPseudo_UPD:
4361 case ARM::VLD1q16HighTPseudo:
4362 case ARM::VLD1q16LowTPseudo_UPD:
4363 case ARM::VLD1q32HighQPseudo:
4364 case ARM::VLD1q32LowQPseudo_UPD:
4365 case ARM::VLD1q32HighTPseudo:
4366 case ARM::VLD1q32LowTPseudo_UPD:
4367 case ARM::VLD1q64HighQPseudo:
4368 case ARM::VLD1q64LowQPseudo_UPD:
4369 case ARM::VLD1q64HighTPseudo:
4370 case ARM::VLD1q64LowTPseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004371 case ARM::VLD4d8Pseudo_UPD:
4372 case ARM::VLD4d16Pseudo_UPD:
4373 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004374 case ARM::VLD4q8Pseudo_UPD:
4375 case ARM::VLD4q16Pseudo_UPD:
4376 case ARM::VLD4q32Pseudo_UPD:
4377 case ARM::VLD4q8oddPseudo:
4378 case ARM::VLD4q16oddPseudo:
4379 case ARM::VLD4q32oddPseudo:
4380 case ARM::VLD4q8oddPseudo_UPD:
4381 case ARM::VLD4q16oddPseudo_UPD:
4382 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00004383 case ARM::VLD1DUPq8:
4384 case ARM::VLD1DUPq16:
4385 case ARM::VLD1DUPq32:
4386 case ARM::VLD1DUPq8wb_fixed:
4387 case ARM::VLD1DUPq16wb_fixed:
4388 case ARM::VLD1DUPq32wb_fixed:
4389 case ARM::VLD1DUPq8wb_register:
4390 case ARM::VLD1DUPq16wb_register:
4391 case ARM::VLD1DUPq32wb_register:
4392 case ARM::VLD2DUPd8:
4393 case ARM::VLD2DUPd16:
4394 case ARM::VLD2DUPd32:
4395 case ARM::VLD2DUPd8wb_fixed:
4396 case ARM::VLD2DUPd16wb_fixed:
4397 case ARM::VLD2DUPd32wb_fixed:
4398 case ARM::VLD2DUPd8wb_register:
4399 case ARM::VLD2DUPd16wb_register:
4400 case ARM::VLD2DUPd32wb_register:
Ivan A. Kosarev72315982018-06-27 13:57:52 +00004401 case ARM::VLD2DUPq8EvenPseudo:
4402 case ARM::VLD2DUPq8OddPseudo:
4403 case ARM::VLD2DUPq16EvenPseudo:
4404 case ARM::VLD2DUPq16OddPseudo:
4405 case ARM::VLD2DUPq32EvenPseudo:
4406 case ARM::VLD2DUPq32OddPseudo:
4407 case ARM::VLD3DUPq8EvenPseudo:
4408 case ARM::VLD3DUPq8OddPseudo:
4409 case ARM::VLD3DUPq16EvenPseudo:
4410 case ARM::VLD3DUPq16OddPseudo:
4411 case ARM::VLD3DUPq32EvenPseudo:
4412 case ARM::VLD3DUPq32OddPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004413 case ARM::VLD4DUPd8Pseudo:
4414 case ARM::VLD4DUPd16Pseudo:
4415 case ARM::VLD4DUPd32Pseudo:
4416 case ARM::VLD4DUPd8Pseudo_UPD:
4417 case ARM::VLD4DUPd16Pseudo_UPD:
4418 case ARM::VLD4DUPd32Pseudo_UPD:
Ivan A. Kosarev72315982018-06-27 13:57:52 +00004419 case ARM::VLD4DUPq8EvenPseudo:
4420 case ARM::VLD4DUPq8OddPseudo:
4421 case ARM::VLD4DUPq16EvenPseudo:
4422 case ARM::VLD4DUPq16OddPseudo:
4423 case ARM::VLD4DUPq32EvenPseudo:
4424 case ARM::VLD4DUPq32OddPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004425 case ARM::VLD1LNq8Pseudo:
4426 case ARM::VLD1LNq16Pseudo:
4427 case ARM::VLD1LNq32Pseudo:
4428 case ARM::VLD1LNq8Pseudo_UPD:
4429 case ARM::VLD1LNq16Pseudo_UPD:
4430 case ARM::VLD1LNq32Pseudo_UPD:
4431 case ARM::VLD2LNd8Pseudo:
4432 case ARM::VLD2LNd16Pseudo:
4433 case ARM::VLD2LNd32Pseudo:
4434 case ARM::VLD2LNq16Pseudo:
4435 case ARM::VLD2LNq32Pseudo:
4436 case ARM::VLD2LNd8Pseudo_UPD:
4437 case ARM::VLD2LNd16Pseudo_UPD:
4438 case ARM::VLD2LNd32Pseudo_UPD:
4439 case ARM::VLD2LNq16Pseudo_UPD:
4440 case ARM::VLD2LNq32Pseudo_UPD:
4441 case ARM::VLD4LNd8Pseudo:
4442 case ARM::VLD4LNd16Pseudo:
4443 case ARM::VLD4LNd32Pseudo:
4444 case ARM::VLD4LNq16Pseudo:
4445 case ARM::VLD4LNq32Pseudo:
4446 case ARM::VLD4LNd8Pseudo_UPD:
4447 case ARM::VLD4LNd16Pseudo_UPD:
4448 case ARM::VLD4LNd32Pseudo_UPD:
4449 case ARM::VLD4LNq16Pseudo_UPD:
4450 case ARM::VLD4LNq32Pseudo_UPD:
4451 // If the address is not 64-bit aligned, the latencies of these
4452 // instructions increases by one.
4453 ++Latency;
4454 break;
4455 }
4456
Evan Chengff310732010-10-28 06:47:08 +00004457 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004458}
Evan Cheng63c76082010-10-19 18:58:51 +00004459
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004460unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4461 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4462 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004463 return 0;
4464
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004465 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004466 return 0;
4467
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004468 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004469
Javed Absar4ae7e8122017-06-02 08:53:19 +00004470 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4471 !Subtarget.cheapPredicableCPSRDef())) {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004472 // When predicated, CPSR is an additional source operand for CPSR updating
4473 // instructions, this apparently increases their latencies.
4474 return 1;
4475 }
4476 return 0;
4477}
4478
Andrew Trick45446062012-06-05 21:11:27 +00004479unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004480 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004481 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004482 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4483 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004484 return 1;
4485
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004486 // An instruction scheduler typically runs on unbundled instructions, however
4487 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004488 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004489 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004490 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4491 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004492 while (++I != E && I->isInsideBundle()) {
4493 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004494 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004495 }
4496 return Latency;
4497 }
4498
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004499 const MCInstrDesc &MCID = MI.getDesc();
Javed Absar4ae7e8122017-06-02 08:53:19 +00004500 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4501 !Subtarget.cheapPredicableCPSRDef()))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004502 // When predicated, CPSR is an additional source operand for CPSR updating
4503 // instructions, this apparently increases their latencies.
4504 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004505 }
4506 // Be sure to call getStageLatency for an empty itinerary in case it has a
4507 // valid MinLatency property.
4508 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004509 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004510
4511 unsigned Class = MCID.getSchedClass();
4512
4513 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004514 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004515 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004516
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004517 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004518 unsigned Latency = ItinData->getStageLatency(Class);
4519
4520 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004521 unsigned DefAlign =
4522 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4523 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004524 if (Adj >= 0 || (int)Latency > -Adj) {
4525 return Latency + Adj;
4526 }
4527 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004528}
4529
4530int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4531 SDNode *Node) const {
4532 if (!Node->isMachineOpcode())
4533 return 1;
4534
4535 if (!ItinData || ItinData->isEmpty())
4536 return 1;
4537
4538 unsigned Opcode = Node->getMachineOpcode();
4539 switch (Opcode) {
4540 default:
4541 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004542 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004543 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004544 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004545 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004546}
4547
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004548bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4549 const MachineRegisterInfo *MRI,
4550 const MachineInstr &DefMI,
4551 unsigned DefIdx,
4552 const MachineInstr &UseMI,
4553 unsigned UseIdx) const {
4554 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4555 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004556 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004557 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004558 return true;
4559
4560 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004561 unsigned Latency =
4562 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004563 if (Latency <= 3)
4564 return false;
4565 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4566 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4567}
Evan Chenge96b8d72010-10-26 02:08:50 +00004568
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004569bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4570 const MachineInstr &DefMI,
4571 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004572 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004573 if (!ItinData || ItinData->isEmpty())
4574 return false;
4575
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004576 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004577 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004578 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004579 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4580 return (DefCycle != -1 && DefCycle <= 2);
4581 }
4582 return false;
4583}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004584
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004585bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004586 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004587 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004588 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4589 return false;
4590 }
Eli Friedman68d9a602019-03-15 21:44:49 +00004591 if (MI.getOpcode() == ARM::tMOVr && !Subtarget.hasV6Ops()) {
4592 // Make sure we don't generate a lo-lo mov that isn't supported.
4593 if (!ARM::hGPRRegClass.contains(MI.getOperand(0).getReg()) &&
4594 !ARM::hGPRRegClass.contains(MI.getOperand(1).getReg())) {
4595 ErrInfo = "Non-flag-setting Thumb1 mov is v6-only";
4596 return false;
4597 }
4598 }
4599 if (MI.getOpcode() == ARM::tPUSH ||
4600 MI.getOpcode() == ARM::tPOP ||
4601 MI.getOpcode() == ARM::tPOP_RET) {
4602 for (int i = 2, e = MI.getNumOperands(); i < e; ++i) {
4603 if (MI.getOperand(i).isImplicit() ||
4604 !MI.getOperand(i).isReg())
4605 continue;
4606 unsigned Reg = MI.getOperand(i).getReg();
4607 if (Reg < ARM::R0 || Reg > ARM::R7) {
4608 if (!(MI.getOpcode() == ARM::tPUSH && Reg == ARM::LR) &&
4609 !(MI.getOpcode() == ARM::tPOP_RET && Reg == ARM::PC)) {
4610 ErrInfo = "Unsupported register in Thumb1 push/pop";
4611 return false;
4612 }
4613 }
4614 }
4615 }
Andrew Trick924123a2011-09-21 02:20:46 +00004616 return true;
4617}
4618
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004619// LoadStackGuard has so far only been implemented for MachO. Different code
4620// sequence is needed for other targets.
4621void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4622 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004623 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004624 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4625 "ROPI/RWPI not currently supported with stack guard");
4626
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004627 MachineBasicBlock &MBB = *MI->getParent();
4628 DebugLoc DL = MI->getDebugLoc();
4629 unsigned Reg = MI->getOperand(0).getReg();
4630 const GlobalValue *GV =
4631 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4632 MachineInstrBuilder MIB;
4633
4634 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4635 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4636
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004637 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004638 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4639 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004640 auto Flags = MachineMemOperand::MOLoad |
4641 MachineMemOperand::MODereferenceable |
4642 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004643 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004644 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004645 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004646 }
4647
4648 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004649 MIB.addReg(Reg, RegState::Kill)
Chandler Carruthc73c0302018-08-16 21:30:05 +00004650 .addImm(0)
4651 .cloneMemRefs(*MI)
4652 .add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004653}
4654
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004655bool
4656ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4657 unsigned &AddSubOpc,
4658 bool &NegAcc, bool &HasLane) const {
4659 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4660 if (I == MLxEntryMap.end())
4661 return false;
4662
4663 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4664 MulOpc = Entry.MulOpc;
4665 AddSubOpc = Entry.AddSubOpc;
4666 NegAcc = Entry.NegAcc;
4667 HasLane = Entry.HasLane;
4668 return true;
4669}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004670
4671//===----------------------------------------------------------------------===//
4672// Execution domains.
4673//===----------------------------------------------------------------------===//
4674//
4675// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4676// and some can go down both. The vmov instructions go down the VFP pipeline,
4677// but they can be changed to vorr equivalents that are executed by the NEON
4678// pipeline.
4679//
4680// We use the following execution domain numbering:
4681//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004682enum ARMExeDomain {
4683 ExeGeneric = 0,
4684 ExeVFP = 1,
4685 ExeNEON = 2
4686};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004687
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004688//
4689// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4690//
4691std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004692ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004693 // If we don't have access to NEON instructions then we won't be able
4694 // to swizzle anything to the NEON domain. Check to make sure.
4695 if (Subtarget.hasNEON()) {
4696 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4697 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004698 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004699 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004700
Eric Christopher7e70aba2015-03-07 00:12:22 +00004701 // CortexA9 is particularly picky about mixing the two and wants these
4702 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004703 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4704 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4705 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004706 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4707 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004708 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004709 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004710
4711 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004712 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004713
4714 // Certain instructions can go either way on Cortex-A8.
4715 // Treat them as NEON instructions.
4716 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004717 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004718
4719 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004720 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004721
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004722 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004723}
4724
Tim Northover771f1602012-08-29 16:36:07 +00004725static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4726 unsigned SReg, unsigned &Lane) {
4727 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4728 Lane = 0;
4729
4730 if (DReg != ARM::NoRegister)
4731 return DReg;
4732
4733 Lane = 1;
4734 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4735
4736 assert(DReg && "S-register with no D super-register?");
4737 return DReg;
4738}
4739
Andrew Trickd9296ec2012-10-10 05:43:01 +00004740/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004741/// set ImplicitSReg to a register number that must be marked as implicit-use or
4742/// zero if no register needs to be defined as implicit-use.
4743///
4744/// If the function cannot determine if an SPR should be marked implicit use or
4745/// not, it returns false.
4746///
4747/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004748/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004749/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4750/// lane of the DPR).
4751///
4752/// If the other SPR is defined, an implicit-use of it should be added. Else,
4753/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004754///
James Molloyea052562012-09-18 08:31:15 +00004755static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004756 MachineInstr &MI, unsigned DReg,
4757 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004758 // If the DPR is defined or used already, the other SPR lane will be chained
4759 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004760 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004761 ImplicitSReg = 0;
4762 return true;
4763 }
4764
4765 // Otherwise we need to go searching to see if the SPR is set explicitly.
4766 ImplicitSReg = TRI->getSubReg(DReg,
4767 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4768 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004769 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004770
4771 if (LQR == MachineBasicBlock::LQR_Live)
4772 return true;
4773 else if (LQR == MachineBasicBlock::LQR_Unknown)
4774 return false;
4775
4776 // If the register is known not to be live, there is no need to add an
4777 // implicit-use.
4778 ImplicitSReg = 0;
4779 return true;
4780}
Tim Northover771f1602012-08-29 16:36:07 +00004781
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004782void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4783 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004784 unsigned DstReg, SrcReg, DReg;
4785 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004786 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004787 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004788 switch (MI.getOpcode()) {
4789 default:
4790 llvm_unreachable("cannot handle opcode!");
4791 break;
4792 case ARM::VMOVD:
4793 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004794 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004795
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004796 // Zap the predicate operands.
4797 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004798
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004799 // Make sure we've got NEON instructions.
4800 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004801
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004802 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4803 DstReg = MI.getOperand(0).getReg();
4804 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004805
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004806 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4807 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004808
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004809 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4810 MI.setDesc(get(ARM::VORRd));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004811 MIB.addReg(DstReg, RegState::Define)
4812 .addReg(SrcReg)
4813 .addReg(SrcReg)
4814 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004815 break;
4816 case ARM::VMOVRS:
4817 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004818 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004819 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004820
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004821 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4822 DstReg = MI.getOperand(0).getReg();
4823 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004824
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004825 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4826 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004827
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004828 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004829
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004830 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4831 // Note that DSrc has been widened and the other lane may be undef, which
4832 // contaminates the entire register.
4833 MI.setDesc(get(ARM::VGETLNi32));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004834 MIB.addReg(DstReg, RegState::Define)
4835 .addReg(DReg, RegState::Undef)
4836 .addImm(Lane)
4837 .add(predOps(ARMCC::AL));
Tim Northoverf6618152012-08-17 11:32:52 +00004838
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004839 // The old source should be an implicit use, otherwise we might think it
4840 // was dead before here.
4841 MIB.addReg(SrcReg, RegState::Implicit);
4842 break;
4843 case ARM::VMOVSR: {
4844 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004845 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004846 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004847
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004848 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4849 DstReg = MI.getOperand(0).getReg();
4850 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004851
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004852 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004853
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004854 unsigned ImplicitSReg;
4855 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004856 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004857
4858 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4859 MI.RemoveOperand(i - 1);
4860
4861 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4862 // Again DDst may be undefined at the beginning of this instruction.
4863 MI.setDesc(get(ARM::VSETLNi32));
4864 MIB.addReg(DReg, RegState::Define)
4865 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4866 .addReg(SrcReg)
Diana Picus4f8c3e12017-01-13 09:37:56 +00004867 .addImm(Lane)
4868 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004869
4870 // The narrower destination must be marked as set to keep previous chains
4871 // in place.
4872 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4873 if (ImplicitSReg != 0)
4874 MIB.addReg(ImplicitSReg, RegState::Implicit);
4875 break;
James Molloyea052562012-09-18 08:31:15 +00004876 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004877 case ARM::VMOVS: {
4878 if (Domain != ExeNEON)
4879 break;
4880
4881 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004882 DstReg = MI.getOperand(0).getReg();
4883 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004884
Tim Northoverca9f3842012-08-30 10:17:45 +00004885 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4886 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4887 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4888
James Molloyea052562012-09-18 08:31:15 +00004889 unsigned ImplicitSReg;
4890 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4891 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004892
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004893 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4894 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004895
Tim Northoverca9f3842012-08-30 10:17:45 +00004896 if (DSrc == DDst) {
4897 // Destination can be:
4898 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004899 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004900 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004901 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
Diana Picus4f8c3e12017-01-13 09:37:56 +00004902 .addImm(SrcLane)
4903 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004904
4905 // Neither the source or the destination are naturally represented any
4906 // more, so add them in manually.
4907 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4908 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004909 if (ImplicitSReg != 0)
4910 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004911 break;
4912 }
4913
4914 // In general there's no single instruction that can perform an S <-> S
4915 // move in NEON space, but a pair of VEXT instructions *can* do the
4916 // job. It turns out that the VEXTs needed will only use DSrc once, with
4917 // the position based purely on the combination of lane-0 and lane-1
4918 // involved. For example
4919 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4920 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4921 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4922 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4923 //
4924 // Pattern of the MachineInstrs is:
4925 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4926 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004927 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4928 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004929
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004930 // On the first instruction, both DSrc and DDst may be undef if present.
Tim Northover726d32c2012-09-01 18:07:29 +00004931 // Specifically when the original instruction didn't have them as an
4932 // <imp-use>.
4933 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004934 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004935 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4936
4937 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004938 CurUndef = !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004939 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4940 .addImm(1)
4941 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004942
4943 if (SrcLane == DstLane)
4944 NewMIB.addReg(SrcReg, RegState::Implicit);
4945
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004946 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004947 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004948
4949 // On the second instruction, DDst has definitely been defined above, so
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004950 // it is not undef. DSrc, if present, can be undef as above.
Tim Northover726d32c2012-09-01 18:07:29 +00004951 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004952 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004953 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4954
4955 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004956 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004957 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4958 .addImm(1)
4959 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004960
4961 if (SrcLane != DstLane)
4962 MIB.addReg(SrcReg, RegState::Implicit);
4963
4964 // As before, the original destination is no longer represented, add it
4965 // implicitly.
4966 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004967 if (ImplicitSReg != 0)
4968 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004969 break;
4970 }
Tim Northoverf6618152012-08-17 11:32:52 +00004971 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004972}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004973
Bob Wilsone8a549c2012-09-29 21:43:49 +00004974//===----------------------------------------------------------------------===//
4975// Partial register updates
4976//===----------------------------------------------------------------------===//
4977//
4978// Swift renames NEON registers with 64-bit granularity. That means any
4979// instruction writing an S-reg implicitly reads the containing D-reg. The
4980// problem is mostly avoided by translating f32 operations to v2f32 operations
4981// on D-registers, but f32 loads are still a problem.
4982//
4983// These instructions can load an f32 into a NEON register:
4984//
4985// VLDRS - Only writes S, partial D update.
4986// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4987// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4988//
4989// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004990unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4991 const MachineInstr &MI, unsigned OpNum,
4992 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004993 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4994 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004995 return 0;
4996
4997 assert(TRI && "Need TRI instance");
4998
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004999 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005000 if (MO.readsReg())
5001 return 0;
5002 unsigned Reg = MO.getReg();
5003 int UseOp = -1;
5004
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005005 switch (MI.getOpcode()) {
5006 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00005007 case ARM::VLDRS:
5008 case ARM::FCONSTS:
5009 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00005010 case ARM::VMOVv8i8:
5011 case ARM::VMOVv4i16:
5012 case ARM::VMOVv2i32:
5013 case ARM::VMOVv2f32:
5014 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005015 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005016 break;
5017
5018 // Explicitly reads the dependency.
5019 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00005020 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00005021 break;
5022 default:
5023 return 0;
5024 }
5025
5026 // If this instruction actually reads a value from Reg, there is no unwanted
5027 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005028 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00005029 return 0;
5030
5031 // We must be able to clobber the whole D-reg.
5032 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00005033 // Virtual register must be a def undef foo:ssub_0 operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005034 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00005035 return 0;
5036 } else if (ARM::SPRRegClass.contains(Reg)) {
5037 // Physical register: MI must define the full D-reg.
5038 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
5039 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005040 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00005041 return 0;
5042 }
5043
5044 // MI has an unwanted D-register dependency.
5045 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00005046 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00005047}
5048
5049// Break a partial register dependency after getPartialRegUpdateClearance
5050// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005051void ARMBaseInstrInfo::breakPartialRegDependency(
5052 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
5053 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00005054 assert(TRI && "Need TRI instance");
5055
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005056 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005057 unsigned Reg = MO.getReg();
5058 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
5059 "Can't break virtual register dependencies.");
5060 unsigned DReg = Reg;
5061
5062 // If MI defines an S-reg, find the corresponding D super-register.
5063 if (ARM::SPRRegClass.contains(Reg)) {
5064 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
5065 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
5066 }
5067
5068 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005069 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00005070
5071 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
5072 // the full D-register by loading the same value to both lanes. The
5073 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00005074 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00005075 // too big regressions.
5076
5077 // Insert the dependency-breaking FCONSTD before MI.
5078 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Diana Picus4f8c3e12017-01-13 09:37:56 +00005079 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
5080 .addImm(96)
5081 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005082 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005083}
5084
Jim Grosbach617f84dd2012-02-28 23:53:30 +00005085bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00005086 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00005087}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00005088
5089bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00005090 if (MI->getNumOperands() < 4)
5091 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00005092 unsigned ShOpVal = MI->getOperand(3).getImm();
5093 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
5094 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
5095 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
5096 ((ShImm == 1 || ShImm == 2) &&
5097 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
5098 return true;
5099
5100 return false;
5101}
Quentin Colombetd358e842014-08-22 18:05:22 +00005102
5103bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
5104 const MachineInstr &MI, unsigned DefIdx,
5105 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
5106 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5107 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
5108
5109 switch (MI.getOpcode()) {
5110 case ARM::VMOVDRR:
5111 // dX = VMOVDRR rY, rZ
5112 // is the same as:
5113 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
5114 // Populate the InputRegs accordingly.
5115 // rY
5116 const MachineOperand *MOReg = &MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00005117 if (!MOReg->isUndef())
5118 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5119 MOReg->getSubReg(), ARM::ssub_0));
Quentin Colombetd358e842014-08-22 18:05:22 +00005120 // rZ
5121 MOReg = &MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005122 if (!MOReg->isUndef())
5123 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5124 MOReg->getSubReg(), ARM::ssub_1));
Quentin Colombetd358e842014-08-22 18:05:22 +00005125 return true;
5126 }
5127 llvm_unreachable("Target dependent opcode missing");
5128}
5129
5130bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
5131 const MachineInstr &MI, unsigned DefIdx,
5132 RegSubRegPairAndIdx &InputReg) const {
5133 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5134 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
5135
5136 switch (MI.getOpcode()) {
5137 case ARM::VMOVRRD:
5138 // rX, rY = VMOVRRD dZ
5139 // is the same as:
5140 // rX = EXTRACT_SUBREG dZ, ssub_0
5141 // rY = EXTRACT_SUBREG dZ, ssub_1
5142 const MachineOperand &MOReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005143 if (MOReg.isUndef())
5144 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005145 InputReg.Reg = MOReg.getReg();
5146 InputReg.SubReg = MOReg.getSubReg();
5147 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
5148 return true;
5149 }
5150 llvm_unreachable("Target dependent opcode missing");
5151}
5152
5153bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5154 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
5155 RegSubRegPairAndIdx &InsertedReg) const {
5156 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5157 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
5158
5159 switch (MI.getOpcode()) {
5160 case ARM::VSETLNi32:
5161 // dX = VSETLNi32 dY, rZ, imm
5162 const MachineOperand &MOBaseReg = MI.getOperand(1);
5163 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005164 if (MOInsertedReg.isUndef())
5165 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005166 const MachineOperand &MOIndex = MI.getOperand(3);
5167 BaseReg.Reg = MOBaseReg.getReg();
5168 BaseReg.SubReg = MOBaseReg.getSubReg();
5169
5170 InsertedReg.Reg = MOInsertedReg.getReg();
5171 InsertedReg.SubReg = MOInsertedReg.getSubReg();
5172 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
5173 return true;
5174 }
5175 llvm_unreachable("Target dependent opcode missing");
5176}
Martin Storsjod3b29222018-08-22 20:34:06 +00005177
5178std::pair<unsigned, unsigned>
5179ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
5180 const unsigned Mask = ARMII::MO_OPTION_MASK;
5181 return std::make_pair(TF & Mask, TF & ~Mask);
5182}
5183
5184ArrayRef<std::pair<unsigned, const char *>>
5185ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5186 using namespace ARMII;
5187
5188 static const std::pair<unsigned, const char *> TargetFlags[] = {
5189 {MO_LO16, "arm-lo16"}, {MO_HI16, "arm-hi16"}};
5190 return makeArrayRef(TargetFlags);
5191}
5192
5193ArrayRef<std::pair<unsigned, const char *>>
5194ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5195 using namespace ARMII;
5196
5197 static const std::pair<unsigned, const char *> TargetFlags[] = {
Martin Storsjo2dcaa412018-08-31 08:00:25 +00005198 {MO_COFFSTUB, "arm-coffstub"},
Martin Storsjod3b29222018-08-22 20:34:06 +00005199 {MO_GOT, "arm-got"},
5200 {MO_SBREL, "arm-sbrel"},
5201 {MO_DLLIMPORT, "arm-dllimport"},
5202 {MO_SECREL, "arm-secrel"},
5203 {MO_NONLAZY, "arm-nonlazy"}};
5204 return makeArrayRef(TargetFlags);
5205}
David Greenc519d3c2019-04-23 12:11:26 +00005206
5207bool llvm::registerDefinedBetween(unsigned Reg,
5208 MachineBasicBlock::iterator From,
5209 MachineBasicBlock::iterator To,
5210 const TargetRegisterInfo *TRI) {
5211 for (auto I = From; I != To; ++I)
5212 if (I->modifiesRegister(Reg, TRI))
5213 return true;
5214 return false;
5215}
5216
5217MachineInstr *llvm::findCMPToFoldIntoCBZ(MachineInstr *Br,
5218 const TargetRegisterInfo *TRI) {
5219 // Search backwards to the instruction that defines CSPR. This may or not
5220 // be a CMP, we check that after this loop. If we find another instruction
5221 // that reads cpsr, we return nullptr.
5222 MachineBasicBlock::iterator CmpMI = Br;
5223 while (CmpMI != Br->getParent()->begin()) {
5224 --CmpMI;
5225 if (CmpMI->modifiesRegister(ARM::CPSR, TRI))
5226 break;
5227 if (CmpMI->readsRegister(ARM::CPSR, TRI))
5228 break;
5229 }
5230
5231 // Check that this inst is a CMP r[0-7], #0 and that the register
5232 // is not redefined between the cmp and the br.
5233 if (CmpMI->getOpcode() != ARM::tCMPi8 && CmpMI->getOpcode() != ARM::t2CMPri)
5234 return nullptr;
5235 unsigned Reg = CmpMI->getOperand(0).getReg();
5236 unsigned PredReg = 0;
5237 ARMCC::CondCodes Pred = getInstrPredicate(*CmpMI, PredReg);
5238 if (Pred != ARMCC::AL || CmpMI->getOperand(1).getImm() != 0)
5239 return nullptr;
5240 if (!isARMLowRegister(Reg))
5241 return nullptr;
5242 if (registerDefinedBetween(Reg, CmpMI->getNextNode(), Br, TRI))
5243 return nullptr;
5244
5245 return &*CmpMI;
5246}