blob: b55af47d7db11295442bc6d9115aaba7a8b7aadc [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
David Goodwinaf7451b2009-07-08 16:09:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the Base ARM implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
Amara Emerson52cfb6a2013-10-03 09:31:51 +000013#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000014#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000015#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000016#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000017#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000018#include "ARMMachineFunctionInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000019#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000020#include "MCTargetDesc/ARMAddressingModes.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000021#include "MCTargetDesc/ARMBaseInfo.h"
22#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000024#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000026#include "llvm/ADT/Triple.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000027#include "llvm/CodeGen/LiveVariables.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000029#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstr.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000035#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000037#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Chenga20cde32011-07-20 23:34:39 +000038#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000039#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun88e21312015-06-13 03:42:11 +000041#include "llvm/CodeGen/TargetSchedule.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000042#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Constants.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000044#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Function.h"
46#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000047#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000048#include "llvm/MC/MCInstrDesc.h"
49#include "llvm/MC/MCInstrItineraries.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000050#include "llvm/Support/BranchProbability.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000051#include "llvm/Support/Casting.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000052#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000053#include "llvm/Support/Compiler.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000054#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000055#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000056#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000057#include "llvm/Target/TargetMachine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000058#include <algorithm>
59#include <cassert>
60#include <cstdint>
61#include <iterator>
62#include <new>
63#include <utility>
64#include <vector>
Evan Cheng1e210d02011-06-28 20:07:07 +000065
David Goodwinaf7451b2009-07-08 16:09:28 +000066using namespace llvm;
67
Chandler Carruthe96dd892014-04-21 22:55:11 +000068#define DEBUG_TYPE "arm-instrinfo"
69
Chandler Carruthd174b722014-04-22 02:03:14 +000070#define GET_INSTRINFO_CTOR_DTOR
71#include "ARMGenInstrInfo.inc"
72
David Goodwinaf7451b2009-07-08 16:09:28 +000073static cl::opt<bool>
74EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
75 cl::desc("Enable ARM 2-addr to 3-addr conv"));
76
Evan Cheng62c7b5b2010-12-05 22:04:16 +000077/// ARM_MLxEntry - Record information about MLA / MLS instructions.
78struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000079 uint16_t MLxOpc; // MLA / MLS opcode
80 uint16_t MulOpc; // Expanded multiplication opcode
81 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000082 bool NegAcc; // True if the acc is negated before the add / sub.
83 bool HasLane; // True if instruction has an extra "lane" operand.
84};
85
86static const ARM_MLxEntry ARM_MLxTable[] = {
87 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
88 // fp scalar ops
89 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
90 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
91 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
92 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000093 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
94 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
95 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
96 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
97
98 // fp SIMD ops
99 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
100 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
101 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
102 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
103 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
104 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
105 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
106 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
107};
108
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000109ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +0000110 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000111 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000112 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
113 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000114 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000115 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
116 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
117 }
118}
119
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000120// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
121// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000122ScheduleHazardRecognizer *
123ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
124 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000125 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000126 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000127 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000128 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
129 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000130 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000131}
132
133ScheduleHazardRecognizer *ARMBaseInstrInfo::
134CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
135 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000136 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000137 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000138 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000139}
140
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000141MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
142 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000143 // FIXME: Thumb2 support.
144
David Goodwinaf7451b2009-07-08 16:09:28 +0000145 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000146 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000147
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 MachineFunction &MF = *MI.getParent()->getParent();
149 uint64_t TSFlags = MI.getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000150 bool isPre = false;
151 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000153 case ARMII::IndexModePre:
154 isPre = true;
155 break;
156 case ARMII::IndexModePost:
157 break;
158 }
159
160 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
161 // operation.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000162 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
David Goodwinaf7451b2009-07-08 16:09:28 +0000163 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000164 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000165
Craig Topper062a2ba2014-04-25 05:30:21 +0000166 MachineInstr *UpdateMI = nullptr;
167 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000168 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000170 unsigned NumOps = MCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000171 bool isLoad = !MI.mayStore();
172 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
173 const MachineOperand &Base = MI.getOperand(2);
174 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
David Goodwinaf7451b2009-07-08 16:09:28 +0000175 unsigned WBReg = WB.getReg();
176 unsigned BaseReg = Base.getReg();
177 unsigned OffReg = Offset.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000178 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
179 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
David Goodwinaf7451b2009-07-08 16:09:28 +0000180 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000181 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000182 case ARMII::AddrMode2: {
183 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
184 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
185 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000186 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000187 // Can't encode it in a so_imm operand. This transformation will
188 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000189 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000190 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000191 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000192 .addReg(BaseReg)
193 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000194 .add(predOps(Pred))
195 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000196 } else if (Amt != 0) {
197 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
198 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000199 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000200 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000201 .addReg(BaseReg)
202 .addReg(OffReg)
203 .addReg(0)
204 .addImm(SOOpc)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000205 .add(predOps(Pred))
206 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000207 } else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000208 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000209 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000210 .addReg(BaseReg)
211 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000212 .add(predOps(Pred))
213 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000214 break;
215 }
216 case ARMII::AddrMode3 : {
217 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
218 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
219 if (OffReg == 0)
220 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000221 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000222 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 .addReg(BaseReg)
224 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000225 .add(predOps(Pred))
226 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000227 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000228 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000229 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230 .addReg(BaseReg)
231 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000232 .add(predOps(Pred))
233 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000234 break;
235 }
236 }
237
238 std::vector<MachineInstr*> NewMIs;
239 if (isPre) {
240 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000241 MemMI =
242 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
243 .addReg(WBReg)
244 .addImm(0)
245 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000246 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000247 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
248 .addReg(MI.getOperand(1).getReg())
249 .addReg(WBReg)
250 .addReg(0)
251 .addImm(0)
252 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000253 NewMIs.push_back(MemMI);
254 NewMIs.push_back(UpdateMI);
255 } else {
256 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000257 MemMI =
258 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
259 .addReg(BaseReg)
260 .addImm(0)
261 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000262 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000263 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
264 .addReg(MI.getOperand(1).getReg())
265 .addReg(BaseReg)
266 .addReg(0)
267 .addImm(0)
268 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000269 if (WB.isDead())
270 UpdateMI->getOperand(0).setIsDead();
271 NewMIs.push_back(UpdateMI);
272 NewMIs.push_back(MemMI);
273 }
274
275 // Transfer LiveVariables states, kill / dead info.
276 if (LV) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000277 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
278 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000279 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000280 unsigned Reg = MO.getReg();
281
282 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
283 if (MO.isDef()) {
284 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
285 if (MO.isDead())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000286 LV->addVirtualRegisterDead(Reg, *NewMI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000287 }
288 if (MO.isUse() && MO.isKill()) {
289 for (unsigned j = 0; j < 2; ++j) {
290 // Look at the two new MI's in reverse order.
291 MachineInstr *NewMI = NewMIs[j];
292 if (!NewMI->readsRegister(Reg))
293 continue;
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000294 LV->addVirtualRegisterKilled(Reg, *NewMI);
295 if (VI.removeKill(MI))
David Goodwinaf7451b2009-07-08 16:09:28 +0000296 VI.Kills.push_back(NewMI);
297 break;
298 }
299 }
300 }
301 }
302 }
303
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000304 MachineBasicBlock::iterator MBBI = MI.getIterator();
David Goodwinaf7451b2009-07-08 16:09:28 +0000305 MFI->insert(MBBI, NewMIs[1]);
306 MFI->insert(MBBI, NewMIs[0]);
307 return NewMIs[0];
308}
309
310// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000311bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
312 MachineBasicBlock *&TBB,
313 MachineBasicBlock *&FBB,
314 SmallVectorImpl<MachineOperand> &Cond,
315 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000316 TBB = nullptr;
317 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000318
David Goodwinaf7451b2009-07-08 16:09:28 +0000319 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000320 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000321 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000322 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000323
324 // Walk backwards from the end of the basic block until the branch is
325 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000326 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000327 // Flag to be raised on unanalyzeable instructions. This is useful in cases
328 // where we want to clean up on the end of the basic block before we bail
329 // out.
330 bool CantAnalyze = false;
331
332 // Skip over DEBUG values and predicated nonterminators.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000333 while (I->isDebugInstr() || !I->isTerminator()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000334 if (I == MBB.begin())
335 return false;
336 --I;
337 }
338
339 if (isIndirectBranchOpcode(I->getOpcode()) ||
340 isJumpTableBranchOpcode(I->getOpcode())) {
341 // Indirect branches and jump tables can't be analyzed, but we still want
342 // to clean up any instructions at the tail of the basic block.
343 CantAnalyze = true;
344 } else if (isUncondBranchOpcode(I->getOpcode())) {
345 TBB = I->getOperand(0).getMBB();
346 } else if (isCondBranchOpcode(I->getOpcode())) {
347 // Bail out if we encounter multiple conditional branches.
348 if (!Cond.empty())
349 return true;
350
351 assert(!FBB && "FBB should have been null.");
352 FBB = TBB;
353 TBB = I->getOperand(0).getMBB();
354 Cond.push_back(I->getOperand(1));
355 Cond.push_back(I->getOperand(2));
356 } else if (I->isReturn()) {
357 // Returns can't be analyzed, but we should run cleanup.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000358 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000359 } else {
360 // We encountered other unrecognized terminator. Bail out immediately.
361 return true;
362 }
363
364 // Cleanup code - to be run for unpredicated unconditional branches and
365 // returns.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000366 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000367 (isUncondBranchOpcode(I->getOpcode()) ||
368 isIndirectBranchOpcode(I->getOpcode()) ||
369 isJumpTableBranchOpcode(I->getOpcode()) ||
370 I->isReturn())) {
371 // Forget any previous condition branch information - it no longer applies.
372 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000373 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000374
375 // If we can modify the function, delete everything below this
376 // unconditional branch.
377 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000378 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000379 while (DI != MBB.end()) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000380 MachineInstr &InstToDelete = *DI;
Lang Hames24864fe2013-07-19 23:52:47 +0000381 ++DI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000382 InstToDelete.eraseFromParent();
Lang Hames24864fe2013-07-19 23:52:47 +0000383 }
384 }
385 }
386
387 if (CantAnalyze)
388 return true;
389
Dale Johannesen4244d122010-04-02 01:38:09 +0000390 if (I == MBB.begin())
391 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000392
Dale Johannesen4244d122010-04-02 01:38:09 +0000393 --I;
394 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000395
Lang Hames24864fe2013-07-19 23:52:47 +0000396 // We made it past the terminators without bailing out - we must have
397 // analyzed this branch successfully.
398 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000399}
400
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000401unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000402 int *BytesRemoved) const {
403 assert(!BytesRemoved && "code size not handled");
404
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000405 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
406 if (I == MBB.end())
407 return 0;
408
Evan Cheng056c6692009-07-27 18:20:05 +0000409 if (!isUncondBranchOpcode(I->getOpcode()) &&
410 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000411 return 0;
412
413 // Remove the branch.
414 I->eraseFromParent();
415
416 I = MBB.end();
417
418 if (I == MBB.begin()) return 1;
419 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000420 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000421 return 1;
422
423 // Remove the branch.
424 I->eraseFromParent();
425 return 2;
426}
427
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000428unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000429 MachineBasicBlock *TBB,
430 MachineBasicBlock *FBB,
431 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000432 const DebugLoc &DL,
433 int *BytesAdded) const {
434 assert(!BytesAdded && "code size not handled");
Evan Cheng780748d2009-07-28 05:48:47 +0000435 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
436 int BOpc = !AFI->isThumbFunction()
437 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
438 int BccOpc = !AFI->isThumbFunction()
439 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000440 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000441
David Goodwinaf7451b2009-07-08 16:09:28 +0000442 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000443 assert(TBB && "insertBranch must not be told to insert a fallthrough");
David Goodwinaf7451b2009-07-08 16:09:28 +0000444 assert((Cond.size() == 2 || Cond.size() == 0) &&
445 "ARM branch conditions have two components!");
446
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000447 // For conditional branches, we use addOperand to preserve CPSR flags.
448
Craig Topper062a2ba2014-04-25 05:30:21 +0000449 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000450 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000451 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000452 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000453 else
454 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000455 } else
Diana Picus116bbab2017-01-13 09:58:52 +0000456 BuildMI(&MBB, DL, get(BccOpc))
457 .addMBB(TBB)
458 .addImm(Cond[0].getImm())
459 .add(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000460 return 1;
461 }
462
463 // Two-way conditional branch.
Diana Picus116bbab2017-01-13 09:58:52 +0000464 BuildMI(&MBB, DL, get(BccOpc))
465 .addMBB(TBB)
466 .addImm(Cond[0].getImm())
467 .add(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000468 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000469 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000470 else
471 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000472 return 2;
473}
474
475bool ARMBaseInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000476reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000477 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
478 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
479 return false;
480}
481
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000482bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
483 if (MI.isBundle()) {
484 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
485 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000486 while (++I != E && I->isInsideBundle()) {
487 int PIdx = I->findFirstPredOperandIdx();
488 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
489 return true;
490 }
491 return false;
492 }
493
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000494 int PIdx = MI.findFirstPredOperandIdx();
495 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000496}
497
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000498bool ARMBaseInstrInfo::PredicateInstruction(
499 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
500 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000501 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000502 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
503 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000504 .addImm(Pred[0].getImm())
505 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000506 return true;
507 }
508
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000509 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000510 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000511 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000512 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000513 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000514 return true;
515 }
516 return false;
517}
518
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000519bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
520 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000521 if (Pred1.size() > 2 || Pred2.size() > 2)
522 return false;
523
524 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
525 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
526 if (CC1 == CC2)
527 return true;
528
529 switch (CC1) {
530 default:
531 return false;
532 case ARMCC::AL:
533 return true;
534 case ARMCC::HS:
535 return CC2 == ARMCC::HI;
536 case ARMCC::LS:
537 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
538 case ARMCC::GE:
539 return CC2 == ARMCC::GT;
540 case ARMCC::LE:
541 return CC2 == ARMCC::LT;
542 }
543}
544
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000545bool ARMBaseInstrInfo::DefinesPredicate(
546 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000547 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000548 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
549 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000550 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
551 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000552 Pred.push_back(MO);
553 Found = true;
554 }
555 }
556
557 return Found;
558}
559
Javed Absar4ae7e8122017-06-02 08:53:19 +0000560bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
561 for (const auto &MO : MI.operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000562 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000563 return true;
564 return false;
565}
566
Javed Absar4ae7e8122017-06-02 08:53:19 +0000567bool ARMBaseInstrInfo::isAddrMode3OpImm(const MachineInstr &MI,
568 unsigned Op) const {
569 const MachineOperand &Offset = MI.getOperand(Op + 1);
570 return Offset.getReg() != 0;
571}
572
573// Load with negative register offset requires additional 1cyc and +I unit
574// for Cortex A57
575bool ARMBaseInstrInfo::isAddrMode3OpMinusReg(const MachineInstr &MI,
576 unsigned Op) const {
577 const MachineOperand &Offset = MI.getOperand(Op + 1);
578 const MachineOperand &Opc = MI.getOperand(Op + 2);
579 assert(Opc.isImm());
580 assert(Offset.isReg());
581 int64_t OpcImm = Opc.getImm();
582
583 bool isSub = ARM_AM::getAM3Op(OpcImm) == ARM_AM::sub;
584 return (isSub && Offset.getReg() != 0);
585}
586
587bool ARMBaseInstrInfo::isLdstScaledReg(const MachineInstr &MI,
588 unsigned Op) const {
589 const MachineOperand &Opc = MI.getOperand(Op + 2);
590 unsigned OffImm = Opc.getImm();
591 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
592}
593
594// Load, scaled register offset, not plus LSL2
595bool ARMBaseInstrInfo::isLdstScaledRegNotPlusLsl2(const MachineInstr &MI,
596 unsigned Op) const {
597 const MachineOperand &Opc = MI.getOperand(Op + 2);
598 unsigned OffImm = Opc.getImm();
599
600 bool isAdd = ARM_AM::getAM2Op(OffImm) == ARM_AM::add;
601 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
602 ARM_AM::ShiftOpc ShiftOpc = ARM_AM::getAM2ShiftOpc(OffImm);
603 if (ShiftOpc == ARM_AM::no_shift) return false; // not scaled
604 bool SimpleScaled = (isAdd && ShiftOpc == ARM_AM::lsl && Amt == 2);
605 return !SimpleScaled;
606}
607
608// Minus reg for ldstso addr mode
609bool ARMBaseInstrInfo::isLdstSoMinusReg(const MachineInstr &MI,
610 unsigned Op) const {
611 unsigned OffImm = MI.getOperand(Op + 2).getImm();
612 return ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
613}
614
615// Load, scaled register offset
616bool ARMBaseInstrInfo::isAm2ScaledReg(const MachineInstr &MI,
617 unsigned Op) const {
618 unsigned OffImm = MI.getOperand(Op + 2).getImm();
619 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
620}
621
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000622static bool isEligibleForITBlock(const MachineInstr *MI) {
623 switch (MI->getOpcode()) {
624 default: return true;
625 case ARM::tADC: // ADC (register) T1
626 case ARM::tADDi3: // ADD (immediate) T1
627 case ARM::tADDi8: // ADD (immediate) T2
628 case ARM::tADDrr: // ADD (register) T1
629 case ARM::tAND: // AND (register) T1
630 case ARM::tASRri: // ASR (immediate) T1
631 case ARM::tASRrr: // ASR (register) T1
632 case ARM::tBIC: // BIC (register) T1
633 case ARM::tEOR: // EOR (register) T1
634 case ARM::tLSLri: // LSL (immediate) T1
635 case ARM::tLSLrr: // LSL (register) T1
636 case ARM::tLSRri: // LSR (immediate) T1
637 case ARM::tLSRrr: // LSR (register) T1
638 case ARM::tMUL: // MUL T1
639 case ARM::tMVN: // MVN (register) T1
640 case ARM::tORR: // ORR (register) T1
641 case ARM::tROR: // ROR (register) T1
642 case ARM::tRSB: // RSB (immediate) T1
643 case ARM::tSBC: // SBC (register) T1
644 case ARM::tSUBi3: // SUB (immediate) T1
645 case ARM::tSUBi8: // SUB (immediate) T2
646 case ARM::tSUBrr: // SUB (register) T1
Javed Absar4ae7e8122017-06-02 08:53:19 +0000647 return !ARMBaseInstrInfo::isCPSRDefined(*MI);
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000648 }
649}
650
Evan Chenga33fc862009-11-21 06:21:52 +0000651/// isPredicable - Return true if the specified instruction can be predicated.
652/// By default, this returns true for every instruction with a
653/// PredicateOperand.
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000654bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000655 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000656 return false;
657
Saleem Abdulrasoolbfa25bd2016-09-06 04:00:12 +0000658 if (MI.isBundle())
659 return false;
660
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000661 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000662 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000663
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000664 const ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000665 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000666
Kristof Beyls96652492017-06-22 12:11:38 +0000667 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
668 // In their ARM encoding, they can't be encoded in a conditional form.
669 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
670 return false;
671
Joey Goulya5153cb2013-09-09 14:21:49 +0000672 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000673 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000674 return isV8EligibleForIT(&MI);
Evan Chenga33fc862009-11-21 06:21:52 +0000675 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000676
Evan Chenga33fc862009-11-21 06:21:52 +0000677 return true;
678}
David Goodwinaf7451b2009-07-08 16:09:28 +0000679
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000680namespace llvm {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000681
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000682template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000683 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
684 const MachineOperand &MO = MI->getOperand(i);
685 if (!MO.isReg() || MO.isUndef() || MO.isUse())
686 continue;
687 if (MO.getReg() != ARM::CPSR)
688 continue;
689 if (!MO.isDead())
690 return false;
691 }
692 // all definitions of CPSR are dead
693 return true;
694}
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000695
696} // end namespace llvm
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000697
David Goodwinaf7451b2009-07-08 16:09:28 +0000698/// GetInstSize - Return the size of the specified MachineInstr.
699///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000700unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000701 const MachineBasicBlock &MBB = *MI.getParent();
David Goodwinaf7451b2009-07-08 16:09:28 +0000702 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000703 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000704
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000705 const MCInstrDesc &MCID = MI.getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000706 if (MCID.getSize())
707 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000708
David Blaikie46a9f012012-01-20 21:51:11 +0000709 // If this machine instr is an inline asm, measure it.
Peter Smith6f36cd42018-10-08 09:38:28 +0000710 if (MI.getOpcode() == ARM::INLINEASM) {
711 unsigned Size = getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
712 if (!MF->getInfo<ARMFunctionInfo>()->isThumbFunction())
713 Size = alignTo(Size, 4);
714 return Size;
715 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000716 unsigned Opc = MI.getOpcode();
David Blaikie46a9f012012-01-20 21:51:11 +0000717 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000718 default:
719 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000720 return 0;
721 case TargetOpcode::BUNDLE:
722 return getInstBundleLength(MI);
723 case ARM::MOVi16_ga_pcrel:
724 case ARM::MOVTi16_ga_pcrel:
725 case ARM::t2MOVi16_ga_pcrel:
726 case ARM::t2MOVTi16_ga_pcrel:
727 return 4;
728 case ARM::MOVi32imm:
729 case ARM::t2MOVi32imm:
730 return 8;
731 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000732 case ARM::JUMPTABLE_INSTS:
733 case ARM::JUMPTABLE_ADDRS:
734 case ARM::JUMPTABLE_TBB:
735 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000736 // If this machine instr is a constant pool entry, its size is recorded as
737 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000738 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000739 case ARM::Int_eh_sjlj_longjmp:
740 return 16;
741 case ARM::tInt_eh_sjlj_longjmp:
742 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000743 case ARM::tInt_WIN_eh_sjlj_longjmp:
744 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000745 case ARM::Int_eh_sjlj_setjmp:
746 case ARM::Int_eh_sjlj_setjmp_nofp:
747 return 20;
748 case ARM::tInt_eh_sjlj_setjmp:
749 case ARM::t2Int_eh_sjlj_setjmp:
750 case ARM::t2Int_eh_sjlj_setjmp_nofp:
751 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000752 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000753 return MI.getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000754 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000755}
756
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000757unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000758 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000759 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
760 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000761 while (++I != E && I->isInsideBundle()) {
762 assert(!I->isBundle() && "No nested bundle!");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000763 Size += getInstSizeInBytes(*I);
Evan Cheng7fae11b2011-12-14 02:11:42 +0000764 }
765 return Size;
766}
767
Tim Northover5d72c5d2014-10-01 19:21:03 +0000768void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
769 MachineBasicBlock::iterator I,
770 unsigned DestReg, bool KillSrc,
771 const ARMSubtarget &Subtarget) const {
772 unsigned Opc = Subtarget.isThumb()
773 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
774 : ARM::MRS;
775
776 MachineInstrBuilder MIB =
777 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
778
779 // There is only 1 A/R class MRS instruction, and it always refers to
780 // APSR. However, there are lots of other possibilities on M-class cores.
781 if (Subtarget.isMClass())
782 MIB.addImm(0x800);
783
Diana Picus4f8c3e12017-01-13 09:37:56 +0000784 MIB.add(predOps(ARMCC::AL))
785 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
Tim Northover5d72c5d2014-10-01 19:21:03 +0000786}
787
788void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
789 MachineBasicBlock::iterator I,
790 unsigned SrcReg, bool KillSrc,
791 const ARMSubtarget &Subtarget) const {
792 unsigned Opc = Subtarget.isThumb()
793 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
794 : ARM::MSR;
795
796 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
797
798 if (Subtarget.isMClass())
799 MIB.addImm(0x800);
800 else
801 MIB.addImm(8);
802
Diana Picus4f8c3e12017-01-13 09:37:56 +0000803 MIB.addReg(SrcReg, getKillRegState(KillSrc))
804 .add(predOps(ARMCC::AL))
805 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
Tim Northover5d72c5d2014-10-01 19:21:03 +0000806}
807
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000808void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000809 MachineBasicBlock::iterator I,
810 const DebugLoc &DL, unsigned DestReg,
811 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000812 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000813 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000814
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000815 if (GPRDest && GPRSrc) {
Diana Picus8a73f552017-01-13 10:18:01 +0000816 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
817 .addReg(SrcReg, getKillRegState(KillSrc))
818 .add(predOps(ARMCC::AL))
819 .add(condCodeOp());
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000820 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000821 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000822
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000823 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000824 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000825
Chad Rosierbe762512011-08-20 00:17:25 +0000826 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000827 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000828 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000829 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000830 Opc = ARM::VMOVRS;
831 else if (SPRDest && GPRSrc)
832 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000833 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000834 Opc = ARM::VMOVD;
835 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000836 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000837
Chad Rosierbe762512011-08-20 00:17:25 +0000838 if (Opc) {
839 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000840 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000841 if (Opc == ARM::VORRq)
842 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Diana Picus4f8c3e12017-01-13 09:37:56 +0000843 MIB.add(predOps(ARMCC::AL));
Chad Rosierbe762512011-08-20 00:17:25 +0000844 return;
845 }
846
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000847 // Handle register classes that require multiple instructions.
848 unsigned BeginIdx = 0;
849 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000850 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000851
852 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000853 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
854 Opc = ARM::VORRq;
855 BeginIdx = ARM::qsub_0;
856 SubRegs = 2;
857 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
858 Opc = ARM::VORRq;
859 BeginIdx = ARM::qsub_0;
860 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000861 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000862 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
863 Opc = ARM::VMOVD;
864 BeginIdx = ARM::dsub_0;
865 SubRegs = 2;
866 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
867 Opc = ARM::VMOVD;
868 BeginIdx = ARM::dsub_0;
869 SubRegs = 3;
870 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
871 Opc = ARM::VMOVD;
872 BeginIdx = ARM::dsub_0;
873 SubRegs = 4;
874 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000875 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000876 BeginIdx = ARM::gsub_0;
877 SubRegs = 2;
878 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
879 Opc = ARM::VMOVD;
880 BeginIdx = ARM::dsub_0;
881 SubRegs = 2;
882 Spacing = 2;
883 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
884 Opc = ARM::VMOVD;
885 BeginIdx = ARM::dsub_0;
886 SubRegs = 3;
887 Spacing = 2;
888 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
889 Opc = ARM::VMOVD;
890 BeginIdx = ARM::dsub_0;
891 SubRegs = 4;
892 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000893 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
894 Opc = ARM::VMOVS;
895 BeginIdx = ARM::ssub_0;
896 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000897 } else if (SrcReg == ARM::CPSR) {
898 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
899 return;
900 } else if (DestReg == ARM::CPSR) {
901 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
902 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000903 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000904
Andrew Trickb57e2252012-08-29 04:41:37 +0000905 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000906
Andrew Trick4cc69492012-08-29 01:58:52 +0000907 const TargetRegisterInfo *TRI = &getRegisterInfo();
908 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000909
910 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
911 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000912 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000913 Spacing = -Spacing;
914 }
915#ifndef NDEBUG
916 SmallSet<unsigned, 4> DstRegs;
917#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000918 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000919 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
920 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000921 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000922#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000923 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000924 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000925#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000926 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000927 // VORR takes two source operands.
928 if (Opc == ARM::VORRq)
929 Mov.addReg(Src);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000930 Mov = Mov.add(predOps(ARMCC::AL));
JF Bastien583db652013-07-12 23:33:03 +0000931 // MOVr can set CC.
932 if (Opc == ARM::MOVr)
Diana Picus8a73f552017-01-13 10:18:01 +0000933 Mov = Mov.add(condCodeOp());
Andrew Trick4cc69492012-08-29 01:58:52 +0000934 }
935 // Add implicit super-register defs and kills to the last instruction.
936 Mov->addRegisterDefined(DestReg, TRI);
937 if (KillSrc)
938 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000939}
940
Alexander Ivchenkoaf961122018-08-30 14:32:47 +0000941bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
942 const MachineOperand *&Src,
943 const MachineOperand *&Dest) const {
Petar Jovanovicc0510002018-05-23 15:28:28 +0000944 // VMOVRRD is also a copy instruction but it requires
945 // special way of handling. It is more complex copy version
946 // and since that we are not considering it. For recognition
947 // of such instruction isExtractSubregLike MI interface fuction
948 // could be used.
949 // VORRq is considered as a move only if two inputs are
950 // the same register.
951 if (!MI.isMoveReg() ||
952 (MI.getOpcode() == ARM::VORRq &&
953 MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
954 return false;
Petar Jovanovic8cb6a522018-06-06 16:36:30 +0000955 Dest = &MI.getOperand(0);
956 Src = &MI.getOperand(1);
Petar Jovanovicc0510002018-05-23 15:28:28 +0000957 return true;
958}
959
Tim Northover798697d2013-04-21 11:57:07 +0000960const MachineInstrBuilder &
961ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
962 unsigned SubIdx, unsigned State,
963 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000964 if (!SubIdx)
965 return MIB.addReg(Reg, State);
966
967 if (TargetRegisterInfo::isPhysicalRegister(Reg))
968 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
969 return MIB.addReg(Reg, State, SubIdx);
970}
971
David Goodwinaf7451b2009-07-08 16:09:28 +0000972void ARMBaseInstrInfo::
973storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
974 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000975 const TargetRegisterClass *RC,
976 const TargetRegisterInfo *TRI) const {
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000977 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000978 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000979 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000980
Alex Lorenze40c8a22015-08-11 23:09:45 +0000981 MachineMemOperand *MMO = MF.getMachineMemOperand(
982 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
983 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000984
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000985 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000986 case 2:
987 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +0000988 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRH))
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +0000989 .addReg(SrcReg, getKillRegState(isKill))
990 .addFrameIndex(FI)
991 .addImm(0)
992 .addMemOperand(MMO)
993 .add(predOps(ARMCC::AL));
994 } else
995 llvm_unreachable("Unknown reg class!");
996 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000997 case 4:
998 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +0000999 BuildMI(MBB, I, DebugLoc(), get(ARM::STRi12))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001000 .addReg(SrcReg, getKillRegState(isKill))
1001 .addFrameIndex(FI)
1002 .addImm(0)
1003 .addMemOperand(MMO)
1004 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001005 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001006 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRS))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001007 .addReg(SrcReg, getKillRegState(isKill))
1008 .addFrameIndex(FI)
1009 .addImm(0)
1010 .addMemOperand(MMO)
1011 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001012 } else
1013 llvm_unreachable("Unknown reg class!");
1014 break;
1015 case 8:
1016 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001017 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRD))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001018 .addReg(SrcReg, getKillRegState(isKill))
1019 .addFrameIndex(FI)
1020 .addImm(0)
1021 .addMemOperand(MMO)
1022 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001023 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001024 if (Subtarget.hasV5TEOps()) {
Matthias Braun81578e92018-10-05 22:00:13 +00001025 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STRD));
Tim Northover798697d2013-04-21 11:57:07 +00001026 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1027 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001028 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1029 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001030 } else {
1031 // Fallback to STM instruction, which has existed since the dawn of
1032 // time.
Matthias Braun81578e92018-10-05 22:00:13 +00001033 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STMIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001034 .addFrameIndex(FI)
1035 .addMemOperand(MMO)
1036 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001037 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1038 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1039 }
Owen Anderson732f82c2011-08-10 17:21:20 +00001040 } else
1041 llvm_unreachable("Unknown reg class!");
1042 break;
1043 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001044 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001045 // Use aligned spills if the stack can be realigned.
1046 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001047 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1q64))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001048 .addFrameIndex(FI)
1049 .addImm(16)
1050 .addReg(SrcReg, getKillRegState(isKill))
1051 .addMemOperand(MMO)
1052 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001053 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001054 BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMQIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001055 .addReg(SrcReg, getKillRegState(isKill))
1056 .addFrameIndex(FI)
1057 .addMemOperand(MMO)
1058 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001059 }
1060 } else
1061 llvm_unreachable("Unknown reg class!");
1062 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001063 case 24:
1064 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1065 // Use aligned spills if the stack can be realigned.
1066 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001067 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64TPseudo))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001068 .addFrameIndex(FI)
1069 .addImm(16)
1070 .addReg(SrcReg, getKillRegState(isKill))
1071 .addMemOperand(MMO)
1072 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001073 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001074 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1075 get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001076 .addFrameIndex(FI)
1077 .add(predOps(ARMCC::AL))
1078 .addMemOperand(MMO);
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001079 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1080 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1081 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1082 }
1083 } else
1084 llvm_unreachable("Unknown reg class!");
1085 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001086 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001087 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001088 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1089 // FIXME: It's possible to only store part of the QQ register if the
1090 // spilled def has a sub-register index.
Matthias Braun81578e92018-10-05 22:00:13 +00001091 BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64QPseudo))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001092 .addFrameIndex(FI)
1093 .addImm(16)
1094 .addReg(SrcReg, getKillRegState(isKill))
1095 .addMemOperand(MMO)
1096 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001097 } else {
Matthias Braun81578e92018-10-05 22:00:13 +00001098 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
1099 get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001100 .addFrameIndex(FI)
1101 .add(predOps(ARMCC::AL))
1102 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001103 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1104 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1105 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1106 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1107 }
1108 } else
1109 llvm_unreachable("Unknown reg class!");
1110 break;
1111 case 64:
1112 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Matthias Braun81578e92018-10-05 22:00:13 +00001113 MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMDIA))
Diana Picus4f8c3e12017-01-13 09:37:56 +00001114 .addFrameIndex(FI)
1115 .add(predOps(ARMCC::AL))
1116 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001117 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1118 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1119 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1120 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1121 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1122 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1123 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1124 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1125 } else
1126 llvm_unreachable("Unknown reg class!");
1127 break;
1128 default:
1129 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001130 }
1131}
1132
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001133unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1134 int &FrameIndex) const {
1135 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001136 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001137 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001138 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001139 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1140 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1141 MI.getOperand(3).getImm() == 0) {
1142 FrameIndex = MI.getOperand(1).getIndex();
1143 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001144 }
1145 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001146 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001147 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001148 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001149 case ARM::VSTRD:
1150 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1152 MI.getOperand(2).getImm() == 0) {
1153 FrameIndex = MI.getOperand(1).getIndex();
1154 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001155 }
1156 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001157 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001158 case ARM::VST1d64TPseudo:
1159 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001160 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1161 FrameIndex = MI.getOperand(0).getIndex();
1162 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001163 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001164 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001165 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1167 FrameIndex = MI.getOperand(1).getIndex();
1168 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001169 }
1170 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001171 }
1172
1173 return 0;
1174}
1175
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001176unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001177 int &FrameIndex) const {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001178 SmallVector<const MachineMemOperand *, 1> Accesses;
Eli Friedman1e5d5692019-03-25 22:42:30 +00001179 if (MI.mayStore() && hasStoreToStackSlot(MI, Accesses) &&
1180 Accesses.size() == 1) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001181 FrameIndex =
1182 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1183 ->getFrameIndex();
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001184 return true;
1185 }
1186 return false;
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001187}
1188
David Goodwinaf7451b2009-07-08 16:09:28 +00001189void ARMBaseInstrInfo::
1190loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1191 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001192 const TargetRegisterClass *RC,
1193 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001194 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001195 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001196 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001197 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001198 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001199 MachineMemOperand *MMO = MF.getMachineMemOperand(
1200 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1201 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001202
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001203 switch (TRI->getSpillSize(*RC)) {
Sjoerd Meijer3b4294ed2018-02-14 15:09:09 +00001204 case 2:
1205 if (ARM::HPRRegClass.hasSubClassEq(RC)) {
1206 BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
1207 .addFrameIndex(FI)
1208 .addImm(0)
1209 .addMemOperand(MMO)
1210 .add(predOps(ARMCC::AL));
1211 } else
1212 llvm_unreachable("Unknown reg class!");
1213 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001214 case 4:
1215 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001216 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1217 .addFrameIndex(FI)
1218 .addImm(0)
1219 .addMemOperand(MMO)
1220 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001221 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001222 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1223 .addFrameIndex(FI)
1224 .addImm(0)
1225 .addMemOperand(MMO)
1226 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001227 } else
1228 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001229 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001230 case 8:
1231 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001232 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1233 .addFrameIndex(FI)
1234 .addImm(0)
1235 .addMemOperand(MMO)
1236 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001237 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001238 MachineInstrBuilder MIB;
1239
1240 if (Subtarget.hasV5TEOps()) {
1241 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1242 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1243 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001244 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1245 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001246 } else {
1247 // Fallback to LDM instruction, which has existed since the dawn of
1248 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001249 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1250 .addFrameIndex(FI)
1251 .addMemOperand(MMO)
1252 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001253 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1254 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1255 }
1256
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001257 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1258 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001259 } else
1260 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001261 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001262 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001263 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001264 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001265 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1266 .addFrameIndex(FI)
1267 .addImm(16)
1268 .addMemOperand(MMO)
1269 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001270 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001271 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1272 .addFrameIndex(FI)
1273 .addMemOperand(MMO)
1274 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001275 }
1276 } else
1277 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001278 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001279 case 24:
1280 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1281 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001282 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1283 .addFrameIndex(FI)
1284 .addImm(16)
1285 .addMemOperand(MMO)
1286 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001287 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001288 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1289 .addFrameIndex(FI)
1290 .addMemOperand(MMO)
1291 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001292 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1293 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1294 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1295 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1296 MIB.addReg(DestReg, RegState::ImplicitDefine);
1297 }
1298 } else
1299 llvm_unreachable("Unknown reg class!");
1300 break;
1301 case 32:
1302 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001303 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001304 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1305 .addFrameIndex(FI)
1306 .addImm(16)
1307 .addMemOperand(MMO)
1308 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001309 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001310 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1311 .addFrameIndex(FI)
1312 .add(predOps(ARMCC::AL))
1313 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001314 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1315 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1316 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1317 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001318 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1319 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001320 }
1321 } else
1322 llvm_unreachable("Unknown reg class!");
1323 break;
1324 case 64:
1325 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001326 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1327 .addFrameIndex(FI)
1328 .add(predOps(ARMCC::AL))
1329 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001330 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1331 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1332 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1333 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1334 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1335 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1336 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1337 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001338 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1339 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001340 } else
1341 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001342 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001343 default:
1344 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001345 }
1346}
1347
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001348unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1349 int &FrameIndex) const {
1350 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001351 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001352 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001353 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001354 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1355 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1356 MI.getOperand(3).getImm() == 0) {
1357 FrameIndex = MI.getOperand(1).getIndex();
1358 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001359 }
1360 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001361 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001362 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001363 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001364 case ARM::VLDRD:
1365 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001366 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1367 MI.getOperand(2).getImm() == 0) {
1368 FrameIndex = MI.getOperand(1).getIndex();
1369 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001370 }
1371 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001372 case ARM::VLD1q64:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001373 case ARM::VLD1d8TPseudo:
1374 case ARM::VLD1d16TPseudo:
1375 case ARM::VLD1d32TPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001376 case ARM::VLD1d64TPseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00001377 case ARM::VLD1d8QPseudo:
1378 case ARM::VLD1d16QPseudo:
1379 case ARM::VLD1d32QPseudo:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001380 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001381 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1382 FrameIndex = MI.getOperand(1).getIndex();
1383 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001384 }
1385 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001386 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001387 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1388 FrameIndex = MI.getOperand(1).getIndex();
1389 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001390 }
1391 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001392 }
1393
1394 return 0;
1395}
1396
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001397unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1398 int &FrameIndex) const {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001399 SmallVector<const MachineMemOperand *, 1> Accesses;
Eli Friedman1e5d5692019-03-25 22:42:30 +00001400 if (MI.mayLoad() && hasLoadFromStackSlot(MI, Accesses) &&
1401 Accesses.size() == 1) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +00001402 FrameIndex =
1403 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
1404 ->getFrameIndex();
Sander de Smalen6cab60f2018-09-03 09:15:58 +00001405 return true;
1406 }
1407 return false;
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001408}
1409
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001410/// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
Scott Douglass953f9082015-10-05 14:49:54 +00001411/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001412void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001413 bool isThumb1 = Subtarget.isThumb1Only();
1414 bool isThumb2 = Subtarget.isThumb2();
1415 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1416
Scott Douglass953f9082015-10-05 14:49:54 +00001417 DebugLoc dl = MI->getDebugLoc();
1418 MachineBasicBlock *BB = MI->getParent();
1419
1420 MachineInstrBuilder LDM, STM;
1421 if (isThumb1 || !MI->getOperand(1).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001422 MachineOperand LDWb(MI->getOperand(1));
Scott Douglass953f9082015-10-05 14:49:54 +00001423 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1424 : isThumb1 ? ARM::tLDMIA_UPD
1425 : ARM::LDMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001426 .add(LDWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001427 } else {
1428 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1429 }
1430
1431 if (isThumb1 || !MI->getOperand(0).isDead()) {
Geoff Berry60c43102017-12-12 17:53:59 +00001432 MachineOperand STWb(MI->getOperand(0));
Scott Douglass953f9082015-10-05 14:49:54 +00001433 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1434 : isThumb1 ? ARM::tSTMIA_UPD
1435 : ARM::STMIA_UPD))
Geoff Berry60c43102017-12-12 17:53:59 +00001436 .add(STWb);
Scott Douglass953f9082015-10-05 14:49:54 +00001437 } else {
1438 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1439 }
1440
Geoff Berry60c43102017-12-12 17:53:59 +00001441 MachineOperand LDBase(MI->getOperand(3));
Geoff Berry60c43102017-12-12 17:53:59 +00001442 LDM.add(LDBase).add(predOps(ARMCC::AL));
1443
1444 MachineOperand STBase(MI->getOperand(2));
Geoff Berry60c43102017-12-12 17:53:59 +00001445 STM.add(STBase).add(predOps(ARMCC::AL));
Scott Douglass953f9082015-10-05 14:49:54 +00001446
1447 // Sort the scratch registers into ascending order.
1448 const TargetRegisterInfo &TRI = getRegisterInfo();
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001449 SmallVector<unsigned, 6> ScratchRegs;
Scott Douglass953f9082015-10-05 14:49:54 +00001450 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1451 ScratchRegs.push_back(MI->getOperand(I).getReg());
Fangrui Song0cac7262018-09-27 02:13:45 +00001452 llvm::sort(ScratchRegs,
1453 [&TRI](const unsigned &Reg1, const unsigned &Reg2) -> bool {
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001454 return TRI.getEncodingValue(Reg1) <
1455 TRI.getEncodingValue(Reg2);
1456 });
Scott Douglass953f9082015-10-05 14:49:54 +00001457
1458 for (const auto &Reg : ScratchRegs) {
1459 LDM.addReg(Reg, RegState::Define);
1460 STM.addReg(Reg, RegState::Kill);
1461 }
1462
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001463 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001464}
1465
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001466bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1467 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001468 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001469 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001470 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001471 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001472 return true;
1473 }
1474
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001475 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001476 expandMEMCPY(MI);
1477 return true;
1478 }
1479
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001480 // This hook gets to expand COPY instructions before they become
1481 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1482 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1483 // changed into a VORR that can go down the NEON pipeline.
Diana Picusb772e402016-07-06 11:22:11 +00001484 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001485 return false;
1486
1487 // Look for a copy between even S-registers. That is where we keep floats
1488 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001489 unsigned DstRegS = MI.getOperand(0).getReg();
1490 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001491 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1492 return false;
1493
1494 const TargetRegisterInfo *TRI = &getRegisterInfo();
1495 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1496 &ARM::DPRRegClass);
1497 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1498 &ARM::DPRRegClass);
1499 if (!DstRegD || !SrcRegD)
1500 return false;
1501
1502 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1503 // legal if the COPY already defines the full DstRegD, and it isn't a
1504 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001505 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001506 return false;
1507
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001508 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001509 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001510 return false;
1511
1512 // All clear, widen the COPY.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001513 LLVM_DEBUG(dbgs() << "widening: " << MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001514 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001515
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001516 // 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 +00001517 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001518 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001519 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001520 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001521
1522 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001523 MI.setDesc(get(ARM::VMOVD));
1524 MI.getOperand(0).setReg(DstRegD);
1525 MI.getOperand(1).setReg(SrcRegD);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001526 MIB.add(predOps(ARMCC::AL));
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001527
1528 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1529 // register scavenger and machine verifier, so we need to indicate that we
1530 // are reading an undefined value from SrcRegD, but a proper value from
1531 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001532 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001533 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001534
1535 // SrcRegD may actually contain an unrelated value in the ssub_1
1536 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001537 if (MI.getOperand(1).isKill()) {
1538 MI.getOperand(1).setIsKill(false);
1539 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001540 }
1541
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001542 LLVM_DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001543 return true;
1544}
1545
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001546/// Create a copy of a const pool value. Update CPI to the new index and return
1547/// the label UID.
1548static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1549 MachineConstantPool *MCP = MF.getConstantPool();
1550 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1551
1552 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1553 assert(MCPE.isMachineConstantPoolEntry() &&
1554 "Expecting a machine constantpool entry!");
1555 ARMConstantPoolValue *ACPV =
1556 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1557
Evan Chengdfce83c2011-01-17 08:03:18 +00001558 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001559 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001560
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001561 // FIXME: The below assumes PIC relocation model and that the function
1562 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1563 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1564 // instructions, so that's probably OK, but is PIC always correct when
1565 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001566 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001567 NewCPV = ARMConstantPoolConstant::Create(
1568 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1569 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001570 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001571 NewCPV = ARMConstantPoolSymbol::
Matthias Braunf1caa282017-12-15 22:22:58 +00001572 Create(MF.getFunction().getContext(),
Bill Wendlingc214cb02011-10-01 08:58:29 +00001573 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001574 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001575 NewCPV = ARMConstantPoolConstant::
1576 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1577 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001578 else if (ACPV->isLSDA())
Matthias Braunf1caa282017-12-15 22:22:58 +00001579 NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
Bill Wendling7753d662011-10-01 08:00:54 +00001580 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001581 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001582 NewCPV = ARMConstantPoolMBB::
Matthias Braunf1caa282017-12-15 22:22:58 +00001583 Create(MF.getFunction().getContext(),
Bill Wendling4a4772f2011-10-01 09:30:42 +00001584 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001585 else
1586 llvm_unreachable("Unexpected ARM constantpool value type!!");
1587 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1588 return PCLabelId;
1589}
1590
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001591void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1592 MachineBasicBlock::iterator I,
1593 unsigned DestReg, unsigned SubIdx,
1594 const MachineInstr &Orig,
1595 const TargetRegisterInfo &TRI) const {
1596 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001597 switch (Opcode) {
1598 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001599 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1600 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001601 MBB.insert(I, MI);
1602 break;
1603 }
1604 case ARM::tLDRpci_pic:
1605 case ARM::t2LDRpci_pic: {
1606 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001607 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001608 unsigned PCLabelId = duplicateCPV(MF, CPI);
Chandler Carruthc73c0302018-08-16 21:30:05 +00001609 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1610 .addConstantPoolIndex(CPI)
1611 .addImm(PCLabelId)
1612 .cloneMemRefs(Orig);
Evan Chengfe864422009-11-08 00:15:23 +00001613 break;
1614 }
1615 }
Evan Chengfe864422009-11-08 00:15:23 +00001616}
1617
Matthias Braun55bc9b32017-08-22 23:56:30 +00001618MachineInstr &
1619ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1620 MachineBasicBlock::iterator InsertBefore,
1621 const MachineInstr &Orig) const {
1622 MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1623 MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1624 for (;;) {
1625 switch (I->getOpcode()) {
1626 case ARM::tLDRpci_pic:
1627 case ARM::t2LDRpci_pic: {
1628 MachineFunction &MF = *MBB.getParent();
1629 unsigned CPI = I->getOperand(1).getIndex();
1630 unsigned PCLabelId = duplicateCPV(MF, CPI);
1631 I->getOperand(1).setIndex(CPI);
1632 I->getOperand(2).setImm(PCLabelId);
1633 break;
1634 }
1635 }
1636 if (!I->isBundledWithSucc())
1637 break;
1638 ++I;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001639 }
Matthias Braun55bc9b32017-08-22 23:56:30 +00001640 return Cloned;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001641}
1642
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001643bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1644 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001645 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001646 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001647 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001648 Opcode == ARM::t2LDRpci_pic ||
1649 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001650 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001651 Opcode == ARM::LDRLIT_ga_pcrel ||
1652 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1653 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001654 Opcode == ARM::MOV_ga_pcrel ||
1655 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001656 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001657 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001658 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001659 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001660 return false;
1661
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001662 const MachineOperand &MO0 = MI0.getOperand(1);
1663 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001664 if (MO0.getOffset() != MO1.getOffset())
1665 return false;
1666
Tim Northover72360d22013-12-02 10:35:41 +00001667 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1668 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1669 Opcode == ARM::tLDRLIT_ga_pcrel ||
1670 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001671 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001672 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001673 // Ignore the PC labels.
1674 return MO0.getGlobal() == MO1.getGlobal();
1675
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001676 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001677 const MachineConstantPool *MCP = MF->getConstantPool();
1678 int CPI0 = MO0.getIndex();
1679 int CPI1 = MO1.getIndex();
1680 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1681 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001682 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1683 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1684 if (isARMCP0 && isARMCP1) {
1685 ARMConstantPoolValue *ACPV0 =
1686 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1687 ARMConstantPoolValue *ACPV1 =
1688 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1689 return ACPV0->hasSameValue(ACPV1);
1690 } else if (!isARMCP0 && !isARMCP1) {
1691 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1692 }
1693 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001694 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001695 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001696 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001697 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001698 return false;
1699
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001700 unsigned Addr0 = MI0.getOperand(1).getReg();
1701 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001702 if (Addr0 != Addr1) {
1703 if (!MRI ||
1704 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1705 !TargetRegisterInfo::isVirtualRegister(Addr1))
1706 return false;
1707
1708 // This assumes SSA form.
1709 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1710 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1711 // Check if the loaded value, e.g. a constantpool of a global address, are
1712 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001713 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001714 return false;
1715 }
1716
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001717 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00001718 // %12 = PICLDR %11, 0, 14, %noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001719 const MachineOperand &MO0 = MI0.getOperand(i);
1720 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001721 if (!MO0.isIdenticalTo(MO1))
1722 return false;
1723 }
1724 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001725 }
1726
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001727 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001728}
1729
Bill Wendlingf4707472010-06-23 23:00:16 +00001730/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1731/// determine if two loads are loading from the same base address. It should
1732/// only return true if the base pointers are the same and the only differences
1733/// between the two addresses is the offset. It also returns the offsets by
1734/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001735///
1736/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1737/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001738bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1739 int64_t &Offset1,
1740 int64_t &Offset2) const {
1741 // Don't worry about Thumb: just ARM and Thumb2.
1742 if (Subtarget.isThumb1Only()) return false;
1743
1744 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1745 return false;
1746
1747 switch (Load1->getMachineOpcode()) {
1748 default:
1749 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001750 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001751 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001752 case ARM::LDRD:
1753 case ARM::LDRH:
1754 case ARM::LDRSB:
1755 case ARM::LDRSH:
1756 case ARM::VLDRD:
1757 case ARM::VLDRS:
1758 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001759 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001760 case ARM::t2LDRDi8:
1761 case ARM::t2LDRSHi8:
1762 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001763 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001764 case ARM::t2LDRSHi12:
1765 break;
1766 }
1767
1768 switch (Load2->getMachineOpcode()) {
1769 default:
1770 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001771 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001772 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001773 case ARM::LDRD:
1774 case ARM::LDRH:
1775 case ARM::LDRSB:
1776 case ARM::LDRSH:
1777 case ARM::VLDRD:
1778 case ARM::VLDRS:
1779 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001780 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001781 case ARM::t2LDRSHi8:
1782 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001783 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001784 case ARM::t2LDRSHi12:
1785 break;
1786 }
1787
1788 // Check if base addresses and chain operands match.
1789 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1790 Load1->getOperand(4) != Load2->getOperand(4))
1791 return false;
1792
1793 // Index should be Reg0.
1794 if (Load1->getOperand(3) != Load2->getOperand(3))
1795 return false;
1796
1797 // Determine the offsets.
1798 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1799 isa<ConstantSDNode>(Load2->getOperand(1))) {
1800 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1801 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1802 return true;
1803 }
1804
1805 return false;
1806}
1807
1808/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001809/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001810/// be scheduled togther. On some targets if two loads are loading from
1811/// addresses in the same cache line, it's better if they are scheduled
1812/// together. This function takes two integers that represent the load offsets
1813/// from the common base address. It returns true if it decides it's desirable
1814/// to schedule the two loads together. "NumLoads" is the number of loads that
1815/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001816///
1817/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1818/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001819bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1820 int64_t Offset1, int64_t Offset2,
1821 unsigned NumLoads) const {
1822 // Don't worry about Thumb: just ARM and Thumb2.
1823 if (Subtarget.isThumb1Only()) return false;
1824
1825 assert(Offset2 > Offset1);
1826
1827 if ((Offset2 - Offset1) / 8 > 64)
1828 return false;
1829
Renato Golinb184cd92013-08-14 16:35:29 +00001830 // Check if the machine opcodes are different. If they are different
1831 // then we consider them to not be of the same base address,
1832 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1833 // In this case, they are considered to be the same because they are different
1834 // encoding forms of the same basic instruction.
1835 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1836 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1837 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1838 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1839 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001840 return false; // FIXME: overly conservative?
1841
1842 // Four loads in a row should be sufficient.
1843 if (NumLoads >= 3)
1844 return false;
1845
1846 return true;
1847}
1848
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001849bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001850 const MachineBasicBlock *MBB,
1851 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001852 // Debug info is never a scheduling boundary. It's necessary to be explicit
1853 // due to the special treatment of IT instructions below, otherwise a
1854 // dbg_value followed by an IT will result in the IT instruction being
1855 // considered a scheduling hazard, which is wrong. It should be the actual
1856 // instruction preceding the dbg_value instruction(s), just like it is
1857 // when debug info is not present.
Shiva Chen801bf7e2018-05-09 02:42:00 +00001858 if (MI.isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001859 return false;
1860
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001861 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001862 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001863 return true;
1864
1865 // Treat the start of the IT block as a scheduling boundary, but schedule
1866 // t2IT along with all instructions following it.
1867 // FIXME: This is a big hammer. But the alternative is to add all potential
1868 // true and anti dependencies to IT block instructions as implicit operands
1869 // to the t2IT instruction. The added compile time and complexity does not
1870 // seem worth it.
1871 MachineBasicBlock::const_iterator I = MI;
Shiva Chen801bf7e2018-05-09 02:42:00 +00001872 // Make sure to skip any debug instructions
1873 while (++I != MBB->end() && I->isDebugInstr())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001874 ;
1875 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001876 return true;
1877
1878 // Don't attempt to schedule around any instruction that defines
1879 // a stack-oriented pointer, as it's unlikely to be profitable. This
1880 // saves compile time, because it doesn't require every single
1881 // stack slot reference to depend on the instruction that does the
1882 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001883 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001884 // No ARM calling conventions change the stack pointer. (X86 calling
1885 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001886 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001887 return true;
1888
1889 return false;
1890}
1891
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001892bool ARMBaseInstrInfo::
1893isProfitableToIfCvt(MachineBasicBlock &MBB,
1894 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001895 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001896 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001897 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001898
Peter Collingbourne65295232015-04-23 20:31:30 +00001899 // If we are optimizing for size, see if the branch in the predecessor can be
1900 // lowered to cbn?z by the constant island lowering pass, and return false if
1901 // so. This results in a shorter instruction sequence.
Evandro Menezes85bd3972019-04-04 22:40:06 +00001902 if (MBB.getParent()->getFunction().hasOptSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001903 MachineBasicBlock *Pred = *MBB.pred_begin();
1904 if (!Pred->empty()) {
1905 MachineInstr *LastMI = &*Pred->rbegin();
1906 if (LastMI->getOpcode() == ARM::t2Bcc) {
David Greenc519d3c2019-04-23 12:11:26 +00001907 const TargetRegisterInfo *TRI = &getRegisterInfo();
1908 MachineInstr *CmpMI = findCMPToFoldIntoCBZ(LastMI, TRI);
1909 if (CmpMI)
1910 return false;
Peter Collingbourne65295232015-04-23 20:31:30 +00001911 }
1912 }
1913 }
Artyom Skrobov283316b2017-03-14 13:38:45 +00001914 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1915 MBB, 0, 0, Probability);
Evan Cheng02b184d2010-06-25 22:42:03 +00001916}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001917
Evan Cheng02b184d2010-06-25 22:42:03 +00001918bool ARMBaseInstrInfo::
John Brawn75d76e52017-06-28 14:11:15 +00001919isProfitableToIfCvt(MachineBasicBlock &TBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001920 unsigned TCycles, unsigned TExtra,
John Brawn75d76e52017-06-28 14:11:15 +00001921 MachineBasicBlock &FBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001922 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001923 BranchProbability Probability) const {
Artyom Skrobov283316b2017-03-14 13:38:45 +00001924 if (!TCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001925 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001926
David Green2f9eed62019-04-23 11:46:58 +00001927 // In thumb code we often end up trading one branch for a IT block, and
1928 // if we are cloning the instruction can increase code size. Prevent
1929 // blocks with multiple predecesors from being ifcvted to prevent this
1930 // cloning.
1931 if (Subtarget.isThumb2() && TBB.getParent()->getFunction().hasMinSize()) {
1932 if (TBB.pred_size() != 1 || FBB.pred_size() != 1)
1933 return false;
1934 }
1935
Owen Anderson88af7d02010-09-28 18:32:13 +00001936 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001937 // Here we scale up each component of UnpredCost to avoid precision issue when
1938 // scaling TCycles/FCycles by Probability.
1939 const unsigned ScalingUpFactor = 1024;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001940
John Brawn75d76e52017-06-28 14:11:15 +00001941 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1942 unsigned UnpredCost;
1943 if (!Subtarget.hasBranchPredictor()) {
1944 // When we don't have a branch predictor it's always cheaper to not take a
1945 // branch than take it, so we have to take that into account.
1946 unsigned NotTakenBranchCost = 1;
1947 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1948 unsigned TUnpredCycles, FUnpredCycles;
1949 if (!FCycles) {
1950 // Triangle: TBB is the fallthrough
1951 TUnpredCycles = TCycles + NotTakenBranchCost;
1952 FUnpredCycles = TakenBranchCost;
1953 } else {
1954 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1955 TUnpredCycles = TCycles + TakenBranchCost;
1956 FUnpredCycles = FCycles + NotTakenBranchCost;
John Brawn97cc2832017-07-12 13:23:10 +00001957 // The branch at the end of FBB will disappear when it's predicated, so
1958 // discount it from PredCost.
1959 PredCost -= 1 * ScalingUpFactor;
John Brawn75d76e52017-06-28 14:11:15 +00001960 }
1961 // The total cost is the cost of each path scaled by their probabilites
1962 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1963 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1964 UnpredCost = TUnpredCost + FUnpredCost;
1965 // When predicating assume that the first IT can be folded away but later
1966 // ones cost one cycle each
1967 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1968 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1969 }
1970 } else {
1971 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1972 unsigned FUnpredCost =
1973 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1974 UnpredCost = TUnpredCost + FUnpredCost;
1975 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1976 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1977 }
1978
1979 return PredCost <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001980}
1981
Bob Wilsone8a549c2012-09-29 21:43:49 +00001982bool
1983ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1984 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001985 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001986 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001987 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001988}
1989
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001990/// getInstrPredicate - If instruction is predicated, returns its predicate
1991/// condition, otherwise returns AL. It also returns the condition code
1992/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001993ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1994 unsigned &PredReg) {
1995 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001996 if (PIdx == -1) {
1997 PredReg = 0;
1998 return ARMCC::AL;
1999 }
2000
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002001 PredReg = MI.getOperand(PIdx+1).getReg();
2002 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00002003}
2004
Matthias Braunfa3872e2015-05-18 20:27:55 +00002005unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00002006 if (Opc == ARM::B)
2007 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00002008 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00002009 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00002010 if (Opc == ARM::t2B)
2011 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00002012
2013 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00002014}
2015
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002016MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002017 bool NewMI,
2018 unsigned OpIdx1,
2019 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002020 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002021 case ARM::MOVCCr:
2022 case ARM::t2MOVCCr: {
2023 // MOVCC can be commuted by inverting the condition.
2024 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002025 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002026 // MOVCC AL can't be inverted. Shouldn't happen.
2027 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00002028 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002029 MachineInstr *CommutedMI =
2030 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
2031 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002032 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002033 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002034 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
2035 .setImm(ARMCC::getOppositeCondition(CC));
2036 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002037 }
2038 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00002039 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00002040}
Evan Cheng780748d2009-07-28 05:48:47 +00002041
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002042/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002043/// return the defining instruction.
2044static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
2045 const MachineRegisterInfo &MRI,
2046 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002047 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002048 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002049 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00002050 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002051 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002052 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002053 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002054 // MI is folded into the MOVCC by predicating it.
2055 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00002056 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002057 // Check if MI has any non-dead defs or physreg uses. This also detects
2058 // predicated instructions which will be reading CPSR.
2059 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
2060 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00002061 // Reject frame index operands, PEI can't handle the predicated pseudos.
2062 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00002063 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002064 if (!MO.isReg())
2065 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002066 // MI can't have any tied operands, that would conflict with predication.
2067 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00002068 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002069 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00002070 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002071 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00002072 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002073 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002074 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00002075 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00002076 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002077 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002078}
2079
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002080bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002081 SmallVectorImpl<MachineOperand> &Cond,
2082 unsigned &TrueOp, unsigned &FalseOp,
2083 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002084 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002085 "Unknown select instruction");
2086 // MOVCC operands:
2087 // 0: Def.
2088 // 1: True use.
2089 // 2: False use.
2090 // 3: Condition code.
2091 // 4: CPSR use.
2092 TrueOp = 1;
2093 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002094 Cond.push_back(MI.getOperand(3));
2095 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002096 // We can always fold a def.
2097 Optimizable = true;
2098 return false;
2099}
2100
Mehdi Amini22e59742015-01-13 07:07:13 +00002101MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002102ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00002103 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2104 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002105 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002106 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002107 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2108 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002109 bool Invert = !DefMI;
2110 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002111 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002112 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002113 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002114
Matthias Braun2f169f92013-10-04 16:52:56 +00002115 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002116 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2117 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00002118 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2119 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00002120 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00002121
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002122 // Create a new predicated version of DefMI.
2123 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002124 MachineInstrBuilder NewMI =
2125 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002126
2127 // Copy all the DefMI operands, excluding its (null) predicate.
2128 const MCInstrDesc &DefDesc = DefMI->getDesc();
2129 for (unsigned i = 1, e = DefDesc.getNumOperands();
2130 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00002131 NewMI.add(DefMI->getOperand(i));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002132
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002133 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002134 if (Invert)
2135 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2136 else
2137 NewMI.addImm(CondCode);
Diana Picus116bbab2017-01-13 09:58:52 +00002138 NewMI.add(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002139
2140 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2141 if (NewMI->hasOptionalDef())
Diana Picus8a73f552017-01-13 10:18:01 +00002142 NewMI.add(condCodeOp());
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002143
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002144 // The output register value when the predicate is false is an implicit
2145 // register operand tied to the first def.
2146 // The tie makes the register allocator ensure the FalseReg is allocated the
2147 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002148 FalseReg.setImplicit();
Diana Picus116bbab2017-01-13 09:58:52 +00002149 NewMI.add(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002150 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2151
Mehdi Amini22e59742015-01-13 07:07:13 +00002152 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2153 SeenMIs.insert(NewMI);
2154 SeenMIs.erase(DefMI);
2155
Pete Cooper2127b002015-04-30 23:57:47 +00002156 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2157 // DefMI would be invalid when tranferred inside the loop. Checking for a
2158 // loop is expensive, but at least remove kill flags if they are in different
2159 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002160 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00002161 NewMI->clearKillInfo();
2162
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002163 // The caller will erase MI, but not DefMI.
2164 DefMI->eraseFromParent();
2165 return NewMI;
2166}
2167
Andrew Trick924123a2011-09-21 02:20:46 +00002168/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2169/// instruction is encoded with an 'S' bit is determined by the optional CPSR
2170/// def operand.
2171///
2172/// This will go away once we can teach tblgen how to set the optional CPSR def
2173/// operand itself.
2174struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00002175 uint16_t PseudoOpc;
2176 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002177};
2178
Craig Topper2fbd1302012-05-24 03:59:11 +00002179static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00002180 {ARM::ADDSri, ARM::ADDri},
2181 {ARM::ADDSrr, ARM::ADDrr},
2182 {ARM::ADDSrsi, ARM::ADDrsi},
2183 {ARM::ADDSrsr, ARM::ADDrsr},
2184
2185 {ARM::SUBSri, ARM::SUBri},
2186 {ARM::SUBSrr, ARM::SUBrr},
2187 {ARM::SUBSrsi, ARM::SUBrsi},
2188 {ARM::SUBSrsr, ARM::SUBrsr},
2189
2190 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00002191 {ARM::RSBSrsi, ARM::RSBrsi},
2192 {ARM::RSBSrsr, ARM::RSBrsr},
2193
Artyom Skrobov92c06532017-03-22 23:35:51 +00002194 {ARM::tADDSi3, ARM::tADDi3},
2195 {ARM::tADDSi8, ARM::tADDi8},
2196 {ARM::tADDSrr, ARM::tADDrr},
2197 {ARM::tADCS, ARM::tADC},
2198
2199 {ARM::tSUBSi3, ARM::tSUBi3},
2200 {ARM::tSUBSi8, ARM::tSUBi8},
2201 {ARM::tSUBSrr, ARM::tSUBrr},
2202 {ARM::tSBCS, ARM::tSBC},
Eli Friedman063fd982018-10-31 21:45:48 +00002203 {ARM::tRSBS, ARM::tRSB},
Artyom Skrobov92c06532017-03-22 23:35:51 +00002204
Andrew Trick924123a2011-09-21 02:20:46 +00002205 {ARM::t2ADDSri, ARM::t2ADDri},
2206 {ARM::t2ADDSrr, ARM::t2ADDrr},
2207 {ARM::t2ADDSrs, ARM::t2ADDrs},
2208
2209 {ARM::t2SUBSri, ARM::t2SUBri},
2210 {ARM::t2SUBSrr, ARM::t2SUBrr},
2211 {ARM::t2SUBSrs, ARM::t2SUBrs},
2212
2213 {ARM::t2RSBSri, ARM::t2RSBri},
2214 {ARM::t2RSBSrs, ARM::t2RSBrs},
2215};
2216
2217unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00002218 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2219 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2220 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002221 return 0;
2222}
2223
Evan Cheng780748d2009-07-28 05:48:47 +00002224void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002225 MachineBasicBlock::iterator &MBBI,
2226 const DebugLoc &dl, unsigned DestReg,
2227 unsigned BaseReg, int NumBytes,
2228 ARMCC::CondCodes Pred, unsigned PredReg,
2229 const ARMBaseInstrInfo &TII,
2230 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002231 if (NumBytes == 0 && DestReg != BaseReg) {
2232 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002233 .addReg(BaseReg, RegState::Kill)
2234 .add(predOps(Pred, PredReg))
2235 .add(condCodeOp())
2236 .setMIFlags(MIFlags);
Tim Northoverc9432eb2013-11-04 23:04:15 +00002237 return;
2238 }
2239
Evan Cheng780748d2009-07-28 05:48:47 +00002240 bool isSub = NumBytes < 0;
2241 if (isSub) NumBytes = -NumBytes;
2242
2243 while (NumBytes) {
2244 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2245 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2246 assert(ThisVal && "Didn't extract field correctly");
2247
2248 // We will handle these bits from offset, clear them.
2249 NumBytes &= ~ThisVal;
2250
2251 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2252
2253 // Build the new ADD / SUB.
2254 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2255 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002256 .addReg(BaseReg, RegState::Kill)
2257 .addImm(ThisVal)
2258 .add(predOps(Pred, PredReg))
2259 .add(condCodeOp())
2260 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002261 BaseReg = DestReg;
2262 }
2263}
2264
Tim Northoverdee86042013-12-02 14:46:26 +00002265bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2266 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002267 unsigned NumBytes) {
2268 // This optimisation potentially adds lots of load and store
2269 // micro-operations, it's only really a great benefit to code-size.
Evandro Menezes85bd3972019-04-04 22:40:06 +00002270 if (!Subtarget.hasMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002271 return false;
2272
2273 // If only one register is pushed/popped, LLVM can use an LDR/STR
2274 // instead. We can't modify those so make sure we're dealing with an
2275 // instruction we understand.
2276 bool IsPop = isPopOpcode(MI->getOpcode());
2277 bool IsPush = isPushOpcode(MI->getOpcode());
2278 if (!IsPush && !IsPop)
2279 return false;
2280
2281 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2282 MI->getOpcode() == ARM::VLDMDIA_UPD;
2283 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2284 MI->getOpcode() == ARM::tPOP ||
2285 MI->getOpcode() == ARM::tPOP_RET;
2286
2287 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2288 MI->getOperand(1).getReg() == ARM::SP)) &&
2289 "trying to fold sp update into non-sp-updating push/pop");
2290
2291 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2292 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2293 // if this is violated.
2294 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2295 return false;
2296
2297 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2298 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2299 int RegListIdx = IsT1PushPop ? 2 : 4;
2300
2301 // Calculate the space we'll need in terms of registers.
Tim Northovera9cc3852016-10-26 20:01:00 +00002302 unsigned RegsNeeded;
2303 const TargetRegisterClass *RegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002304 if (IsVFPPushPop) {
Tim Northover93bcc662013-11-08 17:18:07 +00002305 RegsNeeded = NumBytes / 8;
Tim Northovera9cc3852016-10-26 20:01:00 +00002306 RegClass = &ARM::DPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002307 } else {
Tim Northover93bcc662013-11-08 17:18:07 +00002308 RegsNeeded = NumBytes / 4;
Tim Northovera9cc3852016-10-26 20:01:00 +00002309 RegClass = &ARM::GPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002310 }
2311
2312 // We're going to have to strip all list operands off before
2313 // re-adding them since the order matters, so save the existing ones
2314 // for later.
2315 SmallVector<MachineOperand, 4> RegList;
Tim Northovera9cc3852016-10-26 20:01:00 +00002316
2317 // We're also going to need the first register transferred by this
2318 // instruction, which won't necessarily be the first register in the list.
2319 unsigned FirstRegEnc = -1;
Tim Northover93bcc662013-11-08 17:18:07 +00002320
Tim Northover93bcc662013-11-08 17:18:07 +00002321 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northovera9cc3852016-10-26 20:01:00 +00002322 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2323 MachineOperand &MO = MI->getOperand(i);
2324 RegList.push_back(MO);
2325
2326 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2327 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2328 }
2329
Tim Northover45479dc2013-12-01 14:16:24 +00002330 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002331
2332 // Now try to find enough space in the reglist to allocate NumBytes.
Tim Northovera9cc3852016-10-26 20:01:00 +00002333 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2334 --CurRegEnc) {
2335 unsigned CurReg = RegClass->getRegister(CurRegEnc);
Eli Friedman73af6ef2019-04-01 23:55:57 +00002336 if (IsT1PushPop && CurReg > ARM::R7)
2337 continue;
Tim Northover93bcc662013-11-08 17:18:07 +00002338 if (!IsPop) {
Momchil Velikovac7c5c12018-01-08 14:47:19 +00002339 // Pushing any register is completely harmless, mark the register involved
2340 // as undef since we don't care about its value and must not restore it
2341 // during stack unwinding.
Tim Northover93bcc662013-11-08 17:18:07 +00002342 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2343 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002344 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002345 continue;
2346 }
2347
Tim Northover45479dc2013-12-01 14:16:24 +00002348 // However, we can only pop an extra register if it's not live. For
2349 // registers live within the function we might clobber a return value
2350 // register; the other way a register can be live here is if it's
2351 // callee-saved.
2352 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002353 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2354 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002355 // VFP pops don't allow holes in the register list, so any skip is fatal
2356 // for our transformation. GPR pops do, so we should just keep looking.
2357 if (IsVFPPushPop)
2358 return false;
2359 else
2360 continue;
2361 }
Tim Northover93bcc662013-11-08 17:18:07 +00002362
2363 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002364 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2365 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002366 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002367 }
2368
2369 if (RegsNeeded > 0)
2370 return false;
2371
2372 // Finally we know we can profitably perform the optimisation so go
2373 // ahead: strip all existing registers off and add them back again
2374 // in the right order.
2375 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2376 MI->RemoveOperand(i);
2377
2378 // Add the complete list back in.
2379 MachineInstrBuilder MIB(MF, &*MI);
2380 for (int i = RegList.size() - 1; i >= 0; --i)
Diana Picus116bbab2017-01-13 09:58:52 +00002381 MIB.add(RegList[i]);
Tim Northover93bcc662013-11-08 17:18:07 +00002382
2383 return true;
2384}
2385
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002386bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2387 unsigned FrameReg, int &Offset,
2388 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002389 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002390 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002391 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2392 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002393
Evan Cheng780748d2009-07-28 05:48:47 +00002394 // Memory operands in inline assembly always use AddrMode2.
2395 if (Opcode == ARM::INLINEASM)
2396 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002397
Evan Cheng780748d2009-07-28 05:48:47 +00002398 if (Opcode == ARM::ADDri) {
2399 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2400 if (Offset == 0) {
2401 // Turn it into a move.
2402 MI.setDesc(TII.get(ARM::MOVr));
2403 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2404 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002405 Offset = 0;
2406 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002407 } else if (Offset < 0) {
2408 Offset = -Offset;
2409 isSub = true;
2410 MI.setDesc(TII.get(ARM::SUBri));
2411 }
2412
2413 // Common case: small offset, fits into instruction.
2414 if (ARM_AM::getSOImmVal(Offset) != -1) {
2415 // Replace the FrameIndex with sp / fp
2416 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2417 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002418 Offset = 0;
2419 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002420 }
2421
2422 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2423 // as possible.
2424 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2425 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2426
2427 // We will handle these bits from offset, clear them.
2428 Offset &= ~ThisImmVal;
2429
2430 // Get the properly encoded SOImmVal field.
2431 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2432 "Bit extraction didn't work?");
2433 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2434 } else {
2435 unsigned ImmIdx = 0;
2436 int InstrOffs = 0;
2437 unsigned NumBits = 0;
2438 unsigned Scale = 1;
2439 switch (AddrMode) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002440 case ARMII::AddrMode_i12:
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002441 ImmIdx = FrameRegIdx + 1;
2442 InstrOffs = MI.getOperand(ImmIdx).getImm();
2443 NumBits = 12;
2444 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002445 case ARMII::AddrMode2:
Evan Cheng780748d2009-07-28 05:48:47 +00002446 ImmIdx = FrameRegIdx+2;
2447 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2448 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2449 InstrOffs *= -1;
2450 NumBits = 12;
2451 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002452 case ARMII::AddrMode3:
Evan Cheng780748d2009-07-28 05:48:47 +00002453 ImmIdx = FrameRegIdx+2;
2454 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2455 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2456 InstrOffs *= -1;
2457 NumBits = 8;
2458 break;
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002459 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002460 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002461 // Can't fold any offset even if it's zero.
2462 return false;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002463 case ARMII::AddrMode5:
Evan Cheng780748d2009-07-28 05:48:47 +00002464 ImmIdx = FrameRegIdx+1;
2465 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2466 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2467 InstrOffs *= -1;
2468 NumBits = 8;
2469 Scale = 4;
2470 break;
Sjoerd Meijer011de9c2018-01-26 09:26:40 +00002471 case ARMII::AddrMode5FP16:
2472 ImmIdx = FrameRegIdx+1;
2473 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2474 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2475 InstrOffs *= -1;
2476 NumBits = 8;
2477 Scale = 2;
2478 break;
Evan Cheng780748d2009-07-28 05:48:47 +00002479 default:
2480 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002481 }
2482
2483 Offset += InstrOffs * Scale;
2484 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2485 if (Offset < 0) {
2486 Offset = -Offset;
2487 isSub = true;
2488 }
2489
2490 // Attempt to fold address comp. if opcode has offset bits
2491 if (NumBits > 0) {
2492 // Common case: small offset, fits into instruction.
2493 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2494 int ImmedOffset = Offset / Scale;
2495 unsigned Mask = (1 << NumBits) - 1;
2496 if ((unsigned)Offset <= Mask * Scale) {
2497 // Replace the FrameIndex with sp
2498 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002499 // FIXME: When addrmode2 goes away, this will simplify (like the
2500 // T2 version), as the LDR.i12 versions don't need the encoding
2501 // tricks for the offset value.
2502 if (isSub) {
2503 if (AddrMode == ARMII::AddrMode_i12)
2504 ImmedOffset = -ImmedOffset;
2505 else
2506 ImmedOffset |= 1 << NumBits;
2507 }
Evan Cheng780748d2009-07-28 05:48:47 +00002508 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002509 Offset = 0;
2510 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002511 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002512
Evan Cheng780748d2009-07-28 05:48:47 +00002513 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2514 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002515 if (isSub) {
2516 if (AddrMode == ARMII::AddrMode_i12)
2517 ImmedOffset = -ImmedOffset;
2518 else
2519 ImmedOffset |= 1 << NumBits;
2520 }
Evan Cheng780748d2009-07-28 05:48:47 +00002521 ImmOp.ChangeToImmediate(ImmedOffset);
2522 Offset &= ~(Mask*Scale);
2523 }
2524 }
2525
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002526 Offset = (isSub) ? -Offset : Offset;
2527 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002528}
Bill Wendling7de9d522010-08-06 01:32:48 +00002529
Manman Ren6fa76dc2012-06-29 21:33:59 +00002530/// analyzeCompare - For a comparison instruction, return the source registers
2531/// in SrcReg and SrcReg2 if having two register operands, and the value it
2532/// compares against in CmpValue. Return true if the comparison instruction
2533/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002534bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2535 unsigned &SrcReg2, int &CmpMask,
2536 int &CmpValue) const {
2537 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002538 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002539 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002540 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002541 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002542 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002543 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002544 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002545 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002546 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002547 case ARM::CMPrr:
2548 case ARM::t2CMPrr:
David Greenacb628b22019-02-22 12:23:31 +00002549 case ARM::tCMPr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002550 SrcReg = MI.getOperand(0).getReg();
2551 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002552 CmpMask = ~0;
2553 CmpValue = 0;
2554 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002555 case ARM::TSTri:
2556 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002557 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002558 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002559 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002560 CmpValue = 0;
2561 return true;
2562 }
2563
2564 return false;
2565}
2566
Gabor Greifd36e3e82010-09-29 10:12:08 +00002567/// isSuitableForMask - Identify a suitable 'and' instruction that
2568/// operates on the given source register and applies the same mask
2569/// as a 'tst' instruction. Provide a limited look-through for copies.
2570/// When successful, MI will hold the found instruction.
2571static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002572 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002573 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002574 case ARM::ANDri:
2575 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002576 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002577 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002578 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002579 return true;
2580 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002581 }
2582
2583 return false;
2584}
2585
Manman Renb1b3db62012-06-29 22:06:19 +00002586/// getSwappedCondition - assume the flags are set by MI(a,b), return
2587/// the condition code if we modify the instructions such that flags are
2588/// set by MI(b,a).
2589inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2590 switch (CC) {
2591 default: return ARMCC::AL;
2592 case ARMCC::EQ: return ARMCC::EQ;
2593 case ARMCC::NE: return ARMCC::NE;
2594 case ARMCC::HS: return ARMCC::LS;
2595 case ARMCC::LO: return ARMCC::HI;
2596 case ARMCC::HI: return ARMCC::LO;
2597 case ARMCC::LS: return ARMCC::HS;
2598 case ARMCC::GE: return ARMCC::LE;
2599 case ARMCC::LT: return ARMCC::GT;
2600 case ARMCC::GT: return ARMCC::LT;
2601 case ARMCC::LE: return ARMCC::GE;
2602 }
2603}
2604
Joel Galensonfe7fa402018-01-17 19:19:05 +00002605/// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
2606/// the condition code if we modify the instructions such that flags are
2607/// set by ADD(a,b,X).
2608inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
2609 switch (CC) {
2610 default: return ARMCC::AL;
2611 case ARMCC::HS: return ARMCC::LO;
2612 case ARMCC::LO: return ARMCC::HS;
2613 case ARMCC::VS: return ARMCC::VS;
2614 case ARMCC::VC: return ARMCC::VC;
2615 }
2616}
2617
Manman Renb1b3db62012-06-29 22:06:19 +00002618/// isRedundantFlagInstr - check whether the first instruction, whose only
2619/// purpose is to update flags, can be made redundant.
2620/// CMPrr can be made redundant by SUBrr if the operands are the same.
2621/// CMPri can be made redundant by SUBri if the operands are the same.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002622/// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
Manman Renb1b3db62012-06-29 22:06:19 +00002623/// This function can be extended later on.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002624inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
2625 unsigned SrcReg, unsigned SrcReg2,
David Greenacb628b22019-02-22 12:23:31 +00002626 int ImmValue, const MachineInstr *OI,
2627 bool &IsThumb1) {
David Green7e6da812019-02-07 10:51:04 +00002628 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2629 (OI->getOpcode() == ARM::SUBrr || OI->getOpcode() == ARM::t2SUBrr) &&
Manman Renb1b3db62012-06-29 22:06:19 +00002630 ((OI->getOperand(1).getReg() == SrcReg &&
2631 OI->getOperand(2).getReg() == SrcReg2) ||
2632 (OI->getOperand(1).getReg() == SrcReg2 &&
David Greenacb628b22019-02-22 12:23:31 +00002633 OI->getOperand(2).getReg() == SrcReg))) {
2634 IsThumb1 = false;
Manman Renb1b3db62012-06-29 22:06:19 +00002635 return true;
David Greenacb628b22019-02-22 12:23:31 +00002636 }
2637
2638 if (CmpI->getOpcode() == ARM::tCMPr && OI->getOpcode() == ARM::tSUBrr &&
2639 ((OI->getOperand(2).getReg() == SrcReg &&
2640 OI->getOperand(3).getReg() == SrcReg2) ||
2641 (OI->getOperand(2).getReg() == SrcReg2 &&
2642 OI->getOperand(3).getReg() == SrcReg))) {
2643 IsThumb1 = true;
2644 return true;
2645 }
Manman Renb1b3db62012-06-29 22:06:19 +00002646
David Green7e6da812019-02-07 10:51:04 +00002647 if ((CmpI->getOpcode() == ARM::CMPri || CmpI->getOpcode() == ARM::t2CMPri) &&
2648 (OI->getOpcode() == ARM::SUBri || OI->getOpcode() == ARM::t2SUBri) &&
Manman Renb1b3db62012-06-29 22:06:19 +00002649 OI->getOperand(1).getReg() == SrcReg &&
David Greenacb628b22019-02-22 12:23:31 +00002650 OI->getOperand(2).getImm() == ImmValue) {
2651 IsThumb1 = false;
Manman Renb1b3db62012-06-29 22:06:19 +00002652 return true;
David Greenacb628b22019-02-22 12:23:31 +00002653 }
2654
2655 if (CmpI->getOpcode() == ARM::tCMPi8 &&
2656 (OI->getOpcode() == ARM::tSUBi8 || OI->getOpcode() == ARM::tSUBi3) &&
2657 OI->getOperand(2).getReg() == SrcReg &&
2658 OI->getOperand(3).getImm() == ImmValue) {
2659 IsThumb1 = true;
2660 return true;
2661 }
Joel Galensonfe7fa402018-01-17 19:19:05 +00002662
2663 if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
2664 (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
2665 OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
David Green7a183a82019-02-21 11:03:13 +00002666 OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
Joel Galensonfe7fa402018-01-17 19:19:05 +00002667 OI->getOperand(0).getReg() == SrcReg &&
David Greenacb628b22019-02-22 12:23:31 +00002668 OI->getOperand(1).getReg() == SrcReg2) {
2669 IsThumb1 = false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002670 return true;
David Greenacb628b22019-02-22 12:23:31 +00002671 }
2672
2673 if (CmpI->getOpcode() == ARM::tCMPr &&
2674 (OI->getOpcode() == ARM::tADDi3 || OI->getOpcode() == ARM::tADDi8 ||
2675 OI->getOpcode() == ARM::tADDrr) &&
2676 OI->getOperand(0).getReg() == SrcReg &&
2677 OI->getOperand(2).getReg() == SrcReg2) {
2678 IsThumb1 = true;
2679 return true;
2680 }
2681
Manman Renb1b3db62012-06-29 22:06:19 +00002682 return false;
2683}
2684
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002685static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2686 switch (MI->getOpcode()) {
2687 default: return false;
2688 case ARM::tLSLri:
2689 case ARM::tLSRri:
2690 case ARM::tLSLrr:
2691 case ARM::tLSRrr:
2692 case ARM::tSUBrr:
2693 case ARM::tADDrr:
2694 case ARM::tADDi3:
2695 case ARM::tADDi8:
2696 case ARM::tSUBi3:
2697 case ARM::tSUBi8:
2698 case ARM::tMUL:
David Greenb504f102019-02-25 15:50:54 +00002699 case ARM::tADC:
2700 case ARM::tSBC:
2701 case ARM::tRSB:
2702 case ARM::tAND:
2703 case ARM::tORR:
2704 case ARM::tEOR:
2705 case ARM::tBIC:
2706 case ARM::tMVN:
2707 case ARM::tASRri:
2708 case ARM::tASRrr:
2709 case ARM::tROR:
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002710 IsThumb1 = true;
2711 LLVM_FALLTHROUGH;
2712 case ARM::RSBrr:
2713 case ARM::RSBri:
2714 case ARM::RSCrr:
2715 case ARM::RSCri:
2716 case ARM::ADDrr:
2717 case ARM::ADDri:
2718 case ARM::ADCrr:
2719 case ARM::ADCri:
2720 case ARM::SUBrr:
2721 case ARM::SUBri:
2722 case ARM::SBCrr:
2723 case ARM::SBCri:
2724 case ARM::t2RSBri:
2725 case ARM::t2ADDrr:
2726 case ARM::t2ADDri:
2727 case ARM::t2ADCrr:
2728 case ARM::t2ADCri:
2729 case ARM::t2SUBrr:
2730 case ARM::t2SUBri:
2731 case ARM::t2SBCrr:
2732 case ARM::t2SBCri:
2733 case ARM::ANDrr:
2734 case ARM::ANDri:
2735 case ARM::t2ANDrr:
2736 case ARM::t2ANDri:
2737 case ARM::ORRrr:
2738 case ARM::ORRri:
2739 case ARM::t2ORRrr:
2740 case ARM::t2ORRri:
2741 case ARM::EORrr:
2742 case ARM::EORri:
2743 case ARM::t2EORrr:
2744 case ARM::t2EORri:
2745 case ARM::t2LSRri:
2746 case ARM::t2LSRrr:
2747 case ARM::t2LSLri:
2748 case ARM::t2LSLrr:
2749 return true;
2750 }
2751}
2752
Manman Ren6fa76dc2012-06-29 21:33:59 +00002753/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2754/// comparison into one that sets the zero bit in the flags register;
2755/// Remove a redundant Compare instruction if an earlier instruction can set the
2756/// flags in the same way as Compare.
2757/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2758/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2759/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002760bool ARMBaseInstrInfo::optimizeCompareInstr(
2761 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2762 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002763 // Get the unique definition of SrcReg.
2764 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2765 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002766
Gabor Greifadbbb932010-09-21 12:01:15 +00002767 // Masked compares sometimes use the same register as the corresponding 'and'.
2768 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002769 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002770 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002771 for (MachineRegisterInfo::use_instr_iterator
2772 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2773 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002774 if (UI->getParent() != CmpInstr.getParent())
2775 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002776 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002777 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002778 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002779 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002780 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002781 break;
2782 }
2783 if (!MI) return false;
2784 }
2785 }
2786
Manman Rendc8ad002012-05-11 01:30:47 +00002787 // Get ready to iterate backward from CmpInstr.
2788 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002789 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002790
2791 // Early exit if CmpInstr is at the beginning of the BB.
2792 if (I == B) return false;
2793
Manman Rendc8ad002012-05-11 01:30:47 +00002794 // There are two possible candidates which can be changed to set CPSR:
Joel Galensonfe7fa402018-01-17 19:19:05 +00002795 // One is MI, the other is a SUB or ADD instruction.
2796 // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
2797 // ADDr[ri](r1, r2, X).
Manman Rendc8ad002012-05-11 01:30:47 +00002798 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Joel Galensonfe7fa402018-01-17 19:19:05 +00002799 MachineInstr *SubAdd = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002800 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002801 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002802 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002803 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002804 // Conservatively refuse to convert an instruction which isn't in the same
2805 // BB as the comparison.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002806 // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002807 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002808 if (CmpInstr.getOpcode() == ARM::CMPri ||
David Greenacb628b22019-02-22 12:23:31 +00002809 CmpInstr.getOpcode() == ARM::t2CMPri ||
2810 CmpInstr.getOpcode() == ARM::tCMPi8)
Craig Topper062a2ba2014-04-25 05:30:21 +00002811 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002812 else
2813 return false;
2814 }
2815
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002816 bool IsThumb1 = false;
2817 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2818 return false;
2819
2820 // We also want to do this peephole for cases like this: if (a*b == 0),
2821 // and optimise away the CMP instruction from the generated code sequence:
2822 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2823 // resulting from the select instruction, but these MOVS instructions for
2824 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2825 // However, if we only have MOVS instructions in between the CMP and the
2826 // other instruction (the MULS in this example), then the CPSR is dead so we
2827 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2828 // reordering and then continue the analysis hoping we can eliminate the
2829 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2830 // consequence, the movs won't redefine/kill the MUL operands which would
2831 // make this reordering illegal.
David Greenb504f102019-02-25 15:50:54 +00002832 const TargetRegisterInfo *TRI = &getRegisterInfo();
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002833 if (MI && IsThumb1) {
2834 --I;
David Greenb504f102019-02-25 15:50:54 +00002835 if (I != E && !MI->readsRegister(ARM::CPSR, TRI)) {
2836 bool CanReorder = true;
2837 for (; I != E; --I) {
2838 if (I->getOpcode() != ARM::tMOVi8) {
2839 CanReorder = false;
2840 break;
2841 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002842 }
David Greenb504f102019-02-25 15:50:54 +00002843 if (CanReorder) {
2844 MI = MI->removeFromParent();
2845 E = CmpInstr;
2846 CmpInstr.getParent()->insert(E, MI);
2847 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002848 }
2849 I = CmpInstr;
2850 E = MI;
2851 }
2852
Manman Rendc8ad002012-05-11 01:30:47 +00002853 // Check that CPSR isn't set between the comparison instruction and the one we
Joel Galensonfe7fa402018-01-17 19:19:05 +00002854 // want to change. At the same time, search for SubAdd.
David Greenacb628b22019-02-22 12:23:31 +00002855 bool SubAddIsThumb1 = false;
Joel Galenson1d89cd22018-01-22 17:53:47 +00002856 do {
2857 const MachineInstr &Instr = *--I;
Bill Wendling7de9d522010-08-06 01:32:48 +00002858
Joel Galensonfe7fa402018-01-17 19:19:05 +00002859 // Check whether CmpInstr can be made redundant by the current instruction.
David Greenacb628b22019-02-22 12:23:31 +00002860 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr,
2861 SubAddIsThumb1)) {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002862 SubAdd = &*I;
2863 break;
2864 }
2865
Joel Galenson1d89cd22018-01-22 17:53:47 +00002866 // Allow E (which was initially MI) to be SubAdd but do not search before E.
2867 if (I == E)
2868 break;
2869
Manman Renb1b3db62012-06-29 22:06:19 +00002870 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2871 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002872 // This instruction modifies or uses CPSR after the one we want to
2873 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002874 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002875
Eli Friedmanb906bba2019-03-22 20:49:15 +00002876 if (I == B) {
2877 // In some cases, we scan the use-list of an instruction for an AND;
2878 // that AND is in the same BB, but may not be scheduled before the
2879 // corresponding TST. In that case, bail out.
2880 //
2881 // FIXME: We could try to reschedule the AND.
2882 return false;
2883 }
2884 } while (true);
Bill Wendling7de9d522010-08-06 01:32:48 +00002885
Manman Rendc8ad002012-05-11 01:30:47 +00002886 // Return false if no candidates exist.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002887 if (!MI && !SubAdd)
Manman Rendc8ad002012-05-11 01:30:47 +00002888 return false;
2889
David Green743abf22019-02-14 11:09:24 +00002890 // If we found a SubAdd, use it as it will be closer to the CMP
2891 if (SubAdd) {
2892 MI = SubAdd;
David Greenacb628b22019-02-22 12:23:31 +00002893 IsThumb1 = SubAddIsThumb1;
David Green743abf22019-02-14 11:09:24 +00002894 }
Manman Rendc8ad002012-05-11 01:30:47 +00002895
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002896 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002897 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002898 return false;
2899
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002900 // Scan forward for the use of CPSR
2901 // When checking against MI: if it's a conditional code that requires
2902 // checking of the V bit or C bit, then this is not safe to do.
2903 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2904 // If we are done with the basic block, we need to check whether CPSR is
2905 // live-out.
2906 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2907 OperandsToUpdate;
2908 bool isSafe = false;
2909 I = CmpInstr;
2910 E = CmpInstr.getParent()->end();
2911 while (!isSafe && ++I != E) {
2912 const MachineInstr &Instr = *I;
2913 for (unsigned IO = 0, EO = Instr.getNumOperands();
2914 !isSafe && IO != EO; ++IO) {
2915 const MachineOperand &MO = Instr.getOperand(IO);
2916 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2917 isSafe = true;
2918 break;
2919 }
2920 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2921 continue;
2922 if (MO.isDef()) {
2923 isSafe = true;
2924 break;
2925 }
2926 // Condition code is after the operand before CPSR except for VSELs.
2927 ARMCC::CondCodes CC;
2928 bool IsInstrVSel = true;
2929 switch (Instr.getOpcode()) {
2930 default:
2931 IsInstrVSel = false;
2932 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2933 break;
2934 case ARM::VSELEQD:
2935 case ARM::VSELEQS:
2936 CC = ARMCC::EQ;
2937 break;
2938 case ARM::VSELGTD:
2939 case ARM::VSELGTS:
2940 CC = ARMCC::GT;
2941 break;
2942 case ARM::VSELGED:
2943 case ARM::VSELGES:
2944 CC = ARMCC::GE;
2945 break;
2946 case ARM::VSELVSS:
2947 case ARM::VSELVSD:
2948 CC = ARMCC::VS;
2949 break;
2950 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002951
Joel Galensonfe7fa402018-01-17 19:19:05 +00002952 if (SubAdd) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002953 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2954 // on CMP needs to be updated to be based on SUB.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002955 // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
2956 // needs to be modified.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002957 // Push the condition code operands to OperandsToUpdate.
2958 // If it is safe to remove CmpInstr, the condition code of these
2959 // operands will be modified.
Joel Galensonfe7fa402018-01-17 19:19:05 +00002960 unsigned Opc = SubAdd->getOpcode();
2961 bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
David Greenacb628b22019-02-22 12:23:31 +00002962 Opc == ARM::SUBri || Opc == ARM::t2SUBri ||
2963 Opc == ARM::tSUBrr || Opc == ARM::tSUBi3 ||
2964 Opc == ARM::tSUBi8;
2965 unsigned OpI = Opc != ARM::tSUBrr ? 1 : 2;
2966 if (!IsSub ||
2967 (SrcReg2 != 0 && SubAdd->getOperand(OpI).getReg() == SrcReg2 &&
2968 SubAdd->getOperand(OpI + 1).getReg() == SrcReg)) {
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002969 // VSel doesn't support condition code update.
2970 if (IsInstrVSel)
Manman Rendc8ad002012-05-11 01:30:47 +00002971 return false;
Joel Galensonfe7fa402018-01-17 19:19:05 +00002972 // Ensure we can swap the condition.
2973 ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
2974 if (NewCC == ARMCC::AL)
2975 return false;
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002976 OperandsToUpdate.push_back(
2977 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2978 }
2979 } else {
Joel Galensonfe7fa402018-01-17 19:19:05 +00002980 // No SubAdd, so this is x = <op> y, z; cmp x, 0.
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002981 switch (CC) {
2982 case ARMCC::EQ: // Z
2983 case ARMCC::NE: // Z
2984 case ARMCC::MI: // N
2985 case ARMCC::PL: // N
2986 case ARMCC::AL: // none
2987 // CPSR can be used multiple times, we should continue.
2988 break;
2989 case ARMCC::HS: // C
2990 case ARMCC::LO: // C
2991 case ARMCC::VS: // V
2992 case ARMCC::VC: // V
2993 case ARMCC::HI: // C Z
2994 case ARMCC::LS: // C Z
2995 case ARMCC::GE: // N V
2996 case ARMCC::LT: // N V
2997 case ARMCC::GT: // Z N V
2998 case ARMCC::LE: // Z N V
2999 // The instruction uses the V bit or C bit which is not safe.
3000 return false;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00003001 }
Evan Cheng425489d2011-03-23 22:52:04 +00003002 }
3003 }
Bill Wendling7de9d522010-08-06 01:32:48 +00003004 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003005
3006 // If CPSR is not killed nor re-defined, we should check whether it is
3007 // live-out. If it is live-out, do not optimize.
3008 if (!isSafe) {
3009 MachineBasicBlock *MBB = CmpInstr.getParent();
3010 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
3011 SE = MBB->succ_end(); SI != SE; ++SI)
3012 if ((*SI)->isLiveIn(ARM::CPSR))
3013 return false;
Cameron Zwarich0829b302011-04-15 20:45:00 +00003014 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003015
3016 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
3017 // set CPSR so this is represented as an explicit output)
3018 if (!IsThumb1) {
3019 MI->getOperand(5).setReg(ARM::CPSR);
3020 MI->getOperand(5).setIsDef(true);
3021 }
3022 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
3023 CmpInstr.eraseFromParent();
3024
3025 // Modify the condition code of operands in OperandsToUpdate.
3026 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
3027 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
3028 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
3029 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
3030
Eli Friedman2ac11622018-10-26 19:32:24 +00003031 MI->clearRegisterDeads(ARM::CPSR);
3032
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00003033 return true;
Bill Wendling7de9d522010-08-06 01:32:48 +00003034}
Evan Cheng367a5df2010-09-09 18:18:55 +00003035
Joel Galensonfe7fa402018-01-17 19:19:05 +00003036bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
3037 // Do not sink MI if it might be used to optimize a redundant compare.
3038 // We heuristically only look at the instruction immediately following MI to
3039 // avoid potentially searching the entire basic block.
3040 if (isPredicated(MI))
3041 return true;
3042 MachineBasicBlock::const_iterator Next = &MI;
3043 ++Next;
3044 unsigned SrcReg, SrcReg2;
3045 int CmpMask, CmpValue;
David Greenacb628b22019-02-22 12:23:31 +00003046 bool IsThumb1;
Joel Galensonfe7fa402018-01-17 19:19:05 +00003047 if (Next != MI.getParent()->end() &&
3048 analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
David Greenacb628b22019-02-22 12:23:31 +00003049 isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI, IsThumb1))
Joel Galensonfe7fa402018-01-17 19:19:05 +00003050 return false;
3051 return true;
3052}
3053
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003054bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
3055 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003056 MachineRegisterInfo *MRI) const {
3057 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003058 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003059 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
3060 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003061 if (!DefMI.getOperand(1).isImm())
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00003062 // Could be t2MOVi32imm @xx
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003063 return false;
3064
3065 if (!MRI->hasOneNonDBGUse(Reg))
3066 return false;
3067
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003068 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00003069 if (DefMCID.hasOptionalDef()) {
3070 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003071 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00003072 if (MO.getReg() == ARM::CPSR && !MO.isDead())
3073 // If DefMI defines CPSR and it is not dead, it's obviously not safe
3074 // to delete DefMI.
3075 return false;
3076 }
3077
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003078 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00003079 if (UseMCID.hasOptionalDef()) {
3080 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003081 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00003082 // If the instruction sets the flag, do not attempt this optimization
3083 // since it may change the semantics of the code.
3084 return false;
3085 }
3086
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003087 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003088 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003089 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00003090 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003091 bool Commute = false;
3092 switch (UseOpc) {
3093 default: return false;
3094 case ARM::SUBrr:
3095 case ARM::ADDrr:
3096 case ARM::ORRrr:
3097 case ARM::EORrr:
3098 case ARM::t2SUBrr:
3099 case ARM::t2ADDrr:
3100 case ARM::t2ORRrr:
3101 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003102 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003103 switch (UseOpc) {
3104 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003105 case ARM::ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003106 case ARM::SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003107 if (UseOpc == ARM::SUBrr && Commute)
3108 return false;
3109
3110 // ADD/SUB are special because they're essentially the same operation, so
3111 // we can handle a larger range of immediates.
3112 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
3113 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
3114 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
3115 ImmVal = -ImmVal;
3116 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
3117 } else
3118 return false;
3119 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3120 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3121 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003122 case ARM::ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003123 case ARM::EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003124 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
3125 return false;
3126 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
3127 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
3128 switch (UseOpc) {
3129 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003130 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
3131 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
3132 }
3133 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003134 case ARM::t2ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003135 case ARM::t2SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00003136 if (UseOpc == ARM::t2SUBrr && Commute)
3137 return false;
3138
3139 // ADD/SUB are special because they're essentially the same operation, so
3140 // we can handle a larger range of immediates.
3141 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3142 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
3143 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
3144 ImmVal = -ImmVal;
3145 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
3146 } else
3147 return false;
3148 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3149 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3150 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003151 case ARM::t2ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003152 case ARM::t2EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003153 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3154 return false;
3155 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
3156 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
3157 switch (UseOpc) {
3158 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003159 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
3160 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
3161 }
3162 break;
3163 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003164 }
3165 }
3166
3167 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003168 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
3169 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003170 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Diana Picus8a73f552017-01-13 10:18:01 +00003171 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
3172 NewReg)
3173 .addReg(Reg1, getKillRegState(isKill))
3174 .addImm(SOImmValV1)
3175 .add(predOps(ARMCC::AL))
3176 .add(condCodeOp());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003177 UseMI.setDesc(get(NewUseOpc));
3178 UseMI.getOperand(1).setReg(NewReg);
3179 UseMI.getOperand(1).setIsKill();
3180 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
3181 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00003182 return true;
3183}
3184
Bob Wilsone8a549c2012-09-29 21:43:49 +00003185static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003186 const MachineInstr &MI) {
3187 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003188 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003189 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003190 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
3191 assert(UOps >= 0 && "bad # UOps");
3192 return UOps;
3193 }
3194
3195 case ARM::LDRrs:
3196 case ARM::LDRBrs:
3197 case ARM::STRrs:
3198 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003199 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003200 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3201 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3202 if (!isSub &&
3203 (ShImm == 0 ||
3204 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3205 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3206 return 1;
3207 return 2;
3208 }
3209
3210 case ARM::LDRH:
3211 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003212 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00003213 return 1;
3214
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003215 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003216 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3217 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3218 if (!isSub &&
3219 (ShImm == 0 ||
3220 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3221 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3222 return 1;
3223 return 2;
3224 }
3225
3226 case ARM::LDRSB:
3227 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003228 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003229
3230 case ARM::LDRSB_POST:
3231 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003232 unsigned Rt = MI.getOperand(0).getReg();
3233 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003234 return (Rt == Rm) ? 4 : 3;
3235 }
3236
3237 case ARM::LDR_PRE_REG:
3238 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003239 unsigned Rt = MI.getOperand(0).getReg();
3240 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003241 if (Rt == Rm)
3242 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003243 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003244 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3245 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3246 if (!isSub &&
3247 (ShImm == 0 ||
3248 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3249 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3250 return 2;
3251 return 3;
3252 }
3253
3254 case ARM::STR_PRE_REG:
3255 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003256 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003257 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3258 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3259 if (!isSub &&
3260 (ShImm == 0 ||
3261 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3262 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3263 return 2;
3264 return 3;
3265 }
3266
3267 case ARM::LDRH_PRE:
3268 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003269 unsigned Rt = MI.getOperand(0).getReg();
3270 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003271 if (!Rm)
3272 return 2;
3273 if (Rt == Rm)
3274 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003275 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003276 }
3277
3278 case ARM::LDR_POST_REG:
3279 case ARM::LDRB_POST_REG:
3280 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003281 unsigned Rt = MI.getOperand(0).getReg();
3282 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003283 return (Rt == Rm) ? 3 : 2;
3284 }
3285
3286 case ARM::LDR_PRE_IMM:
3287 case ARM::LDRB_PRE_IMM:
3288 case ARM::LDR_POST_IMM:
3289 case ARM::LDRB_POST_IMM:
3290 case ARM::STRB_POST_IMM:
3291 case ARM::STRB_POST_REG:
3292 case ARM::STRB_PRE_IMM:
3293 case ARM::STRH_POST:
3294 case ARM::STR_POST_IMM:
3295 case ARM::STR_POST_REG:
3296 case ARM::STR_PRE_IMM:
3297 return 2;
3298
3299 case ARM::LDRSB_PRE:
3300 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003301 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003302 if (Rm == 0)
3303 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003304 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003305 if (Rt == Rm)
3306 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003307 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003308 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3309 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3310 if (!isSub &&
3311 (ShImm == 0 ||
3312 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3313 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3314 return 3;
3315 return 4;
3316 }
3317
3318 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003319 unsigned Rt = MI.getOperand(0).getReg();
3320 unsigned Rn = MI.getOperand(2).getReg();
3321 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003322 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003323 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3324 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003325 return (Rt == Rn) ? 3 : 2;
3326 }
3327
3328 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003329 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003330 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003331 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3332 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003333 return 2;
3334 }
3335
3336 case ARM::LDRD_POST:
3337 case ARM::t2LDRD_POST:
3338 return 3;
3339
3340 case ARM::STRD_POST:
3341 case ARM::t2STRD_POST:
3342 return 4;
3343
3344 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003345 unsigned Rt = MI.getOperand(0).getReg();
3346 unsigned Rn = MI.getOperand(3).getReg();
3347 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003348 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003349 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3350 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003351 return (Rt == Rn) ? 4 : 3;
3352 }
3353
3354 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003355 unsigned Rt = MI.getOperand(0).getReg();
3356 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003357 return (Rt == Rn) ? 4 : 3;
3358 }
3359
3360 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003361 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003362 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003363 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3364 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003365 return 3;
3366 }
3367
3368 case ARM::t2STRD_PRE:
3369 return 3;
3370
3371 case ARM::t2LDR_POST:
3372 case ARM::t2LDRB_POST:
3373 case ARM::t2LDRB_PRE:
3374 case ARM::t2LDRSBi12:
3375 case ARM::t2LDRSBi8:
3376 case ARM::t2LDRSBpci:
3377 case ARM::t2LDRSBs:
3378 case ARM::t2LDRH_POST:
3379 case ARM::t2LDRH_PRE:
3380 case ARM::t2LDRSBT:
3381 case ARM::t2LDRSB_POST:
3382 case ARM::t2LDRSB_PRE:
3383 case ARM::t2LDRSH_POST:
3384 case ARM::t2LDRSH_PRE:
3385 case ARM::t2LDRSHi12:
3386 case ARM::t2LDRSHi8:
3387 case ARM::t2LDRSHpci:
3388 case ARM::t2LDRSHs:
3389 return 2;
3390
3391 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003392 unsigned Rt = MI.getOperand(0).getReg();
3393 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003394 return (Rt == Rn) ? 3 : 2;
3395 }
3396
3397 case ARM::t2STRB_POST:
3398 case ARM::t2STRB_PRE:
3399 case ARM::t2STRBs:
3400 case ARM::t2STRDi8:
3401 case ARM::t2STRH_POST:
3402 case ARM::t2STRH_PRE:
3403 case ARM::t2STRHs:
3404 case ARM::t2STR_POST:
3405 case ARM::t2STR_PRE:
3406 case ARM::t2STRs:
3407 return 2;
3408 }
3409}
3410
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003411// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3412// can't be easily determined return 0 (missing MachineMemOperand).
3413//
3414// FIXME: The current MachineInstr design does not support relying on machine
3415// mem operands to determine the width of a memory access. Instead, we expect
3416// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003417// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003418// two reasons:
3419//
3420// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3421// operands. This is much more dangerous than using the MachineMemOperand
3422// sizes because CodeGen passes can insert/remove optional machine operands. In
3423// fact, it's totally incorrect for preRA passes and appears to be wrong for
3424// postRA passes as well.
3425//
3426// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3427// machine model that calls this should handle the unknown (zero size) case.
3428//
3429// Long term, we should require a target hook that verifies MachineMemOperand
3430// sizes during MC lowering. That target hook should be local to MC lowering
3431// because we can't ensure that it is aware of other MI forms. Doing this will
3432// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003433unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003434 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003435 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3436 E = MI.memoperands_end();
3437 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003438 Size += (*I)->getSize();
3439 }
Eli Friedmanc388bfa2019-03-27 18:33:30 +00003440 // FIXME: The scheduler currently can't handle values larger than 16. But
3441 // the values can actually go up to 32 for floating-point load/store
3442 // multiple (VLDMIA etc.). Also, the way this code is reasoning about memory
3443 // operations isn't right; we could end up with "extra" memory operands for
3444 // various reasons, like tail merge merging two memory operations.
3445 return std::min(Size / 4, 16U);
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003446}
3447
Diana Picus92423ce2016-06-27 09:08:23 +00003448static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3449 unsigned NumRegs) {
3450 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3451 switch (Opc) {
3452 default:
3453 break;
3454 case ARM::VLDMDIA_UPD:
3455 case ARM::VLDMDDB_UPD:
3456 case ARM::VLDMSIA_UPD:
3457 case ARM::VLDMSDB_UPD:
3458 case ARM::VSTMDIA_UPD:
3459 case ARM::VSTMDDB_UPD:
3460 case ARM::VSTMSIA_UPD:
3461 case ARM::VSTMSDB_UPD:
3462 case ARM::LDMIA_UPD:
3463 case ARM::LDMDA_UPD:
3464 case ARM::LDMDB_UPD:
3465 case ARM::LDMIB_UPD:
3466 case ARM::STMIA_UPD:
3467 case ARM::STMDA_UPD:
3468 case ARM::STMDB_UPD:
3469 case ARM::STMIB_UPD:
3470 case ARM::tLDMIA_UPD:
3471 case ARM::tSTMIA_UPD:
3472 case ARM::t2LDMIA_UPD:
3473 case ARM::t2LDMDB_UPD:
3474 case ARM::t2STMIA_UPD:
3475 case ARM::t2STMDB_UPD:
3476 ++UOps; // One for base register writeback.
3477 break;
3478 case ARM::LDMIA_RET:
3479 case ARM::tPOP_RET:
3480 case ARM::t2LDMIA_RET:
3481 UOps += 2; // One for base reg wb, one for write to pc.
3482 break;
3483 }
3484 return UOps;
3485}
3486
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003487unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3488 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003489 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003490 return 1;
3491
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003492 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003493 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003494 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003495 if (ItinUOps >= 0) {
3496 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3497 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3498
Andrew Trickf161e392012-07-02 18:10:42 +00003499 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003500 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003501
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003502 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003503 switch (Opc) {
3504 default:
3505 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003506 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003507 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003508 return 2;
3509
3510 // The number of uOps for load / store multiple are determined by the number
3511 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003512 //
Evan Chengbf407072010-09-10 01:29:16 +00003513 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3514 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003515 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003516 //
Evan Chengbf407072010-09-10 01:29:16 +00003517 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003518 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3519 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3520 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003521 case ARM::VLDMDIA_UPD:
3522 case ARM::VLDMDDB_UPD:
3523 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003524 case ARM::VLDMSIA_UPD:
3525 case ARM::VLDMSDB_UPD:
3526 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003527 case ARM::VSTMDIA_UPD:
3528 case ARM::VSTMDDB_UPD:
3529 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003530 case ARM::VSTMSIA_UPD:
3531 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003532 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003533 return (NumRegs / 2) + (NumRegs % 2) + 1;
3534 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003535
3536 case ARM::LDMIA_RET:
3537 case ARM::LDMIA:
3538 case ARM::LDMDA:
3539 case ARM::LDMDB:
3540 case ARM::LDMIB:
3541 case ARM::LDMIA_UPD:
3542 case ARM::LDMDA_UPD:
3543 case ARM::LDMDB_UPD:
3544 case ARM::LDMIB_UPD:
3545 case ARM::STMIA:
3546 case ARM::STMDA:
3547 case ARM::STMDB:
3548 case ARM::STMIB:
3549 case ARM::STMIA_UPD:
3550 case ARM::STMDA_UPD:
3551 case ARM::STMDB_UPD:
3552 case ARM::STMIB_UPD:
3553 case ARM::tLDMIA:
3554 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003555 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003556 case ARM::tPOP_RET:
3557 case ARM::tPOP:
3558 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003559 case ARM::t2LDMIA_RET:
3560 case ARM::t2LDMIA:
3561 case ARM::t2LDMDB:
3562 case ARM::t2LDMIA_UPD:
3563 case ARM::t2LDMDB_UPD:
3564 case ARM::t2STMIA:
3565 case ARM::t2STMDB:
3566 case ARM::t2STMIA_UPD:
3567 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003568 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003569 switch (Subtarget.getLdStMultipleTiming()) {
3570 case ARMSubtarget::SingleIssuePlusExtras:
3571 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3572 case ARMSubtarget::SingleIssue:
3573 // Assume the worst.
3574 return NumRegs;
3575 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003576 if (NumRegs < 4)
3577 return 2;
3578 // 4 registers would be issued: 2, 2.
3579 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003580 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003581 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003582 ++UOps;
3583 return UOps;
3584 }
3585 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3586 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003587 // If there are odd number of registers or if it's not 64-bit aligned,
3588 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003589 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3590 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003591 ++UOps;
3592 return UOps;
3593 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003594 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003595 }
3596 }
Diana Picus92423ce2016-06-27 09:08:23 +00003597 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003598}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003599
3600int
Evan Cheng412e37b2010-10-07 23:12:15 +00003601ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003602 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003603 unsigned DefClass,
3604 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003605 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003606 if (RegNo <= 0)
3607 // Def is the address writeback.
3608 return ItinData->getOperandCycle(DefClass, DefIdx);
3609
3610 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003611 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003612 // (regno / 2) + (regno % 2) + 1
3613 DefCycle = RegNo / 2 + 1;
3614 if (RegNo % 2)
3615 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003616 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003617 DefCycle = RegNo;
3618 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003619
Evan Cheng6cc775f2011-06-28 19:10:37 +00003620 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003621 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003622 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003623 case ARM::VLDMSIA_UPD:
3624 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003625 isSLoad = true;
3626 break;
3627 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003628
Evan Cheng412e37b2010-10-07 23:12:15 +00003629 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3630 // then it takes an extra cycle.
3631 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3632 ++DefCycle;
3633 } else {
3634 // Assume the worst.
3635 DefCycle = RegNo + 2;
3636 }
3637
3638 return DefCycle;
3639}
3640
Javed Absar4ae7e8122017-06-02 08:53:19 +00003641bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3642 unsigned BaseReg = MI.getOperand(0).getReg();
3643 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3644 const auto &Op = MI.getOperand(i);
3645 if (Op.isReg() && Op.getReg() == BaseReg)
3646 return true;
3647 }
3648 return false;
3649}
3650unsigned
3651ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00003652 // ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops
3653 // (outs GPR:$wb), (ins GPR:$Rn, $p (2xOp), reglist:$regs, variable_ops)
Javed Absar4ae7e8122017-06-02 08:53:19 +00003654 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3655}
3656
Evan Cheng412e37b2010-10-07 23:12:15 +00003657int
3658ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003659 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003660 unsigned DefClass,
3661 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003662 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003663 if (RegNo <= 0)
3664 // Def is the address writeback.
3665 return ItinData->getOperandCycle(DefClass, DefIdx);
3666
3667 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003668 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003669 // 4 registers would be issued: 1, 2, 1.
3670 // 5 registers would be issued: 1, 2, 2.
3671 DefCycle = RegNo / 2;
3672 if (DefCycle < 1)
3673 DefCycle = 1;
3674 // Result latency is issue cycle + 2: E2.
3675 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003676 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003677 DefCycle = (RegNo / 2);
3678 // If there are odd number of registers or if it's not 64-bit aligned,
3679 // then it takes an extra AGU (Address Generation Unit) cycle.
3680 if ((RegNo % 2) || DefAlign < 8)
3681 ++DefCycle;
3682 // Result latency is AGU cycles + 2.
3683 DefCycle += 2;
3684 } else {
3685 // Assume the worst.
3686 DefCycle = RegNo + 2;
3687 }
3688
3689 return DefCycle;
3690}
3691
3692int
3693ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003694 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003695 unsigned UseClass,
3696 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003697 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003698 if (RegNo <= 0)
3699 return ItinData->getOperandCycle(UseClass, UseIdx);
3700
3701 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003702 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003703 // (regno / 2) + (regno % 2) + 1
3704 UseCycle = RegNo / 2 + 1;
3705 if (RegNo % 2)
3706 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003707 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003708 UseCycle = RegNo;
3709 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003710
Evan Cheng6cc775f2011-06-28 19:10:37 +00003711 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003712 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003713 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003714 case ARM::VSTMSIA_UPD:
3715 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003716 isSStore = true;
3717 break;
3718 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003719
Evan Cheng412e37b2010-10-07 23:12:15 +00003720 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3721 // then it takes an extra cycle.
3722 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3723 ++UseCycle;
3724 } else {
3725 // Assume the worst.
3726 UseCycle = RegNo + 2;
3727 }
3728
3729 return UseCycle;
3730}
3731
3732int
3733ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003734 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003735 unsigned UseClass,
3736 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003737 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003738 if (RegNo <= 0)
3739 return ItinData->getOperandCycle(UseClass, UseIdx);
3740
3741 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003742 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003743 UseCycle = RegNo / 2;
3744 if (UseCycle < 2)
3745 UseCycle = 2;
3746 // Read in E3.
3747 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003748 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003749 UseCycle = (RegNo / 2);
3750 // If there are odd number of registers or if it's not 64-bit aligned,
3751 // then it takes an extra AGU (Address Generation Unit) cycle.
3752 if ((RegNo % 2) || UseAlign < 8)
3753 ++UseCycle;
3754 } else {
3755 // Assume the worst.
3756 UseCycle = 1;
3757 }
3758 return UseCycle;
3759}
3760
3761int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003762ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003763 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003764 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003765 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003766 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003767 unsigned DefClass = DefMCID.getSchedClass();
3768 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003769
Evan Cheng6cc775f2011-06-28 19:10:37 +00003770 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003771 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3772
3773 // This may be a def / use of a variable_ops instruction, the operand
3774 // latency might be determinable dynamically. Let the target try to
3775 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003776 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003777 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003778 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003779 default:
3780 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3781 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003782
3783 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003784 case ARM::VLDMDIA_UPD:
3785 case ARM::VLDMDDB_UPD:
3786 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003787 case ARM::VLDMSIA_UPD:
3788 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003789 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003790 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003791
3792 case ARM::LDMIA_RET:
3793 case ARM::LDMIA:
3794 case ARM::LDMDA:
3795 case ARM::LDMDB:
3796 case ARM::LDMIB:
3797 case ARM::LDMIA_UPD:
3798 case ARM::LDMDA_UPD:
3799 case ARM::LDMDB_UPD:
3800 case ARM::LDMIB_UPD:
3801 case ARM::tLDMIA:
3802 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003803 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003804 case ARM::t2LDMIA_RET:
3805 case ARM::t2LDMIA:
3806 case ARM::t2LDMDB:
3807 case ARM::t2LDMIA_UPD:
3808 case ARM::t2LDMDB_UPD:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003809 LdmBypass = true;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003810 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003811 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003812 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003813
3814 if (DefCycle == -1)
3815 // We can't seem to determine the result latency of the def, assume it's 2.
3816 DefCycle = 2;
3817
3818 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003819 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003820 default:
3821 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3822 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003823
3824 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003825 case ARM::VSTMDIA_UPD:
3826 case ARM::VSTMDDB_UPD:
3827 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003828 case ARM::VSTMSIA_UPD:
3829 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003830 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003831 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003832
3833 case ARM::STMIA:
3834 case ARM::STMDA:
3835 case ARM::STMDB:
3836 case ARM::STMIB:
3837 case ARM::STMIA_UPD:
3838 case ARM::STMDA_UPD:
3839 case ARM::STMDB_UPD:
3840 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003841 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003842 case ARM::tPOP_RET:
3843 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003844 case ARM::t2STMIA:
3845 case ARM::t2STMDB:
3846 case ARM::t2STMIA_UPD:
3847 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003848 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003849 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003850 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003851
3852 if (UseCycle == -1)
3853 // Assume it's read in the first stage.
3854 UseCycle = 1;
3855
3856 UseCycle = DefCycle - UseCycle + 1;
3857 if (UseCycle > 0) {
3858 if (LdmBypass) {
3859 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3860 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003861 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003862 UseClass, UseIdx))
3863 --UseCycle;
3864 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003865 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003866 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003867 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003868 }
3869
3870 return UseCycle;
3871}
3872
Evan Cheng7fae11b2011-12-14 02:11:42 +00003873static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003874 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003875 unsigned &DefIdx, unsigned &Dist) {
3876 Dist = 0;
3877
3878 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003879 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003880 assert(II->isInsideBundle() && "Empty bundle?");
3881
3882 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003883 while (II->isInsideBundle()) {
3884 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3885 if (Idx != -1)
3886 break;
3887 --II;
3888 ++Dist;
3889 }
3890
3891 assert(Idx != -1 && "Cannot find bundled definition!");
3892 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003893 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003894}
3895
3896static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003897 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003898 unsigned &UseIdx, unsigned &Dist) {
3899 Dist = 0;
3900
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003901 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003902 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003903 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003904
3905 // FIXME: This doesn't properly handle multiple uses.
3906 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003907 while (II != E && II->isInsideBundle()) {
3908 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3909 if (Idx != -1)
3910 break;
3911 if (II->getOpcode() != ARM::t2IT)
3912 ++Dist;
3913 ++II;
3914 }
3915
Evan Chengda103bf2011-12-14 20:00:08 +00003916 if (Idx == -1) {
3917 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003918 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003919 }
3920
Evan Cheng7fae11b2011-12-14 02:11:42 +00003921 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003922 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003923}
3924
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003925/// Return the number of cycles to add to (or subtract from) the static
3926/// itinerary based on the def opcode and alignment. The caller will ensure that
3927/// adjusted latency is at least one cycle.
3928static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003929 const MachineInstr &DefMI,
3930 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003931 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003932 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003933 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3934 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003935 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003936 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003937 case ARM::LDRrs:
3938 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003939 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003940 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3941 if (ShImm == 0 ||
3942 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003943 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003944 break;
3945 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003946 case ARM::t2LDRs:
3947 case ARM::t2LDRBs:
3948 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003949 case ARM::t2LDRSHs: {
3950 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003951 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003952 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003953 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003954 break;
3955 }
3956 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003957 } else if (Subtarget.isSwift()) {
3958 // FIXME: Properly handle all of the latency adjustments for address
3959 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003960 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003961 default: break;
3962 case ARM::LDRrs:
3963 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003964 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003965 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3966 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3967 if (!isSub &&
3968 (ShImm == 0 ||
3969 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3970 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3971 Adjust -= 2;
3972 else if (!isSub &&
3973 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3974 --Adjust;
3975 break;
3976 }
3977 case ARM::t2LDRs:
3978 case ARM::t2LDRBs:
3979 case ARM::t2LDRHs:
3980 case ARM::t2LDRSHs: {
3981 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003982 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003983 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3984 Adjust -= 2;
3985 break;
3986 }
3987 }
Evan Chengff310732010-10-28 06:47:08 +00003988 }
3989
Diana Picus92423ce2016-06-27 09:08:23 +00003990 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003991 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003992 default: break;
3993 case ARM::VLD1q8:
3994 case ARM::VLD1q16:
3995 case ARM::VLD1q32:
3996 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003997 case ARM::VLD1q8wb_fixed:
3998 case ARM::VLD1q16wb_fixed:
3999 case ARM::VLD1q32wb_fixed:
4000 case ARM::VLD1q64wb_fixed:
4001 case ARM::VLD1q8wb_register:
4002 case ARM::VLD1q16wb_register:
4003 case ARM::VLD1q32wb_register:
4004 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004005 case ARM::VLD2d8:
4006 case ARM::VLD2d16:
4007 case ARM::VLD2d32:
4008 case ARM::VLD2q8:
4009 case ARM::VLD2q16:
4010 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00004011 case ARM::VLD2d8wb_fixed:
4012 case ARM::VLD2d16wb_fixed:
4013 case ARM::VLD2d32wb_fixed:
4014 case ARM::VLD2q8wb_fixed:
4015 case ARM::VLD2q16wb_fixed:
4016 case ARM::VLD2q32wb_fixed:
4017 case ARM::VLD2d8wb_register:
4018 case ARM::VLD2d16wb_register:
4019 case ARM::VLD2d32wb_register:
4020 case ARM::VLD2q8wb_register:
4021 case ARM::VLD2q16wb_register:
4022 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004023 case ARM::VLD3d8:
4024 case ARM::VLD3d16:
4025 case ARM::VLD3d32:
4026 case ARM::VLD1d64T:
4027 case ARM::VLD3d8_UPD:
4028 case ARM::VLD3d16_UPD:
4029 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00004030 case ARM::VLD1d64Twb_fixed:
4031 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004032 case ARM::VLD3q8_UPD:
4033 case ARM::VLD3q16_UPD:
4034 case ARM::VLD3q32_UPD:
4035 case ARM::VLD4d8:
4036 case ARM::VLD4d16:
4037 case ARM::VLD4d32:
4038 case ARM::VLD1d64Q:
4039 case ARM::VLD4d8_UPD:
4040 case ARM::VLD4d16_UPD:
4041 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00004042 case ARM::VLD1d64Qwb_fixed:
4043 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004044 case ARM::VLD4q8_UPD:
4045 case ARM::VLD4q16_UPD:
4046 case ARM::VLD4q32_UPD:
4047 case ARM::VLD1DUPq8:
4048 case ARM::VLD1DUPq16:
4049 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00004050 case ARM::VLD1DUPq8wb_fixed:
4051 case ARM::VLD1DUPq16wb_fixed:
4052 case ARM::VLD1DUPq32wb_fixed:
4053 case ARM::VLD1DUPq8wb_register:
4054 case ARM::VLD1DUPq16wb_register:
4055 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004056 case ARM::VLD2DUPd8:
4057 case ARM::VLD2DUPd16:
4058 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00004059 case ARM::VLD2DUPd8wb_fixed:
4060 case ARM::VLD2DUPd16wb_fixed:
4061 case ARM::VLD2DUPd32wb_fixed:
4062 case ARM::VLD2DUPd8wb_register:
4063 case ARM::VLD2DUPd16wb_register:
4064 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004065 case ARM::VLD4DUPd8:
4066 case ARM::VLD4DUPd16:
4067 case ARM::VLD4DUPd32:
4068 case ARM::VLD4DUPd8_UPD:
4069 case ARM::VLD4DUPd16_UPD:
4070 case ARM::VLD4DUPd32_UPD:
4071 case ARM::VLD1LNd8:
4072 case ARM::VLD1LNd16:
4073 case ARM::VLD1LNd32:
4074 case ARM::VLD1LNd8_UPD:
4075 case ARM::VLD1LNd16_UPD:
4076 case ARM::VLD1LNd32_UPD:
4077 case ARM::VLD2LNd8:
4078 case ARM::VLD2LNd16:
4079 case ARM::VLD2LNd32:
4080 case ARM::VLD2LNq16:
4081 case ARM::VLD2LNq32:
4082 case ARM::VLD2LNd8_UPD:
4083 case ARM::VLD2LNd16_UPD:
4084 case ARM::VLD2LNd32_UPD:
4085 case ARM::VLD2LNq16_UPD:
4086 case ARM::VLD2LNq32_UPD:
4087 case ARM::VLD4LNd8:
4088 case ARM::VLD4LNd16:
4089 case ARM::VLD4LNd32:
4090 case ARM::VLD4LNq16:
4091 case ARM::VLD4LNq32:
4092 case ARM::VLD4LNd8_UPD:
4093 case ARM::VLD4LNd16_UPD:
4094 case ARM::VLD4LNd32_UPD:
4095 case ARM::VLD4LNq16_UPD:
4096 case ARM::VLD4LNq32_UPD:
4097 // If the address is not 64-bit aligned, the latencies of these
4098 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004099 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004100 break;
4101 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004102 }
4103 return Adjust;
4104}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004105
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004106int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4107 const MachineInstr &DefMI,
4108 unsigned DefIdx,
4109 const MachineInstr &UseMI,
4110 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004111 // No operand latency. The caller may fall back to getInstrLatency.
4112 if (!ItinData || ItinData->isEmpty())
4113 return -1;
4114
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004115 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004116 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004117
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004118 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004119 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004120 if (DefMI.isBundle())
4121 ResolvedDefMI =
4122 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
4123 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
4124 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004125 return 1;
4126 }
4127
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004128 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004129 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004130 if (UseMI.isBundle()) {
4131 ResolvedUseMI =
4132 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
4133 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00004134 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004135 }
4136
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004137 return getOperandLatencyImpl(
4138 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
4139 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
4140}
4141
4142int ARMBaseInstrInfo::getOperandLatencyImpl(
4143 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4144 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
4145 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
4146 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004147 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004148 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004149 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00004150 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004151 }
4152
4153 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004154 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004155 return 0;
4156
4157 // Otherwise it takes the instruction latency (generally one).
4158 unsigned Latency = getInstrLatency(ItinData, DefMI);
4159
4160 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
4161 // its uses. Instructions which are otherwise scheduled between them may
4162 // incur a code size penalty (not able to use the CPSR setting 16-bit
4163 // instructions).
4164 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004165 const MachineFunction *MF = DefMI.getParent()->getParent();
Evandro Menezes85bd3972019-04-04 22:40:06 +00004166 // FIXME: Use Function::hasOptSize().
Matthias Braunf1caa282017-12-15 22:22:58 +00004167 if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004168 --Latency;
4169 }
4170 return Latency;
4171 }
4172
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004173 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00004174 return -1;
4175
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004176 unsigned DefAlign = DefMI.hasOneMemOperand()
4177 ? (*DefMI.memoperands_begin())->getAlignment()
4178 : 0;
4179 unsigned UseAlign = UseMI.hasOneMemOperand()
4180 ? (*UseMI.memoperands_begin())->getAlignment()
4181 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004182
4183 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004184 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
4185 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004186 // Unable to find operand latency. The caller may resort to getInstrLatency.
4187 if (Latency < 0)
4188 return Latency;
4189
4190 // Adjust for IT block position.
4191 int Adj = DefAdj + UseAdj;
4192
4193 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4194 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4195 if (Adj >= 0 || (int)Latency > -Adj) {
4196 return Latency + Adj;
4197 }
4198 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00004199 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004200}
4201
4202int
4203ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4204 SDNode *DefNode, unsigned DefIdx,
4205 SDNode *UseNode, unsigned UseIdx) const {
4206 if (!DefNode->isMachineOpcode())
4207 return 1;
4208
Evan Cheng6cc775f2011-06-28 19:10:37 +00004209 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00004210
Evan Cheng6cc775f2011-06-28 19:10:37 +00004211 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00004212 return 0;
4213
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004214 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004215 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004216
Evan Cheng6c1414f2010-10-29 18:09:28 +00004217 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00004218 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00004219 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4220 int Threshold = 1 + Adj;
4221 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00004222 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004223
Evan Cheng6cc775f2011-06-28 19:10:37 +00004224 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004225 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4226 unsigned DefAlign = !DefMN->memoperands_empty()
4227 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4228 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4229 unsigned UseAlign = !UseMN->memoperands_empty()
4230 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00004231 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4232 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00004233
4234 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00004235 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4236 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00004237 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4238 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00004239 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00004240 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004241 case ARM::LDRrs:
4242 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00004243 unsigned ShOpVal =
4244 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4245 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4246 if (ShImm == 0 ||
4247 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4248 --Latency;
4249 break;
4250 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004251 case ARM::t2LDRs:
4252 case ARM::t2LDRBs:
4253 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00004254 case ARM::t2LDRSHs: {
4255 // Thumb2 mode: lsl only.
4256 unsigned ShAmt =
4257 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4258 if (ShAmt == 0 || ShAmt == 2)
4259 --Latency;
4260 break;
4261 }
4262 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00004263 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4264 // FIXME: Properly handle all of the latency adjustments for address
4265 // writeback.
4266 switch (DefMCID.getOpcode()) {
4267 default: break;
4268 case ARM::LDRrs:
4269 case ARM::LDRBrs: {
4270 unsigned ShOpVal =
4271 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4272 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4273 if (ShImm == 0 ||
4274 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4275 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4276 Latency -= 2;
4277 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4278 --Latency;
4279 break;
4280 }
4281 case ARM::t2LDRs:
4282 case ARM::t2LDRBs:
4283 case ARM::t2LDRHs:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004284 case ARM::t2LDRSHs:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004285 // Thumb2 mode: lsl 0-3 only.
4286 Latency -= 2;
4287 break;
4288 }
Evan Chengff310732010-10-28 06:47:08 +00004289 }
4290
Diana Picus92423ce2016-06-27 09:08:23 +00004291 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004292 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004293 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004294 case ARM::VLD1q8:
4295 case ARM::VLD1q16:
4296 case ARM::VLD1q32:
4297 case ARM::VLD1q64:
4298 case ARM::VLD1q8wb_register:
4299 case ARM::VLD1q16wb_register:
4300 case ARM::VLD1q32wb_register:
4301 case ARM::VLD1q64wb_register:
4302 case ARM::VLD1q8wb_fixed:
4303 case ARM::VLD1q16wb_fixed:
4304 case ARM::VLD1q32wb_fixed:
4305 case ARM::VLD1q64wb_fixed:
4306 case ARM::VLD2d8:
4307 case ARM::VLD2d16:
4308 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004309 case ARM::VLD2q8Pseudo:
4310 case ARM::VLD2q16Pseudo:
4311 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004312 case ARM::VLD2d8wb_fixed:
4313 case ARM::VLD2d16wb_fixed:
4314 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00004315 case ARM::VLD2q8PseudoWB_fixed:
4316 case ARM::VLD2q16PseudoWB_fixed:
4317 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004318 case ARM::VLD2d8wb_register:
4319 case ARM::VLD2d16wb_register:
4320 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00004321 case ARM::VLD2q8PseudoWB_register:
4322 case ARM::VLD2q16PseudoWB_register:
4323 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004324 case ARM::VLD3d8Pseudo:
4325 case ARM::VLD3d16Pseudo:
4326 case ARM::VLD3d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004327 case ARM::VLD1d8TPseudo:
4328 case ARM::VLD1d16TPseudo:
4329 case ARM::VLD1d32TPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004330 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004331 case ARM::VLD1d64TPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004332 case ARM::VLD1d64TPseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004333 case ARM::VLD3d8Pseudo_UPD:
4334 case ARM::VLD3d16Pseudo_UPD:
4335 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004336 case ARM::VLD3q8Pseudo_UPD:
4337 case ARM::VLD3q16Pseudo_UPD:
4338 case ARM::VLD3q32Pseudo_UPD:
4339 case ARM::VLD3q8oddPseudo:
4340 case ARM::VLD3q16oddPseudo:
4341 case ARM::VLD3q32oddPseudo:
4342 case ARM::VLD3q8oddPseudo_UPD:
4343 case ARM::VLD3q16oddPseudo_UPD:
4344 case ARM::VLD3q32oddPseudo_UPD:
4345 case ARM::VLD4d8Pseudo:
4346 case ARM::VLD4d16Pseudo:
4347 case ARM::VLD4d32Pseudo:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004348 case ARM::VLD1d8QPseudo:
4349 case ARM::VLD1d16QPseudo:
4350 case ARM::VLD1d32QPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004351 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004352 case ARM::VLD1d64QPseudoWB_fixed:
Florian Hahn9deef202018-03-02 13:02:55 +00004353 case ARM::VLD1d64QPseudoWB_register:
Ivan A. Kosarev60a991e2018-06-02 16:40:03 +00004354 case ARM::VLD1q8HighQPseudo:
4355 case ARM::VLD1q8LowQPseudo_UPD:
4356 case ARM::VLD1q8HighTPseudo:
4357 case ARM::VLD1q8LowTPseudo_UPD:
4358 case ARM::VLD1q16HighQPseudo:
4359 case ARM::VLD1q16LowQPseudo_UPD:
4360 case ARM::VLD1q16HighTPseudo:
4361 case ARM::VLD1q16LowTPseudo_UPD:
4362 case ARM::VLD1q32HighQPseudo:
4363 case ARM::VLD1q32LowQPseudo_UPD:
4364 case ARM::VLD1q32HighTPseudo:
4365 case ARM::VLD1q32LowTPseudo_UPD:
4366 case ARM::VLD1q64HighQPseudo:
4367 case ARM::VLD1q64LowQPseudo_UPD:
4368 case ARM::VLD1q64HighTPseudo:
4369 case ARM::VLD1q64LowTPseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004370 case ARM::VLD4d8Pseudo_UPD:
4371 case ARM::VLD4d16Pseudo_UPD:
4372 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004373 case ARM::VLD4q8Pseudo_UPD:
4374 case ARM::VLD4q16Pseudo_UPD:
4375 case ARM::VLD4q32Pseudo_UPD:
4376 case ARM::VLD4q8oddPseudo:
4377 case ARM::VLD4q16oddPseudo:
4378 case ARM::VLD4q32oddPseudo:
4379 case ARM::VLD4q8oddPseudo_UPD:
4380 case ARM::VLD4q16oddPseudo_UPD:
4381 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00004382 case ARM::VLD1DUPq8:
4383 case ARM::VLD1DUPq16:
4384 case ARM::VLD1DUPq32:
4385 case ARM::VLD1DUPq8wb_fixed:
4386 case ARM::VLD1DUPq16wb_fixed:
4387 case ARM::VLD1DUPq32wb_fixed:
4388 case ARM::VLD1DUPq8wb_register:
4389 case ARM::VLD1DUPq16wb_register:
4390 case ARM::VLD1DUPq32wb_register:
4391 case ARM::VLD2DUPd8:
4392 case ARM::VLD2DUPd16:
4393 case ARM::VLD2DUPd32:
4394 case ARM::VLD2DUPd8wb_fixed:
4395 case ARM::VLD2DUPd16wb_fixed:
4396 case ARM::VLD2DUPd32wb_fixed:
4397 case ARM::VLD2DUPd8wb_register:
4398 case ARM::VLD2DUPd16wb_register:
4399 case ARM::VLD2DUPd32wb_register:
Ivan A. Kosarev72315982018-06-27 13:57:52 +00004400 case ARM::VLD2DUPq8EvenPseudo:
4401 case ARM::VLD2DUPq8OddPseudo:
4402 case ARM::VLD2DUPq16EvenPseudo:
4403 case ARM::VLD2DUPq16OddPseudo:
4404 case ARM::VLD2DUPq32EvenPseudo:
4405 case ARM::VLD2DUPq32OddPseudo:
4406 case ARM::VLD3DUPq8EvenPseudo:
4407 case ARM::VLD3DUPq8OddPseudo:
4408 case ARM::VLD3DUPq16EvenPseudo:
4409 case ARM::VLD3DUPq16OddPseudo:
4410 case ARM::VLD3DUPq32EvenPseudo:
4411 case ARM::VLD3DUPq32OddPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004412 case ARM::VLD4DUPd8Pseudo:
4413 case ARM::VLD4DUPd16Pseudo:
4414 case ARM::VLD4DUPd32Pseudo:
4415 case ARM::VLD4DUPd8Pseudo_UPD:
4416 case ARM::VLD4DUPd16Pseudo_UPD:
4417 case ARM::VLD4DUPd32Pseudo_UPD:
Ivan A. Kosarev72315982018-06-27 13:57:52 +00004418 case ARM::VLD4DUPq8EvenPseudo:
4419 case ARM::VLD4DUPq8OddPseudo:
4420 case ARM::VLD4DUPq16EvenPseudo:
4421 case ARM::VLD4DUPq16OddPseudo:
4422 case ARM::VLD4DUPq32EvenPseudo:
4423 case ARM::VLD4DUPq32OddPseudo:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004424 case ARM::VLD1LNq8Pseudo:
4425 case ARM::VLD1LNq16Pseudo:
4426 case ARM::VLD1LNq32Pseudo:
4427 case ARM::VLD1LNq8Pseudo_UPD:
4428 case ARM::VLD1LNq16Pseudo_UPD:
4429 case ARM::VLD1LNq32Pseudo_UPD:
4430 case ARM::VLD2LNd8Pseudo:
4431 case ARM::VLD2LNd16Pseudo:
4432 case ARM::VLD2LNd32Pseudo:
4433 case ARM::VLD2LNq16Pseudo:
4434 case ARM::VLD2LNq32Pseudo:
4435 case ARM::VLD2LNd8Pseudo_UPD:
4436 case ARM::VLD2LNd16Pseudo_UPD:
4437 case ARM::VLD2LNd32Pseudo_UPD:
4438 case ARM::VLD2LNq16Pseudo_UPD:
4439 case ARM::VLD2LNq32Pseudo_UPD:
4440 case ARM::VLD4LNd8Pseudo:
4441 case ARM::VLD4LNd16Pseudo:
4442 case ARM::VLD4LNd32Pseudo:
4443 case ARM::VLD4LNq16Pseudo:
4444 case ARM::VLD4LNq32Pseudo:
4445 case ARM::VLD4LNd8Pseudo_UPD:
4446 case ARM::VLD4LNd16Pseudo_UPD:
4447 case ARM::VLD4LNd32Pseudo_UPD:
4448 case ARM::VLD4LNq16Pseudo_UPD:
4449 case ARM::VLD4LNq32Pseudo_UPD:
4450 // If the address is not 64-bit aligned, the latencies of these
4451 // instructions increases by one.
4452 ++Latency;
4453 break;
4454 }
4455
Evan Chengff310732010-10-28 06:47:08 +00004456 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004457}
Evan Cheng63c76082010-10-19 18:58:51 +00004458
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004459unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4460 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4461 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004462 return 0;
4463
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004464 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004465 return 0;
4466
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004467 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004468
Javed Absar4ae7e8122017-06-02 08:53:19 +00004469 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4470 !Subtarget.cheapPredicableCPSRDef())) {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004471 // When predicated, CPSR is an additional source operand for CPSR updating
4472 // instructions, this apparently increases their latencies.
4473 return 1;
4474 }
4475 return 0;
4476}
4477
Andrew Trick45446062012-06-05 21:11:27 +00004478unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004479 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004480 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004481 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4482 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004483 return 1;
4484
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004485 // An instruction scheduler typically runs on unbundled instructions, however
4486 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004487 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004488 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004489 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4490 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004491 while (++I != E && I->isInsideBundle()) {
4492 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004493 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004494 }
4495 return Latency;
4496 }
4497
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004498 const MCInstrDesc &MCID = MI.getDesc();
Javed Absar4ae7e8122017-06-02 08:53:19 +00004499 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4500 !Subtarget.cheapPredicableCPSRDef()))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004501 // When predicated, CPSR is an additional source operand for CPSR updating
4502 // instructions, this apparently increases their latencies.
4503 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004504 }
4505 // Be sure to call getStageLatency for an empty itinerary in case it has a
4506 // valid MinLatency property.
4507 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004508 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004509
4510 unsigned Class = MCID.getSchedClass();
4511
4512 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004513 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004514 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004515
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004516 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004517 unsigned Latency = ItinData->getStageLatency(Class);
4518
4519 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004520 unsigned DefAlign =
4521 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4522 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004523 if (Adj >= 0 || (int)Latency > -Adj) {
4524 return Latency + Adj;
4525 }
4526 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004527}
4528
4529int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4530 SDNode *Node) const {
4531 if (!Node->isMachineOpcode())
4532 return 1;
4533
4534 if (!ItinData || ItinData->isEmpty())
4535 return 1;
4536
4537 unsigned Opcode = Node->getMachineOpcode();
4538 switch (Opcode) {
4539 default:
4540 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004541 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004542 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004543 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004544 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004545}
4546
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004547bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4548 const MachineRegisterInfo *MRI,
4549 const MachineInstr &DefMI,
4550 unsigned DefIdx,
4551 const MachineInstr &UseMI,
4552 unsigned UseIdx) const {
4553 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4554 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004555 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004556 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004557 return true;
4558
4559 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004560 unsigned Latency =
4561 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004562 if (Latency <= 3)
4563 return false;
4564 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4565 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4566}
Evan Chenge96b8d72010-10-26 02:08:50 +00004567
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004568bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4569 const MachineInstr &DefMI,
4570 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004571 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004572 if (!ItinData || ItinData->isEmpty())
4573 return false;
4574
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004575 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004576 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004577 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004578 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4579 return (DefCycle != -1 && DefCycle <= 2);
4580 }
4581 return false;
4582}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004583
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004584bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004585 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004586 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004587 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4588 return false;
4589 }
Eli Friedman68d9a602019-03-15 21:44:49 +00004590 if (MI.getOpcode() == ARM::tMOVr && !Subtarget.hasV6Ops()) {
4591 // Make sure we don't generate a lo-lo mov that isn't supported.
4592 if (!ARM::hGPRRegClass.contains(MI.getOperand(0).getReg()) &&
4593 !ARM::hGPRRegClass.contains(MI.getOperand(1).getReg())) {
4594 ErrInfo = "Non-flag-setting Thumb1 mov is v6-only";
4595 return false;
4596 }
4597 }
4598 if (MI.getOpcode() == ARM::tPUSH ||
4599 MI.getOpcode() == ARM::tPOP ||
4600 MI.getOpcode() == ARM::tPOP_RET) {
4601 for (int i = 2, e = MI.getNumOperands(); i < e; ++i) {
4602 if (MI.getOperand(i).isImplicit() ||
4603 !MI.getOperand(i).isReg())
4604 continue;
4605 unsigned Reg = MI.getOperand(i).getReg();
4606 if (Reg < ARM::R0 || Reg > ARM::R7) {
4607 if (!(MI.getOpcode() == ARM::tPUSH && Reg == ARM::LR) &&
4608 !(MI.getOpcode() == ARM::tPOP_RET && Reg == ARM::PC)) {
4609 ErrInfo = "Unsupported register in Thumb1 push/pop";
4610 return false;
4611 }
4612 }
4613 }
4614 }
Andrew Trick924123a2011-09-21 02:20:46 +00004615 return true;
4616}
4617
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004618// LoadStackGuard has so far only been implemented for MachO. Different code
4619// sequence is needed for other targets.
4620void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4621 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004622 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004623 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4624 "ROPI/RWPI not currently supported with stack guard");
4625
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004626 MachineBasicBlock &MBB = *MI->getParent();
4627 DebugLoc DL = MI->getDebugLoc();
4628 unsigned Reg = MI->getOperand(0).getReg();
4629 const GlobalValue *GV =
4630 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4631 MachineInstrBuilder MIB;
4632
4633 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4634 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4635
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004636 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004637 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4638 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004639 auto Flags = MachineMemOperand::MOLoad |
4640 MachineMemOperand::MODereferenceable |
4641 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004642 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004643 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004644 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004645 }
4646
4647 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004648 MIB.addReg(Reg, RegState::Kill)
Chandler Carruthc73c0302018-08-16 21:30:05 +00004649 .addImm(0)
4650 .cloneMemRefs(*MI)
4651 .add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004652}
4653
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004654bool
4655ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4656 unsigned &AddSubOpc,
4657 bool &NegAcc, bool &HasLane) const {
4658 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4659 if (I == MLxEntryMap.end())
4660 return false;
4661
4662 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4663 MulOpc = Entry.MulOpc;
4664 AddSubOpc = Entry.AddSubOpc;
4665 NegAcc = Entry.NegAcc;
4666 HasLane = Entry.HasLane;
4667 return true;
4668}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004669
4670//===----------------------------------------------------------------------===//
4671// Execution domains.
4672//===----------------------------------------------------------------------===//
4673//
4674// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4675// and some can go down both. The vmov instructions go down the VFP pipeline,
4676// but they can be changed to vorr equivalents that are executed by the NEON
4677// pipeline.
4678//
4679// We use the following execution domain numbering:
4680//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004681enum ARMExeDomain {
4682 ExeGeneric = 0,
4683 ExeVFP = 1,
4684 ExeNEON = 2
4685};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004686
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004687//
4688// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4689//
4690std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004691ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004692 // If we don't have access to NEON instructions then we won't be able
4693 // to swizzle anything to the NEON domain. Check to make sure.
4694 if (Subtarget.hasNEON()) {
4695 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4696 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004697 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004698 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004699
Eric Christopher7e70aba2015-03-07 00:12:22 +00004700 // CortexA9 is particularly picky about mixing the two and wants these
4701 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004702 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4703 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4704 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004705 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4706 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004707 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004708 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004709
4710 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004711 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004712
4713 // Certain instructions can go either way on Cortex-A8.
4714 // Treat them as NEON instructions.
4715 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004716 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004717
4718 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004719 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004720
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004721 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004722}
4723
Tim Northover771f1602012-08-29 16:36:07 +00004724static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4725 unsigned SReg, unsigned &Lane) {
4726 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4727 Lane = 0;
4728
4729 if (DReg != ARM::NoRegister)
4730 return DReg;
4731
4732 Lane = 1;
4733 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4734
4735 assert(DReg && "S-register with no D super-register?");
4736 return DReg;
4737}
4738
Andrew Trickd9296ec2012-10-10 05:43:01 +00004739/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004740/// set ImplicitSReg to a register number that must be marked as implicit-use or
4741/// zero if no register needs to be defined as implicit-use.
4742///
4743/// If the function cannot determine if an SPR should be marked implicit use or
4744/// not, it returns false.
4745///
4746/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004747/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004748/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4749/// lane of the DPR).
4750///
4751/// If the other SPR is defined, an implicit-use of it should be added. Else,
4752/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004753///
James Molloyea052562012-09-18 08:31:15 +00004754static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004755 MachineInstr &MI, unsigned DReg,
4756 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004757 // If the DPR is defined or used already, the other SPR lane will be chained
4758 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004759 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004760 ImplicitSReg = 0;
4761 return true;
4762 }
4763
4764 // Otherwise we need to go searching to see if the SPR is set explicitly.
4765 ImplicitSReg = TRI->getSubReg(DReg,
4766 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4767 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004768 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004769
4770 if (LQR == MachineBasicBlock::LQR_Live)
4771 return true;
4772 else if (LQR == MachineBasicBlock::LQR_Unknown)
4773 return false;
4774
4775 // If the register is known not to be live, there is no need to add an
4776 // implicit-use.
4777 ImplicitSReg = 0;
4778 return true;
4779}
Tim Northover771f1602012-08-29 16:36:07 +00004780
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004781void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4782 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004783 unsigned DstReg, SrcReg, DReg;
4784 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004785 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004786 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004787 switch (MI.getOpcode()) {
4788 default:
4789 llvm_unreachable("cannot handle opcode!");
4790 break;
4791 case ARM::VMOVD:
4792 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004793 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004794
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004795 // Zap the predicate operands.
4796 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004797
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004798 // Make sure we've got NEON instructions.
4799 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004800
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004801 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4802 DstReg = MI.getOperand(0).getReg();
4803 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004804
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004805 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4806 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004807
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004808 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4809 MI.setDesc(get(ARM::VORRd));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004810 MIB.addReg(DstReg, RegState::Define)
4811 .addReg(SrcReg)
4812 .addReg(SrcReg)
4813 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004814 break;
4815 case ARM::VMOVRS:
4816 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004817 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004818 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004819
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004820 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4821 DstReg = MI.getOperand(0).getReg();
4822 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004823
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004824 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4825 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004826
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004827 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004828
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004829 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4830 // Note that DSrc has been widened and the other lane may be undef, which
4831 // contaminates the entire register.
4832 MI.setDesc(get(ARM::VGETLNi32));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004833 MIB.addReg(DstReg, RegState::Define)
4834 .addReg(DReg, RegState::Undef)
4835 .addImm(Lane)
4836 .add(predOps(ARMCC::AL));
Tim Northoverf6618152012-08-17 11:32:52 +00004837
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004838 // The old source should be an implicit use, otherwise we might think it
4839 // was dead before here.
4840 MIB.addReg(SrcReg, RegState::Implicit);
4841 break;
4842 case ARM::VMOVSR: {
4843 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004844 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004845 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004846
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004847 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4848 DstReg = MI.getOperand(0).getReg();
4849 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004850
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004851 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004852
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004853 unsigned ImplicitSReg;
4854 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004855 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004856
4857 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4858 MI.RemoveOperand(i - 1);
4859
4860 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4861 // Again DDst may be undefined at the beginning of this instruction.
4862 MI.setDesc(get(ARM::VSETLNi32));
4863 MIB.addReg(DReg, RegState::Define)
4864 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4865 .addReg(SrcReg)
Diana Picus4f8c3e12017-01-13 09:37:56 +00004866 .addImm(Lane)
4867 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004868
4869 // The narrower destination must be marked as set to keep previous chains
4870 // in place.
4871 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4872 if (ImplicitSReg != 0)
4873 MIB.addReg(ImplicitSReg, RegState::Implicit);
4874 break;
James Molloyea052562012-09-18 08:31:15 +00004875 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004876 case ARM::VMOVS: {
4877 if (Domain != ExeNEON)
4878 break;
4879
4880 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004881 DstReg = MI.getOperand(0).getReg();
4882 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004883
Tim Northoverca9f3842012-08-30 10:17:45 +00004884 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4885 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4886 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4887
James Molloyea052562012-09-18 08:31:15 +00004888 unsigned ImplicitSReg;
4889 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4890 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004891
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004892 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4893 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004894
Tim Northoverca9f3842012-08-30 10:17:45 +00004895 if (DSrc == DDst) {
4896 // Destination can be:
4897 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004898 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004899 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004900 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
Diana Picus4f8c3e12017-01-13 09:37:56 +00004901 .addImm(SrcLane)
4902 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004903
4904 // Neither the source or the destination are naturally represented any
4905 // more, so add them in manually.
4906 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4907 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004908 if (ImplicitSReg != 0)
4909 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004910 break;
4911 }
4912
4913 // In general there's no single instruction that can perform an S <-> S
4914 // move in NEON space, but a pair of VEXT instructions *can* do the
4915 // job. It turns out that the VEXTs needed will only use DSrc once, with
4916 // the position based purely on the combination of lane-0 and lane-1
4917 // involved. For example
4918 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4919 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4920 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4921 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4922 //
4923 // Pattern of the MachineInstrs is:
4924 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4925 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004926 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4927 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004928
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004929 // On the first instruction, both DSrc and DDst may be undef if present.
Tim Northover726d32c2012-09-01 18:07:29 +00004930 // Specifically when the original instruction didn't have them as an
4931 // <imp-use>.
4932 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004933 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004934 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4935
4936 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004937 CurUndef = !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004938 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4939 .addImm(1)
4940 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004941
4942 if (SrcLane == DstLane)
4943 NewMIB.addReg(SrcReg, RegState::Implicit);
4944
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004945 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004946 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004947
4948 // On the second instruction, DDst has definitely been defined above, so
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00004949 // it is not undef. DSrc, if present, can be undef as above.
Tim Northover726d32c2012-09-01 18:07:29 +00004950 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004951 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004952 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4953
4954 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004955 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004956 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4957 .addImm(1)
4958 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004959
4960 if (SrcLane != DstLane)
4961 MIB.addReg(SrcReg, RegState::Implicit);
4962
4963 // As before, the original destination is no longer represented, add it
4964 // implicitly.
4965 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004966 if (ImplicitSReg != 0)
4967 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004968 break;
4969 }
Tim Northoverf6618152012-08-17 11:32:52 +00004970 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004971}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004972
Bob Wilsone8a549c2012-09-29 21:43:49 +00004973//===----------------------------------------------------------------------===//
4974// Partial register updates
4975//===----------------------------------------------------------------------===//
4976//
4977// Swift renames NEON registers with 64-bit granularity. That means any
4978// instruction writing an S-reg implicitly reads the containing D-reg. The
4979// problem is mostly avoided by translating f32 operations to v2f32 operations
4980// on D-registers, but f32 loads are still a problem.
4981//
4982// These instructions can load an f32 into a NEON register:
4983//
4984// VLDRS - Only writes S, partial D update.
4985// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4986// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4987//
4988// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004989unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4990 const MachineInstr &MI, unsigned OpNum,
4991 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004992 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4993 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004994 return 0;
4995
4996 assert(TRI && "Need TRI instance");
4997
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004998 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004999 if (MO.readsReg())
5000 return 0;
5001 unsigned Reg = MO.getReg();
5002 int UseOp = -1;
5003
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005004 switch (MI.getOpcode()) {
5005 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00005006 case ARM::VLDRS:
5007 case ARM::FCONSTS:
5008 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00005009 case ARM::VMOVv8i8:
5010 case ARM::VMOVv4i16:
5011 case ARM::VMOVv2i32:
5012 case ARM::VMOVv2f32:
5013 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005014 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005015 break;
5016
5017 // Explicitly reads the dependency.
5018 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00005019 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00005020 break;
5021 default:
5022 return 0;
5023 }
5024
5025 // If this instruction actually reads a value from Reg, there is no unwanted
5026 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005027 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00005028 return 0;
5029
5030 // We must be able to clobber the whole D-reg.
5031 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00005032 // Virtual register must be a def undef foo:ssub_0 operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005033 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00005034 return 0;
5035 } else if (ARM::SPRRegClass.contains(Reg)) {
5036 // Physical register: MI must define the full D-reg.
5037 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
5038 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005039 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00005040 return 0;
5041 }
5042
5043 // MI has an unwanted D-register dependency.
5044 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00005045 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00005046}
5047
5048// Break a partial register dependency after getPartialRegUpdateClearance
5049// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005050void ARMBaseInstrInfo::breakPartialRegDependency(
5051 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
5052 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00005053 assert(TRI && "Need TRI instance");
5054
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005055 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005056 unsigned Reg = MO.getReg();
5057 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
5058 "Can't break virtual register dependencies.");
5059 unsigned DReg = Reg;
5060
5061 // If MI defines an S-reg, find the corresponding D super-register.
5062 if (ARM::SPRRegClass.contains(Reg)) {
5063 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
5064 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
5065 }
5066
5067 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005068 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00005069
5070 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
5071 // the full D-register by loading the same value to both lanes. The
5072 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00005073 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00005074 // too big regressions.
5075
5076 // Insert the dependency-breaking FCONSTD before MI.
5077 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Diana Picus4f8c3e12017-01-13 09:37:56 +00005078 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
5079 .addImm(96)
5080 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00005081 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00005082}
5083
Jim Grosbach617f84dd2012-02-28 23:53:30 +00005084bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00005085 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00005086}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00005087
5088bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00005089 if (MI->getNumOperands() < 4)
5090 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00005091 unsigned ShOpVal = MI->getOperand(3).getImm();
5092 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
5093 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
5094 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
5095 ((ShImm == 1 || ShImm == 2) &&
5096 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
5097 return true;
5098
5099 return false;
5100}
Quentin Colombetd358e842014-08-22 18:05:22 +00005101
5102bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
5103 const MachineInstr &MI, unsigned DefIdx,
5104 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
5105 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5106 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
5107
5108 switch (MI.getOpcode()) {
5109 case ARM::VMOVDRR:
5110 // dX = VMOVDRR rY, rZ
5111 // is the same as:
5112 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
5113 // Populate the InputRegs accordingly.
5114 // rY
5115 const MachineOperand *MOReg = &MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00005116 if (!MOReg->isUndef())
5117 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5118 MOReg->getSubReg(), ARM::ssub_0));
Quentin Colombetd358e842014-08-22 18:05:22 +00005119 // rZ
5120 MOReg = &MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005121 if (!MOReg->isUndef())
5122 InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
5123 MOReg->getSubReg(), ARM::ssub_1));
Quentin Colombetd358e842014-08-22 18:05:22 +00005124 return true;
5125 }
5126 llvm_unreachable("Target dependent opcode missing");
5127}
5128
5129bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
5130 const MachineInstr &MI, unsigned DefIdx,
5131 RegSubRegPairAndIdx &InputReg) const {
5132 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5133 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
5134
5135 switch (MI.getOpcode()) {
5136 case ARM::VMOVRRD:
5137 // rX, rY = VMOVRRD dZ
5138 // is the same as:
5139 // rX = EXTRACT_SUBREG dZ, ssub_0
5140 // rY = EXTRACT_SUBREG dZ, ssub_1
5141 const MachineOperand &MOReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005142 if (MOReg.isUndef())
5143 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005144 InputReg.Reg = MOReg.getReg();
5145 InputReg.SubReg = MOReg.getSubReg();
5146 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
5147 return true;
5148 }
5149 llvm_unreachable("Target dependent opcode missing");
5150}
5151
5152bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
5153 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
5154 RegSubRegPairAndIdx &InsertedReg) const {
5155 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
5156 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
5157
5158 switch (MI.getOpcode()) {
5159 case ARM::VSETLNi32:
5160 // dX = VSETLNi32 dY, rZ, imm
5161 const MachineOperand &MOBaseReg = MI.getOperand(1);
5162 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00005163 if (MOInsertedReg.isUndef())
5164 return false;
Quentin Colombetd358e842014-08-22 18:05:22 +00005165 const MachineOperand &MOIndex = MI.getOperand(3);
5166 BaseReg.Reg = MOBaseReg.getReg();
5167 BaseReg.SubReg = MOBaseReg.getSubReg();
5168
5169 InsertedReg.Reg = MOInsertedReg.getReg();
5170 InsertedReg.SubReg = MOInsertedReg.getSubReg();
5171 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
5172 return true;
5173 }
5174 llvm_unreachable("Target dependent opcode missing");
5175}
Martin Storsjod3b29222018-08-22 20:34:06 +00005176
5177std::pair<unsigned, unsigned>
5178ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
5179 const unsigned Mask = ARMII::MO_OPTION_MASK;
5180 return std::make_pair(TF & Mask, TF & ~Mask);
5181}
5182
5183ArrayRef<std::pair<unsigned, const char *>>
5184ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
5185 using namespace ARMII;
5186
5187 static const std::pair<unsigned, const char *> TargetFlags[] = {
5188 {MO_LO16, "arm-lo16"}, {MO_HI16, "arm-hi16"}};
5189 return makeArrayRef(TargetFlags);
5190}
5191
5192ArrayRef<std::pair<unsigned, const char *>>
5193ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
5194 using namespace ARMII;
5195
5196 static const std::pair<unsigned, const char *> TargetFlags[] = {
Martin Storsjo2dcaa412018-08-31 08:00:25 +00005197 {MO_COFFSTUB, "arm-coffstub"},
Martin Storsjod3b29222018-08-22 20:34:06 +00005198 {MO_GOT, "arm-got"},
5199 {MO_SBREL, "arm-sbrel"},
5200 {MO_DLLIMPORT, "arm-dllimport"},
5201 {MO_SECREL, "arm-secrel"},
5202 {MO_NONLAZY, "arm-nonlazy"}};
5203 return makeArrayRef(TargetFlags);
5204}
David Greenc519d3c2019-04-23 12:11:26 +00005205
5206bool llvm::registerDefinedBetween(unsigned Reg,
5207 MachineBasicBlock::iterator From,
5208 MachineBasicBlock::iterator To,
5209 const TargetRegisterInfo *TRI) {
5210 for (auto I = From; I != To; ++I)
5211 if (I->modifiesRegister(Reg, TRI))
5212 return true;
5213 return false;
5214}
5215
5216MachineInstr *llvm::findCMPToFoldIntoCBZ(MachineInstr *Br,
5217 const TargetRegisterInfo *TRI) {
5218 // Search backwards to the instruction that defines CSPR. This may or not
5219 // be a CMP, we check that after this loop. If we find another instruction
5220 // that reads cpsr, we return nullptr.
5221 MachineBasicBlock::iterator CmpMI = Br;
5222 while (CmpMI != Br->getParent()->begin()) {
5223 --CmpMI;
5224 if (CmpMI->modifiesRegister(ARM::CPSR, TRI))
5225 break;
5226 if (CmpMI->readsRegister(ARM::CPSR, TRI))
5227 break;
5228 }
5229
5230 // Check that this inst is a CMP r[0-7], #0 and that the register
5231 // is not redefined between the cmp and the br.
5232 if (CmpMI->getOpcode() != ARM::tCMPi8 && CmpMI->getOpcode() != ARM::t2CMPri)
5233 return nullptr;
5234 unsigned Reg = CmpMI->getOperand(0).getReg();
5235 unsigned PredReg = 0;
5236 ARMCC::CondCodes Pred = getInstrPredicate(*CmpMI, PredReg);
5237 if (Pred != ARMCC::AL || CmpMI->getOperand(1).getImm() != 0)
5238 return nullptr;
5239 if (!isARMLowRegister(Reg))
5240 return nullptr;
5241 if (registerDefinedBetween(Reg, CmpMI->getNextNode(), Br, TRI))
5242 return nullptr;
5243
5244 return &*CmpMI;
5245}