blob: 3688db943d54161ca649be32a9890b31dcbd8975 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Amara Emerson52cfb6a2013-10-03 09:31:51 +000014#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000015#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000016#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000017#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000018#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000019#include "ARMMachineFunctionInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000020#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000022#include "MCTargetDesc/ARMBaseInfo.h"
23#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000025#include "llvm/ADT/SmallSet.h"
26#include "llvm/ADT/SmallVector.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000027#include "llvm/ADT/Triple.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000028#include "llvm/CodeGen/LiveVariables.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000030#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstr.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000035#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000036#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000038#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Evan Chenga20cde32011-07-20 23:34:39 +000039#include "llvm/CodeGen/SelectionDAGNodes.h"
Matthias Braun88e21312015-06-13 03:42:11 +000040#include "llvm/CodeGen/TargetSchedule.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000041#include "llvm/IR/Attributes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Constants.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000043#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/Function.h"
45#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000046#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000047#include "llvm/MC/MCInstrDesc.h"
48#include "llvm/MC/MCInstrItineraries.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000049#include "llvm/Support/BranchProbability.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000050#include "llvm/Support/Casting.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000051#include "llvm/Support/CommandLine.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000052#include "llvm/Support/Compiler.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000053#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000054#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000055#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +000056#include "llvm/Target/TargetInstrInfo.h"
57#include "llvm/Target/TargetMachine.h"
58#include "llvm/Target/TargetRegisterInfo.h"
59#include <algorithm>
60#include <cassert>
61#include <cstdint>
62#include <iterator>
63#include <new>
64#include <utility>
65#include <vector>
Evan Cheng1e210d02011-06-28 20:07:07 +000066
David Goodwinaf7451b2009-07-08 16:09:28 +000067using namespace llvm;
68
Chandler Carruthe96dd892014-04-21 22:55:11 +000069#define DEBUG_TYPE "arm-instrinfo"
70
Chandler Carruthd174b722014-04-22 02:03:14 +000071#define GET_INSTRINFO_CTOR_DTOR
72#include "ARMGenInstrInfo.inc"
73
David Goodwinaf7451b2009-07-08 16:09:28 +000074static cl::opt<bool>
75EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
76 cl::desc("Enable ARM 2-addr to 3-addr conv"));
77
Evan Cheng62c7b5b2010-12-05 22:04:16 +000078/// ARM_MLxEntry - Record information about MLA / MLS instructions.
79struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000080 uint16_t MLxOpc; // MLA / MLS opcode
81 uint16_t MulOpc; // Expanded multiplication opcode
82 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000083 bool NegAcc; // True if the acc is negated before the add / sub.
84 bool HasLane; // True if instruction has an extra "lane" operand.
85};
86
87static const ARM_MLxEntry ARM_MLxTable[] = {
88 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
89 // fp scalar ops
90 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
91 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
92 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
93 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000094 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
95 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
96 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
97 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
98
99 // fp SIMD ops
100 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
101 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
102 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
103 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
104 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
105 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
106 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
107 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
108};
109
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000110ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +0000111 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +0000112 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000113 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
114 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +0000115 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000116 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
117 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
118 }
119}
120
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000121// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
122// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +0000123ScheduleHazardRecognizer *
124ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
125 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000126 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000127 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000128 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000129 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
130 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000131 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000132}
133
134ScheduleHazardRecognizer *ARMBaseInstrInfo::
135CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
136 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000137 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000138 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000139 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000140}
141
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000142MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
143 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000144 // FIXME: Thumb2 support.
145
David Goodwinaf7451b2009-07-08 16:09:28 +0000146 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000147 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000148
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000149 MachineFunction &MF = *MI.getParent()->getParent();
150 uint64_t TSFlags = MI.getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000151 bool isPre = false;
152 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000153 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000154 case ARMII::IndexModePre:
155 isPre = true;
156 break;
157 case ARMII::IndexModePost:
158 break;
159 }
160
161 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
162 // operation.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000163 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
David Goodwinaf7451b2009-07-08 16:09:28 +0000164 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000165 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000166
Craig Topper062a2ba2014-04-25 05:30:21 +0000167 MachineInstr *UpdateMI = nullptr;
168 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000169 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000170 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000171 unsigned NumOps = MCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000172 bool isLoad = !MI.mayStore();
173 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
174 const MachineOperand &Base = MI.getOperand(2);
175 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 unsigned WBReg = WB.getReg();
177 unsigned BaseReg = Base.getReg();
178 unsigned OffReg = Offset.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000179 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
180 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
David Goodwinaf7451b2009-07-08 16:09:28 +0000181 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000182 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000183 case ARMII::AddrMode2: {
184 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
185 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
186 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000187 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000188 // Can't encode it in a so_imm operand. This transformation will
189 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000190 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000191 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000192 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000193 .addReg(BaseReg)
194 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000195 .add(predOps(Pred))
196 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000197 } else if (Amt != 0) {
198 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
199 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000200 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000201 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000202 .addReg(BaseReg)
203 .addReg(OffReg)
204 .addReg(0)
205 .addImm(SOOpc)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000206 .add(predOps(Pred))
207 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 } else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000209 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000210 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000211 .addReg(BaseReg)
212 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000213 .add(predOps(Pred))
214 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000215 break;
216 }
217 case ARMII::AddrMode3 : {
218 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
219 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
220 if (OffReg == 0)
221 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000222 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000223 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000224 .addReg(BaseReg)
225 .addImm(Amt)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000226 .add(predOps(Pred))
227 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000228 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000229 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000230 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000231 .addReg(BaseReg)
232 .addReg(OffReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000233 .add(predOps(Pred))
234 .add(condCodeOp());
David Goodwinaf7451b2009-07-08 16:09:28 +0000235 break;
236 }
237 }
238
239 std::vector<MachineInstr*> NewMIs;
240 if (isPre) {
241 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000242 MemMI =
243 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
244 .addReg(WBReg)
245 .addImm(0)
246 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000247 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
249 .addReg(MI.getOperand(1).getReg())
250 .addReg(WBReg)
251 .addReg(0)
252 .addImm(0)
253 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000254 NewMIs.push_back(MemMI);
255 NewMIs.push_back(UpdateMI);
256 } else {
257 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000258 MemMI =
259 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
260 .addReg(BaseReg)
261 .addImm(0)
262 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000263 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000264 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
265 .addReg(MI.getOperand(1).getReg())
266 .addReg(BaseReg)
267 .addReg(0)
268 .addImm(0)
269 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000270 if (WB.isDead())
271 UpdateMI->getOperand(0).setIsDead();
272 NewMIs.push_back(UpdateMI);
273 NewMIs.push_back(MemMI);
274 }
275
276 // Transfer LiveVariables states, kill / dead info.
277 if (LV) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000278 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
279 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000280 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000281 unsigned Reg = MO.getReg();
282
283 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
284 if (MO.isDef()) {
285 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
286 if (MO.isDead())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000287 LV->addVirtualRegisterDead(Reg, *NewMI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000288 }
289 if (MO.isUse() && MO.isKill()) {
290 for (unsigned j = 0; j < 2; ++j) {
291 // Look at the two new MI's in reverse order.
292 MachineInstr *NewMI = NewMIs[j];
293 if (!NewMI->readsRegister(Reg))
294 continue;
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000295 LV->addVirtualRegisterKilled(Reg, *NewMI);
296 if (VI.removeKill(MI))
David Goodwinaf7451b2009-07-08 16:09:28 +0000297 VI.Kills.push_back(NewMI);
298 break;
299 }
300 }
301 }
302 }
303 }
304
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000305 MachineBasicBlock::iterator MBBI = MI.getIterator();
David Goodwinaf7451b2009-07-08 16:09:28 +0000306 MFI->insert(MBBI, NewMIs[1]);
307 MFI->insert(MBBI, NewMIs[0]);
308 return NewMIs[0];
309}
310
311// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000312bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
313 MachineBasicBlock *&TBB,
314 MachineBasicBlock *&FBB,
315 SmallVectorImpl<MachineOperand> &Cond,
316 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000317 TBB = nullptr;
318 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000319
David Goodwinaf7451b2009-07-08 16:09:28 +0000320 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000321 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000322 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000323 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000324
325 // Walk backwards from the end of the basic block until the branch is
326 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000327 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000328 // Flag to be raised on unanalyzeable instructions. This is useful in cases
329 // where we want to clean up on the end of the basic block before we bail
330 // out.
331 bool CantAnalyze = false;
332
333 // Skip over DEBUG values and predicated nonterminators.
334 while (I->isDebugValue() || !I->isTerminator()) {
335 if (I == MBB.begin())
336 return false;
337 --I;
338 }
339
340 if (isIndirectBranchOpcode(I->getOpcode()) ||
341 isJumpTableBranchOpcode(I->getOpcode())) {
342 // Indirect branches and jump tables can't be analyzed, but we still want
343 // to clean up any instructions at the tail of the basic block.
344 CantAnalyze = true;
345 } else if (isUncondBranchOpcode(I->getOpcode())) {
346 TBB = I->getOperand(0).getMBB();
347 } else if (isCondBranchOpcode(I->getOpcode())) {
348 // Bail out if we encounter multiple conditional branches.
349 if (!Cond.empty())
350 return true;
351
352 assert(!FBB && "FBB should have been null.");
353 FBB = TBB;
354 TBB = I->getOperand(0).getMBB();
355 Cond.push_back(I->getOperand(1));
356 Cond.push_back(I->getOperand(2));
357 } else if (I->isReturn()) {
358 // Returns can't be analyzed, but we should run cleanup.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000359 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000360 } else {
361 // We encountered other unrecognized terminator. Bail out immediately.
362 return true;
363 }
364
365 // Cleanup code - to be run for unpredicated unconditional branches and
366 // returns.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000367 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000368 (isUncondBranchOpcode(I->getOpcode()) ||
369 isIndirectBranchOpcode(I->getOpcode()) ||
370 isJumpTableBranchOpcode(I->getOpcode()) ||
371 I->isReturn())) {
372 // Forget any previous condition branch information - it no longer applies.
373 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000374 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000375
376 // If we can modify the function, delete everything below this
377 // unconditional branch.
378 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000379 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000380 while (DI != MBB.end()) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000381 MachineInstr &InstToDelete = *DI;
Lang Hames24864fe2013-07-19 23:52:47 +0000382 ++DI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000383 InstToDelete.eraseFromParent();
Lang Hames24864fe2013-07-19 23:52:47 +0000384 }
385 }
386 }
387
388 if (CantAnalyze)
389 return true;
390
Dale Johannesen4244d122010-04-02 01:38:09 +0000391 if (I == MBB.begin())
392 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000393
Dale Johannesen4244d122010-04-02 01:38:09 +0000394 --I;
395 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000396
Lang Hames24864fe2013-07-19 23:52:47 +0000397 // We made it past the terminators without bailing out - we must have
398 // analyzed this branch successfully.
399 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000400}
401
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000402unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000403 int *BytesRemoved) const {
404 assert(!BytesRemoved && "code size not handled");
405
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000406 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
407 if (I == MBB.end())
408 return 0;
409
Evan Cheng056c6692009-07-27 18:20:05 +0000410 if (!isUncondBranchOpcode(I->getOpcode()) &&
411 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000412 return 0;
413
414 // Remove the branch.
415 I->eraseFromParent();
416
417 I = MBB.end();
418
419 if (I == MBB.begin()) return 1;
420 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000421 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000422 return 1;
423
424 // Remove the branch.
425 I->eraseFromParent();
426 return 2;
427}
428
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000429unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000430 MachineBasicBlock *TBB,
431 MachineBasicBlock *FBB,
432 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000433 const DebugLoc &DL,
434 int *BytesAdded) const {
435 assert(!BytesAdded && "code size not handled");
Evan Cheng780748d2009-07-28 05:48:47 +0000436 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
437 int BOpc = !AFI->isThumbFunction()
438 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
439 int BccOpc = !AFI->isThumbFunction()
440 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000441 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000442
David Goodwinaf7451b2009-07-08 16:09:28 +0000443 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000444 assert(TBB && "insertBranch must not be told to insert a fallthrough");
David Goodwinaf7451b2009-07-08 16:09:28 +0000445 assert((Cond.size() == 2 || Cond.size() == 0) &&
446 "ARM branch conditions have two components!");
447
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000448 // For conditional branches, we use addOperand to preserve CPSR flags.
449
Craig Topper062a2ba2014-04-25 05:30:21 +0000450 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000451 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000452 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000453 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000454 else
455 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000456 } else
Diana Picus116bbab2017-01-13 09:58:52 +0000457 BuildMI(&MBB, DL, get(BccOpc))
458 .addMBB(TBB)
459 .addImm(Cond[0].getImm())
460 .add(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000461 return 1;
462 }
463
464 // Two-way conditional branch.
Diana Picus116bbab2017-01-13 09:58:52 +0000465 BuildMI(&MBB, DL, get(BccOpc))
466 .addMBB(TBB)
467 .addImm(Cond[0].getImm())
468 .add(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000469 if (isThumb)
Diana Picusbd66b7d2017-01-20 08:15:24 +0000470 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000471 else
472 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000473 return 2;
474}
475
476bool ARMBaseInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000477reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000478 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
479 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
480 return false;
481}
482
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000483bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
484 if (MI.isBundle()) {
485 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
486 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000487 while (++I != E && I->isInsideBundle()) {
488 int PIdx = I->findFirstPredOperandIdx();
489 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
490 return true;
491 }
492 return false;
493 }
494
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000495 int PIdx = MI.findFirstPredOperandIdx();
496 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000497}
498
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000499bool ARMBaseInstrInfo::PredicateInstruction(
500 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
501 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000502 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000503 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
504 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000505 .addImm(Pred[0].getImm())
506 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000507 return true;
508 }
509
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000510 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000511 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000512 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000513 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000514 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000515 return true;
516 }
517 return false;
518}
519
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000520bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
521 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000522 if (Pred1.size() > 2 || Pred2.size() > 2)
523 return false;
524
525 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
526 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
527 if (CC1 == CC2)
528 return true;
529
530 switch (CC1) {
531 default:
532 return false;
533 case ARMCC::AL:
534 return true;
535 case ARMCC::HS:
536 return CC2 == ARMCC::HI;
537 case ARMCC::LS:
538 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
539 case ARMCC::GE:
540 return CC2 == ARMCC::GT;
541 case ARMCC::LE:
542 return CC2 == ARMCC::LT;
543 }
544}
545
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000546bool ARMBaseInstrInfo::DefinesPredicate(
547 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000548 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000549 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
550 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000551 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
552 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000553 Pred.push_back(MO);
554 Found = true;
555 }
556 }
557
558 return Found;
559}
560
Javed Absar4ae7e8122017-06-02 08:53:19 +0000561bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
562 for (const auto &MO : MI.operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000563 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000564 return true;
565 return false;
566}
567
Javed Absar4ae7e8122017-06-02 08:53:19 +0000568bool ARMBaseInstrInfo::isAddrMode3OpImm(const MachineInstr &MI,
569 unsigned Op) const {
570 const MachineOperand &Offset = MI.getOperand(Op + 1);
571 return Offset.getReg() != 0;
572}
573
574// Load with negative register offset requires additional 1cyc and +I unit
575// for Cortex A57
576bool ARMBaseInstrInfo::isAddrMode3OpMinusReg(const MachineInstr &MI,
577 unsigned Op) const {
578 const MachineOperand &Offset = MI.getOperand(Op + 1);
579 const MachineOperand &Opc = MI.getOperand(Op + 2);
580 assert(Opc.isImm());
581 assert(Offset.isReg());
582 int64_t OpcImm = Opc.getImm();
583
584 bool isSub = ARM_AM::getAM3Op(OpcImm) == ARM_AM::sub;
585 return (isSub && Offset.getReg() != 0);
586}
587
588bool ARMBaseInstrInfo::isLdstScaledReg(const MachineInstr &MI,
589 unsigned Op) const {
590 const MachineOperand &Opc = MI.getOperand(Op + 2);
591 unsigned OffImm = Opc.getImm();
592 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
593}
594
595// Load, scaled register offset, not plus LSL2
596bool ARMBaseInstrInfo::isLdstScaledRegNotPlusLsl2(const MachineInstr &MI,
597 unsigned Op) const {
598 const MachineOperand &Opc = MI.getOperand(Op + 2);
599 unsigned OffImm = Opc.getImm();
600
601 bool isAdd = ARM_AM::getAM2Op(OffImm) == ARM_AM::add;
602 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
603 ARM_AM::ShiftOpc ShiftOpc = ARM_AM::getAM2ShiftOpc(OffImm);
604 if (ShiftOpc == ARM_AM::no_shift) return false; // not scaled
605 bool SimpleScaled = (isAdd && ShiftOpc == ARM_AM::lsl && Amt == 2);
606 return !SimpleScaled;
607}
608
609// Minus reg for ldstso addr mode
610bool ARMBaseInstrInfo::isLdstSoMinusReg(const MachineInstr &MI,
611 unsigned Op) const {
612 unsigned OffImm = MI.getOperand(Op + 2).getImm();
613 return ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
614}
615
616// Load, scaled register offset
617bool ARMBaseInstrInfo::isAm2ScaledReg(const MachineInstr &MI,
618 unsigned Op) const {
619 unsigned OffImm = MI.getOperand(Op + 2).getImm();
620 return ARM_AM::getAM2ShiftOpc(OffImm) != ARM_AM::no_shift;
621}
622
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000623static bool isEligibleForITBlock(const MachineInstr *MI) {
624 switch (MI->getOpcode()) {
625 default: return true;
626 case ARM::tADC: // ADC (register) T1
627 case ARM::tADDi3: // ADD (immediate) T1
628 case ARM::tADDi8: // ADD (immediate) T2
629 case ARM::tADDrr: // ADD (register) T1
630 case ARM::tAND: // AND (register) T1
631 case ARM::tASRri: // ASR (immediate) T1
632 case ARM::tASRrr: // ASR (register) T1
633 case ARM::tBIC: // BIC (register) T1
634 case ARM::tEOR: // EOR (register) T1
635 case ARM::tLSLri: // LSL (immediate) T1
636 case ARM::tLSLrr: // LSL (register) T1
637 case ARM::tLSRri: // LSR (immediate) T1
638 case ARM::tLSRrr: // LSR (register) T1
639 case ARM::tMUL: // MUL T1
640 case ARM::tMVN: // MVN (register) T1
641 case ARM::tORR: // ORR (register) T1
642 case ARM::tROR: // ROR (register) T1
643 case ARM::tRSB: // RSB (immediate) T1
644 case ARM::tSBC: // SBC (register) T1
645 case ARM::tSUBi3: // SUB (immediate) T1
646 case ARM::tSUBi8: // SUB (immediate) T2
647 case ARM::tSUBrr: // SUB (register) T1
Javed Absar4ae7e8122017-06-02 08:53:19 +0000648 return !ARMBaseInstrInfo::isCPSRDefined(*MI);
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000649 }
650}
651
Evan Chenga33fc862009-11-21 06:21:52 +0000652/// isPredicable - Return true if the specified instruction can be predicated.
653/// By default, this returns true for every instruction with a
654/// PredicateOperand.
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000655bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000656 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000657 return false;
658
Saleem Abdulrasoolbfa25bd2016-09-06 04:00:12 +0000659 if (MI.isBundle())
660 return false;
661
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000662 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000663 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000664
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000665 const ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000666 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000667
Kristof Beyls96652492017-06-22 12:11:38 +0000668 // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
669 // In their ARM encoding, they can't be encoded in a conditional form.
670 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
671 return false;
672
Joey Goulya5153cb2013-09-09 14:21:49 +0000673 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000674 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000675 return isV8EligibleForIT(&MI);
Evan Chenga33fc862009-11-21 06:21:52 +0000676 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000677
Evan Chenga33fc862009-11-21 06:21:52 +0000678 return true;
679}
David Goodwinaf7451b2009-07-08 16:09:28 +0000680
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000681namespace llvm {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000682
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000683template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000684 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
685 const MachineOperand &MO = MI->getOperand(i);
686 if (!MO.isReg() || MO.isUndef() || MO.isUse())
687 continue;
688 if (MO.getReg() != ARM::CPSR)
689 continue;
690 if (!MO.isDead())
691 return false;
692 }
693 // all definitions of CPSR are dead
694 return true;
695}
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +0000696
697} // end namespace llvm
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000698
David Goodwinaf7451b2009-07-08 16:09:28 +0000699/// GetInstSize - Return the size of the specified MachineInstr.
700///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000701unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000702 const MachineBasicBlock &MBB = *MI.getParent();
David Goodwinaf7451b2009-07-08 16:09:28 +0000703 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000704 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000705
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000706 const MCInstrDesc &MCID = MI.getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000707 if (MCID.getSize())
708 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000709
David Blaikie46a9f012012-01-20 21:51:11 +0000710 // If this machine instr is an inline asm, measure it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000711 if (MI.getOpcode() == ARM::INLINEASM)
712 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
713 unsigned Opc = MI.getOpcode();
David Blaikie46a9f012012-01-20 21:51:11 +0000714 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000715 default:
716 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000717 return 0;
718 case TargetOpcode::BUNDLE:
719 return getInstBundleLength(MI);
720 case ARM::MOVi16_ga_pcrel:
721 case ARM::MOVTi16_ga_pcrel:
722 case ARM::t2MOVi16_ga_pcrel:
723 case ARM::t2MOVTi16_ga_pcrel:
724 return 4;
725 case ARM::MOVi32imm:
726 case ARM::t2MOVi32imm:
727 return 8;
728 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000729 case ARM::JUMPTABLE_INSTS:
730 case ARM::JUMPTABLE_ADDRS:
731 case ARM::JUMPTABLE_TBB:
732 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000733 // If this machine instr is a constant pool entry, its size is recorded as
734 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000735 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000736 case ARM::Int_eh_sjlj_longjmp:
737 return 16;
738 case ARM::tInt_eh_sjlj_longjmp:
739 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000740 case ARM::tInt_WIN_eh_sjlj_longjmp:
741 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000742 case ARM::Int_eh_sjlj_setjmp:
743 case ARM::Int_eh_sjlj_setjmp_nofp:
744 return 20;
745 case ARM::tInt_eh_sjlj_setjmp:
746 case ARM::t2Int_eh_sjlj_setjmp:
747 case ARM::t2Int_eh_sjlj_setjmp_nofp:
748 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000749 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000750 return MI.getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000751 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000752}
753
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000754unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000755 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000756 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
757 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000758 while (++I != E && I->isInsideBundle()) {
759 assert(!I->isBundle() && "No nested bundle!");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000760 Size += getInstSizeInBytes(*I);
Evan Cheng7fae11b2011-12-14 02:11:42 +0000761 }
762 return Size;
763}
764
Tim Northover5d72c5d2014-10-01 19:21:03 +0000765void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
766 MachineBasicBlock::iterator I,
767 unsigned DestReg, bool KillSrc,
768 const ARMSubtarget &Subtarget) const {
769 unsigned Opc = Subtarget.isThumb()
770 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
771 : ARM::MRS;
772
773 MachineInstrBuilder MIB =
774 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
775
776 // There is only 1 A/R class MRS instruction, and it always refers to
777 // APSR. However, there are lots of other possibilities on M-class cores.
778 if (Subtarget.isMClass())
779 MIB.addImm(0x800);
780
Diana Picus4f8c3e12017-01-13 09:37:56 +0000781 MIB.add(predOps(ARMCC::AL))
782 .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
Tim Northover5d72c5d2014-10-01 19:21:03 +0000783}
784
785void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
786 MachineBasicBlock::iterator I,
787 unsigned SrcReg, bool KillSrc,
788 const ARMSubtarget &Subtarget) const {
789 unsigned Opc = Subtarget.isThumb()
790 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
791 : ARM::MSR;
792
793 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
794
795 if (Subtarget.isMClass())
796 MIB.addImm(0x800);
797 else
798 MIB.addImm(8);
799
Diana Picus4f8c3e12017-01-13 09:37:56 +0000800 MIB.addReg(SrcReg, getKillRegState(KillSrc))
801 .add(predOps(ARMCC::AL))
802 .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
Tim Northover5d72c5d2014-10-01 19:21:03 +0000803}
804
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000805void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000806 MachineBasicBlock::iterator I,
807 const DebugLoc &DL, unsigned DestReg,
808 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000809 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000810 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000811
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000812 if (GPRDest && GPRSrc) {
Diana Picus8a73f552017-01-13 10:18:01 +0000813 BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
814 .addReg(SrcReg, getKillRegState(KillSrc))
815 .add(predOps(ARMCC::AL))
816 .add(condCodeOp());
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000817 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000818 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000819
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000820 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000821 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000822
Chad Rosierbe762512011-08-20 00:17:25 +0000823 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000824 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000825 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000826 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000827 Opc = ARM::VMOVRS;
828 else if (SPRDest && GPRSrc)
829 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000830 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000831 Opc = ARM::VMOVD;
832 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000833 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000834
Chad Rosierbe762512011-08-20 00:17:25 +0000835 if (Opc) {
836 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000837 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000838 if (Opc == ARM::VORRq)
839 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Diana Picus4f8c3e12017-01-13 09:37:56 +0000840 MIB.add(predOps(ARMCC::AL));
Chad Rosierbe762512011-08-20 00:17:25 +0000841 return;
842 }
843
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000844 // Handle register classes that require multiple instructions.
845 unsigned BeginIdx = 0;
846 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000847 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000848
849 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000850 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
851 Opc = ARM::VORRq;
852 BeginIdx = ARM::qsub_0;
853 SubRegs = 2;
854 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
855 Opc = ARM::VORRq;
856 BeginIdx = ARM::qsub_0;
857 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000858 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000859 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
860 Opc = ARM::VMOVD;
861 BeginIdx = ARM::dsub_0;
862 SubRegs = 2;
863 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
864 Opc = ARM::VMOVD;
865 BeginIdx = ARM::dsub_0;
866 SubRegs = 3;
867 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
868 Opc = ARM::VMOVD;
869 BeginIdx = ARM::dsub_0;
870 SubRegs = 4;
871 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000872 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000873 BeginIdx = ARM::gsub_0;
874 SubRegs = 2;
875 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
876 Opc = ARM::VMOVD;
877 BeginIdx = ARM::dsub_0;
878 SubRegs = 2;
879 Spacing = 2;
880 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
881 Opc = ARM::VMOVD;
882 BeginIdx = ARM::dsub_0;
883 SubRegs = 3;
884 Spacing = 2;
885 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
886 Opc = ARM::VMOVD;
887 BeginIdx = ARM::dsub_0;
888 SubRegs = 4;
889 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000890 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
891 Opc = ARM::VMOVS;
892 BeginIdx = ARM::ssub_0;
893 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000894 } else if (SrcReg == ARM::CPSR) {
895 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
896 return;
897 } else if (DestReg == ARM::CPSR) {
898 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
899 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000900 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000901
Andrew Trickb57e2252012-08-29 04:41:37 +0000902 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000903
Andrew Trick4cc69492012-08-29 01:58:52 +0000904 const TargetRegisterInfo *TRI = &getRegisterInfo();
905 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000906
907 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
908 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000909 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000910 Spacing = -Spacing;
911 }
912#ifndef NDEBUG
913 SmallSet<unsigned, 4> DstRegs;
914#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000915 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000916 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
917 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000918 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000919#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000920 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000921 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000922#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000923 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000924 // VORR takes two source operands.
925 if (Opc == ARM::VORRq)
926 Mov.addReg(Src);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000927 Mov = Mov.add(predOps(ARMCC::AL));
JF Bastien583db652013-07-12 23:33:03 +0000928 // MOVr can set CC.
929 if (Opc == ARM::MOVr)
Diana Picus8a73f552017-01-13 10:18:01 +0000930 Mov = Mov.add(condCodeOp());
Andrew Trick4cc69492012-08-29 01:58:52 +0000931 }
932 // Add implicit super-register defs and kills to the last instruction.
933 Mov->addRegisterDefined(DestReg, TRI);
934 if (KillSrc)
935 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000936}
937
Tim Northover798697d2013-04-21 11:57:07 +0000938const MachineInstrBuilder &
939ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
940 unsigned SubIdx, unsigned State,
941 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000942 if (!SubIdx)
943 return MIB.addReg(Reg, State);
944
945 if (TargetRegisterInfo::isPhysicalRegister(Reg))
946 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
947 return MIB.addReg(Reg, State, SubIdx);
948}
949
David Goodwinaf7451b2009-07-08 16:09:28 +0000950void ARMBaseInstrInfo::
951storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
952 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000953 const TargetRegisterClass *RC,
954 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000955 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000956 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000957 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000958 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000959 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000960
Alex Lorenze40c8a22015-08-11 23:09:45 +0000961 MachineMemOperand *MMO = MF.getMachineMemOperand(
962 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
963 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000964
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000965 switch (TRI->getSpillSize(*RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000966 case 4:
967 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000968 BuildMI(MBB, I, DL, get(ARM::STRi12))
969 .addReg(SrcReg, getKillRegState(isKill))
970 .addFrameIndex(FI)
971 .addImm(0)
972 .addMemOperand(MMO)
973 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +0000974 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000975 BuildMI(MBB, I, DL, get(ARM::VSTRS))
976 .addReg(SrcReg, getKillRegState(isKill))
977 .addFrameIndex(FI)
978 .addImm(0)
979 .addMemOperand(MMO)
980 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +0000981 } else
982 llvm_unreachable("Unknown reg class!");
983 break;
984 case 8:
985 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +0000986 BuildMI(MBB, I, DL, get(ARM::VSTRD))
987 .addReg(SrcReg, getKillRegState(isKill))
988 .addFrameIndex(FI)
989 .addImm(0)
990 .addMemOperand(MMO)
991 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000992 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000993 if (Subtarget.hasV5TEOps()) {
994 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
995 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
996 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000997 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
998 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +0000999 } else {
1000 // Fallback to STM instruction, which has existed since the dawn of
1001 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001002 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STMIA))
1003 .addFrameIndex(FI)
1004 .addMemOperand(MMO)
1005 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001006 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
1007 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
1008 }
Owen Anderson732f82c2011-08-10 17:21:20 +00001009 } else
1010 llvm_unreachable("Unknown reg class!");
1011 break;
1012 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001013 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001014 // Use aligned spills if the stack can be realigned.
1015 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001016 BuildMI(MBB, I, DL, get(ARM::VST1q64))
1017 .addFrameIndex(FI)
1018 .addImm(16)
1019 .addReg(SrcReg, getKillRegState(isKill))
1020 .addMemOperand(MMO)
1021 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001022 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001023 BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
1024 .addReg(SrcReg, getKillRegState(isKill))
1025 .addFrameIndex(FI)
1026 .addMemOperand(MMO)
1027 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001028 }
1029 } else
1030 llvm_unreachable("Unknown reg class!");
1031 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001032 case 24:
1033 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1034 // Use aligned spills if the stack can be realigned.
1035 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001036 BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
1037 .addFrameIndex(FI)
1038 .addImm(16)
1039 .addReg(SrcReg, getKillRegState(isKill))
1040 .addMemOperand(MMO)
1041 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001042 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001043 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1044 .addFrameIndex(FI)
1045 .add(predOps(ARMCC::AL))
1046 .addMemOperand(MMO);
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001047 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1048 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1049 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1050 }
1051 } else
1052 llvm_unreachable("Unknown reg class!");
1053 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001054 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001055 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001056 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1057 // FIXME: It's possible to only store part of the QQ register if the
1058 // spilled def has a sub-register index.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001059 BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
1060 .addFrameIndex(FI)
1061 .addImm(16)
1062 .addReg(SrcReg, getKillRegState(isKill))
1063 .addMemOperand(MMO)
1064 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001065 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001066 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1067 .addFrameIndex(FI)
1068 .add(predOps(ARMCC::AL))
1069 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001070 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1071 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1072 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1073 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1074 }
1075 } else
1076 llvm_unreachable("Unknown reg class!");
1077 break;
1078 case 64:
1079 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001080 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
1081 .addFrameIndex(FI)
1082 .add(predOps(ARMCC::AL))
1083 .addMemOperand(MMO);
Owen Anderson732f82c2011-08-10 17:21:20 +00001084 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
1085 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
1086 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
1087 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
1088 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
1089 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
1090 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
1091 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
1092 } else
1093 llvm_unreachable("Unknown reg class!");
1094 break;
1095 default:
1096 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001097 }
1098}
1099
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001100unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1101 int &FrameIndex) const {
1102 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001103 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001104 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001105 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001106 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1107 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1108 MI.getOperand(3).getImm() == 0) {
1109 FrameIndex = MI.getOperand(1).getIndex();
1110 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001111 }
1112 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001113 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001114 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001115 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001116 case ARM::VSTRD:
1117 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001118 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1119 MI.getOperand(2).getImm() == 0) {
1120 FrameIndex = MI.getOperand(1).getIndex();
1121 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001122 }
1123 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001124 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001125 case ARM::VST1d64TPseudo:
1126 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001127 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1128 FrameIndex = MI.getOperand(0).getIndex();
1129 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001130 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001131 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001132 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001133 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1134 FrameIndex = MI.getOperand(1).getIndex();
1135 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001136 }
1137 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001138 }
1139
1140 return 0;
1141}
1142
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001143unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001144 int &FrameIndex) const {
1145 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001146 return MI.mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001147}
1148
David Goodwinaf7451b2009-07-08 16:09:28 +00001149void ARMBaseInstrInfo::
1150loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1151 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001152 const TargetRegisterClass *RC,
1153 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001154 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001155 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001156 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001157 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001158 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001159 MachineMemOperand *MMO = MF.getMachineMemOperand(
1160 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1161 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001162
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001163 switch (TRI->getSpillSize(*RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001164 case 4:
1165 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001166 BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1167 .addFrameIndex(FI)
1168 .addImm(0)
1169 .addMemOperand(MMO)
1170 .add(predOps(ARMCC::AL));
Bob Wilson37f106e2010-02-16 22:01:59 +00001171
Owen Anderson732f82c2011-08-10 17:21:20 +00001172 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001173 BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
1174 .addFrameIndex(FI)
1175 .addImm(0)
1176 .addMemOperand(MMO)
1177 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001178 } else
1179 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001180 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001181 case 8:
1182 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001183 BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
1184 .addFrameIndex(FI)
1185 .addImm(0)
1186 .addMemOperand(MMO)
1187 .add(predOps(ARMCC::AL));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001188 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001189 MachineInstrBuilder MIB;
1190
1191 if (Subtarget.hasV5TEOps()) {
1192 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1193 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1194 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001195 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
1196 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001197 } else {
1198 // Fallback to LDM instruction, which has existed since the dawn of
1199 // time.
Diana Picus4f8c3e12017-01-13 09:37:56 +00001200 MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
1201 .addFrameIndex(FI)
1202 .addMemOperand(MMO)
1203 .add(predOps(ARMCC::AL));
Tim Northover798697d2013-04-21 11:57:07 +00001204 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1205 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1206 }
1207
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001208 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1209 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001210 } else
1211 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001212 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001213 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001214 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001215 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001216 BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
1217 .addFrameIndex(FI)
1218 .addImm(16)
1219 .addMemOperand(MMO)
1220 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001221 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001222 BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1223 .addFrameIndex(FI)
1224 .addMemOperand(MMO)
1225 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001226 }
1227 } else
1228 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001229 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001230 case 24:
1231 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1232 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001233 BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1234 .addFrameIndex(FI)
1235 .addImm(16)
1236 .addMemOperand(MMO)
1237 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001238 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001239 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1240 .addFrameIndex(FI)
1241 .addMemOperand(MMO)
1242 .add(predOps(ARMCC::AL));
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001243 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1244 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1245 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1246 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1247 MIB.addReg(DestReg, RegState::ImplicitDefine);
1248 }
1249 } else
1250 llvm_unreachable("Unknown reg class!");
1251 break;
1252 case 32:
1253 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001254 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001255 BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
1256 .addFrameIndex(FI)
1257 .addImm(16)
1258 .addMemOperand(MMO)
1259 .add(predOps(ARMCC::AL));
Owen Anderson732f82c2011-08-10 17:21:20 +00001260 } else {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001261 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1262 .addFrameIndex(FI)
1263 .add(predOps(ARMCC::AL))
1264 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001265 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1266 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1267 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1268 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001269 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1270 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001271 }
1272 } else
1273 llvm_unreachable("Unknown reg class!");
1274 break;
1275 case 64:
1276 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
Diana Picus4f8c3e12017-01-13 09:37:56 +00001277 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1278 .addFrameIndex(FI)
1279 .add(predOps(ARMCC::AL))
1280 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001281 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1282 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1283 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1284 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1285 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1286 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1287 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1288 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001289 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1290 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001291 } else
1292 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001293 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001294 default:
1295 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001296 }
1297}
1298
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001299unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1300 int &FrameIndex) const {
1301 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001302 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001303 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001304 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001305 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1306 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1307 MI.getOperand(3).getImm() == 0) {
1308 FrameIndex = MI.getOperand(1).getIndex();
1309 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001310 }
1311 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001312 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001313 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001314 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001315 case ARM::VLDRD:
1316 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001317 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1318 MI.getOperand(2).getImm() == 0) {
1319 FrameIndex = MI.getOperand(1).getIndex();
1320 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001321 }
1322 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001323 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001324 case ARM::VLD1d64TPseudo:
1325 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001326 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1327 FrameIndex = MI.getOperand(1).getIndex();
1328 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001329 }
1330 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001331 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001332 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1333 FrameIndex = MI.getOperand(1).getIndex();
1334 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001335 }
1336 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001337 }
1338
1339 return 0;
1340}
1341
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001342unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1343 int &FrameIndex) const {
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001344 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001345 return MI.mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001346}
1347
Scott Douglass953f9082015-10-05 14:49:54 +00001348/// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1349/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001350void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001351 bool isThumb1 = Subtarget.isThumb1Only();
1352 bool isThumb2 = Subtarget.isThumb2();
1353 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1354
Scott Douglass953f9082015-10-05 14:49:54 +00001355 DebugLoc dl = MI->getDebugLoc();
1356 MachineBasicBlock *BB = MI->getParent();
1357
1358 MachineInstrBuilder LDM, STM;
1359 if (isThumb1 || !MI->getOperand(1).isDead()) {
1360 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1361 : isThumb1 ? ARM::tLDMIA_UPD
1362 : ARM::LDMIA_UPD))
Diana Picus116bbab2017-01-13 09:58:52 +00001363 .add(MI->getOperand(1));
Scott Douglass953f9082015-10-05 14:49:54 +00001364 } else {
1365 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1366 }
1367
1368 if (isThumb1 || !MI->getOperand(0).isDead()) {
1369 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1370 : isThumb1 ? ARM::tSTMIA_UPD
1371 : ARM::STMIA_UPD))
Diana Picus116bbab2017-01-13 09:58:52 +00001372 .add(MI->getOperand(0));
Scott Douglass953f9082015-10-05 14:49:54 +00001373 } else {
1374 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1375 }
1376
Diana Picus116bbab2017-01-13 09:58:52 +00001377 LDM.add(MI->getOperand(3)).add(predOps(ARMCC::AL));
1378 STM.add(MI->getOperand(2)).add(predOps(ARMCC::AL));
Scott Douglass953f9082015-10-05 14:49:54 +00001379
1380 // Sort the scratch registers into ascending order.
1381 const TargetRegisterInfo &TRI = getRegisterInfo();
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00001382 SmallVector<unsigned, 6> ScratchRegs;
Scott Douglass953f9082015-10-05 14:49:54 +00001383 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1384 ScratchRegs.push_back(MI->getOperand(I).getReg());
1385 std::sort(ScratchRegs.begin(), ScratchRegs.end(),
1386 [&TRI](const unsigned &Reg1,
1387 const unsigned &Reg2) -> bool {
1388 return TRI.getEncodingValue(Reg1) <
1389 TRI.getEncodingValue(Reg2);
1390 });
1391
1392 for (const auto &Reg : ScratchRegs) {
1393 LDM.addReg(Reg, RegState::Define);
1394 STM.addReg(Reg, RegState::Kill);
1395 }
1396
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001397 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001398}
1399
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001400bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1401 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001402 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001403 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001404 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001405 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001406 return true;
1407 }
1408
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001409 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001410 expandMEMCPY(MI);
1411 return true;
1412 }
1413
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001414 // This hook gets to expand COPY instructions before they become
1415 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1416 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1417 // changed into a VORR that can go down the NEON pipeline.
Diana Picusb772e402016-07-06 11:22:11 +00001418 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001419 return false;
1420
1421 // Look for a copy between even S-registers. That is where we keep floats
1422 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001423 unsigned DstRegS = MI.getOperand(0).getReg();
1424 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001425 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1426 return false;
1427
1428 const TargetRegisterInfo *TRI = &getRegisterInfo();
1429 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1430 &ARM::DPRRegClass);
1431 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1432 &ARM::DPRRegClass);
1433 if (!DstRegD || !SrcRegD)
1434 return false;
1435
1436 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1437 // legal if the COPY already defines the full DstRegD, and it isn't a
1438 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001439 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001440 return false;
1441
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001442 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001443 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001444 return false;
1445
1446 // All clear, widen the COPY.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001447 DEBUG(dbgs() << "widening: " << MI);
1448 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001449
1450 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1451 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001452 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001453 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001454 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001455
1456 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001457 MI.setDesc(get(ARM::VMOVD));
1458 MI.getOperand(0).setReg(DstRegD);
1459 MI.getOperand(1).setReg(SrcRegD);
Diana Picus4f8c3e12017-01-13 09:37:56 +00001460 MIB.add(predOps(ARMCC::AL));
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001461
1462 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1463 // register scavenger and machine verifier, so we need to indicate that we
1464 // are reading an undefined value from SrcRegD, but a proper value from
1465 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001466 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001467 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001468
1469 // SrcRegD may actually contain an unrelated value in the ssub_1
1470 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001471 if (MI.getOperand(1).isKill()) {
1472 MI.getOperand(1).setIsKill(false);
1473 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001474 }
1475
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001476 DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001477 return true;
1478}
1479
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001480/// Create a copy of a const pool value. Update CPI to the new index and return
1481/// the label UID.
1482static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1483 MachineConstantPool *MCP = MF.getConstantPool();
1484 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1485
1486 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1487 assert(MCPE.isMachineConstantPoolEntry() &&
1488 "Expecting a machine constantpool entry!");
1489 ARMConstantPoolValue *ACPV =
1490 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1491
Evan Chengdfce83c2011-01-17 08:03:18 +00001492 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001493 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001494
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001495 // FIXME: The below assumes PIC relocation model and that the function
1496 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1497 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1498 // instructions, so that's probably OK, but is PIC always correct when
1499 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001500 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001501 NewCPV = ARMConstantPoolConstant::Create(
1502 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1503 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001504 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001505 NewCPV = ARMConstantPoolSymbol::
1506 Create(MF.getFunction()->getContext(),
1507 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001508 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001509 NewCPV = ARMConstantPoolConstant::
1510 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1511 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001512 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001513 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1514 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001515 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001516 NewCPV = ARMConstantPoolMBB::
1517 Create(MF.getFunction()->getContext(),
1518 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001519 else
1520 llvm_unreachable("Unexpected ARM constantpool value type!!");
1521 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1522 return PCLabelId;
1523}
1524
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001525void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1526 MachineBasicBlock::iterator I,
1527 unsigned DestReg, unsigned SubIdx,
1528 const MachineInstr &Orig,
1529 const TargetRegisterInfo &TRI) const {
1530 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001531 switch (Opcode) {
1532 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001533 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1534 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001535 MBB.insert(I, MI);
1536 break;
1537 }
1538 case ARM::tLDRpci_pic:
1539 case ARM::t2LDRpci_pic: {
1540 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001541 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001542 unsigned PCLabelId = duplicateCPV(MF, CPI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001543 MachineInstrBuilder MIB =
1544 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1545 .addConstantPoolIndex(CPI)
1546 .addImm(PCLabelId);
1547 MIB->setMemRefs(Orig.memoperands_begin(), Orig.memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001548 break;
1549 }
1550 }
Evan Chengfe864422009-11-08 00:15:23 +00001551}
1552
Matthias Braun55bc9b32017-08-22 23:56:30 +00001553MachineInstr &
1554ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
1555 MachineBasicBlock::iterator InsertBefore,
1556 const MachineInstr &Orig) const {
1557 MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
1558 MachineBasicBlock::instr_iterator I = Cloned.getIterator();
1559 for (;;) {
1560 switch (I->getOpcode()) {
1561 case ARM::tLDRpci_pic:
1562 case ARM::t2LDRpci_pic: {
1563 MachineFunction &MF = *MBB.getParent();
1564 unsigned CPI = I->getOperand(1).getIndex();
1565 unsigned PCLabelId = duplicateCPV(MF, CPI);
1566 I->getOperand(1).setIndex(CPI);
1567 I->getOperand(2).setImm(PCLabelId);
1568 break;
1569 }
1570 }
1571 if (!I->isBundledWithSucc())
1572 break;
1573 ++I;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001574 }
Matthias Braun55bc9b32017-08-22 23:56:30 +00001575 return Cloned;
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001576}
1577
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001578bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1579 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001580 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001581 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001582 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001583 Opcode == ARM::t2LDRpci_pic ||
1584 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001585 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001586 Opcode == ARM::LDRLIT_ga_pcrel ||
1587 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1588 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001589 Opcode == ARM::MOV_ga_pcrel ||
1590 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001591 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001592 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001593 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001594 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001595 return false;
1596
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001597 const MachineOperand &MO0 = MI0.getOperand(1);
1598 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001599 if (MO0.getOffset() != MO1.getOffset())
1600 return false;
1601
Tim Northover72360d22013-12-02 10:35:41 +00001602 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1603 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1604 Opcode == ARM::tLDRLIT_ga_pcrel ||
1605 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001606 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001607 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001608 // Ignore the PC labels.
1609 return MO0.getGlobal() == MO1.getGlobal();
1610
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001611 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001612 const MachineConstantPool *MCP = MF->getConstantPool();
1613 int CPI0 = MO0.getIndex();
1614 int CPI1 = MO1.getIndex();
1615 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1616 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001617 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1618 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1619 if (isARMCP0 && isARMCP1) {
1620 ARMConstantPoolValue *ACPV0 =
1621 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1622 ARMConstantPoolValue *ACPV1 =
1623 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1624 return ACPV0->hasSameValue(ACPV1);
1625 } else if (!isARMCP0 && !isARMCP1) {
1626 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1627 }
1628 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001629 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001630 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001631 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001632 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001633 return false;
1634
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001635 unsigned Addr0 = MI0.getOperand(1).getReg();
1636 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001637 if (Addr0 != Addr1) {
1638 if (!MRI ||
1639 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1640 !TargetRegisterInfo::isVirtualRegister(Addr1))
1641 return false;
1642
1643 // This assumes SSA form.
1644 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1645 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1646 // Check if the loaded value, e.g. a constantpool of a global address, are
1647 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001648 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001649 return false;
1650 }
1651
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001652 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Evan Chengb8b0ad82011-01-20 08:34:58 +00001653 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001654 const MachineOperand &MO0 = MI0.getOperand(i);
1655 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001656 if (!MO0.isIdenticalTo(MO1))
1657 return false;
1658 }
1659 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001660 }
1661
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001662 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001663}
1664
Bill Wendlingf4707472010-06-23 23:00:16 +00001665/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1666/// determine if two loads are loading from the same base address. It should
1667/// only return true if the base pointers are the same and the only differences
1668/// between the two addresses is the offset. It also returns the offsets by
1669/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001670///
1671/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1672/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001673bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1674 int64_t &Offset1,
1675 int64_t &Offset2) const {
1676 // Don't worry about Thumb: just ARM and Thumb2.
1677 if (Subtarget.isThumb1Only()) return false;
1678
1679 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1680 return false;
1681
1682 switch (Load1->getMachineOpcode()) {
1683 default:
1684 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001685 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001686 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001687 case ARM::LDRD:
1688 case ARM::LDRH:
1689 case ARM::LDRSB:
1690 case ARM::LDRSH:
1691 case ARM::VLDRD:
1692 case ARM::VLDRS:
1693 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001694 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001695 case ARM::t2LDRDi8:
1696 case ARM::t2LDRSHi8:
1697 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001698 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001699 case ARM::t2LDRSHi12:
1700 break;
1701 }
1702
1703 switch (Load2->getMachineOpcode()) {
1704 default:
1705 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001706 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001707 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001708 case ARM::LDRD:
1709 case ARM::LDRH:
1710 case ARM::LDRSB:
1711 case ARM::LDRSH:
1712 case ARM::VLDRD:
1713 case ARM::VLDRS:
1714 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001715 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001716 case ARM::t2LDRSHi8:
1717 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001718 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001719 case ARM::t2LDRSHi12:
1720 break;
1721 }
1722
1723 // Check if base addresses and chain operands match.
1724 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1725 Load1->getOperand(4) != Load2->getOperand(4))
1726 return false;
1727
1728 // Index should be Reg0.
1729 if (Load1->getOperand(3) != Load2->getOperand(3))
1730 return false;
1731
1732 // Determine the offsets.
1733 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1734 isa<ConstantSDNode>(Load2->getOperand(1))) {
1735 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1736 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1737 return true;
1738 }
1739
1740 return false;
1741}
1742
1743/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001744/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001745/// be scheduled togther. On some targets if two loads are loading from
1746/// addresses in the same cache line, it's better if they are scheduled
1747/// together. This function takes two integers that represent the load offsets
1748/// from the common base address. It returns true if it decides it's desirable
1749/// to schedule the two loads together. "NumLoads" is the number of loads that
1750/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001751///
1752/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1753/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001754bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1755 int64_t Offset1, int64_t Offset2,
1756 unsigned NumLoads) const {
1757 // Don't worry about Thumb: just ARM and Thumb2.
1758 if (Subtarget.isThumb1Only()) return false;
1759
1760 assert(Offset2 > Offset1);
1761
1762 if ((Offset2 - Offset1) / 8 > 64)
1763 return false;
1764
Renato Golinb184cd92013-08-14 16:35:29 +00001765 // Check if the machine opcodes are different. If they are different
1766 // then we consider them to not be of the same base address,
1767 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1768 // In this case, they are considered to be the same because they are different
1769 // encoding forms of the same basic instruction.
1770 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1771 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1772 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1773 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1774 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001775 return false; // FIXME: overly conservative?
1776
1777 // Four loads in a row should be sufficient.
1778 if (NumLoads >= 3)
1779 return false;
1780
1781 return true;
1782}
1783
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001784bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001785 const MachineBasicBlock *MBB,
1786 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001787 // Debug info is never a scheduling boundary. It's necessary to be explicit
1788 // due to the special treatment of IT instructions below, otherwise a
1789 // dbg_value followed by an IT will result in the IT instruction being
1790 // considered a scheduling hazard, which is wrong. It should be the actual
1791 // instruction preceding the dbg_value instruction(s), just like it is
1792 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001793 if (MI.isDebugValue())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001794 return false;
1795
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001796 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001797 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001798 return true;
1799
1800 // Treat the start of the IT block as a scheduling boundary, but schedule
1801 // t2IT along with all instructions following it.
1802 // FIXME: This is a big hammer. But the alternative is to add all potential
1803 // true and anti dependencies to IT block instructions as implicit operands
1804 // to the t2IT instruction. The added compile time and complexity does not
1805 // seem worth it.
1806 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001807 // Make sure to skip any dbg_value instructions
1808 while (++I != MBB->end() && I->isDebugValue())
1809 ;
1810 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001811 return true;
1812
1813 // Don't attempt to schedule around any instruction that defines
1814 // a stack-oriented pointer, as it's unlikely to be profitable. This
1815 // saves compile time, because it doesn't require every single
1816 // stack slot reference to depend on the instruction that does the
1817 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001818 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001819 // No ARM calling conventions change the stack pointer. (X86 calling
1820 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001821 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001822 return true;
1823
1824 return false;
1825}
1826
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001827bool ARMBaseInstrInfo::
1828isProfitableToIfCvt(MachineBasicBlock &MBB,
1829 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001830 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001831 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001832 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001833
Peter Collingbourne65295232015-04-23 20:31:30 +00001834 // If we are optimizing for size, see if the branch in the predecessor can be
1835 // lowered to cbn?z by the constant island lowering pass, and return false if
1836 // so. This results in a shorter instruction sequence.
Sanjay Patel924879a2015-08-04 15:49:57 +00001837 if (MBB.getParent()->getFunction()->optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001838 MachineBasicBlock *Pred = *MBB.pred_begin();
1839 if (!Pred->empty()) {
1840 MachineInstr *LastMI = &*Pred->rbegin();
1841 if (LastMI->getOpcode() == ARM::t2Bcc) {
1842 MachineBasicBlock::iterator CmpMI = LastMI;
1843 if (CmpMI != Pred->begin()) {
1844 --CmpMI;
1845 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1846 CmpMI->getOpcode() == ARM::t2CMPri) {
1847 unsigned Reg = CmpMI->getOperand(0).getReg();
1848 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001849 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
Peter Collingbourne65295232015-04-23 20:31:30 +00001850 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1851 isARMLowRegister(Reg))
1852 return false;
1853 }
1854 }
1855 }
1856 }
1857 }
Artyom Skrobov283316b2017-03-14 13:38:45 +00001858 return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
1859 MBB, 0, 0, Probability);
Evan Cheng02b184d2010-06-25 22:42:03 +00001860}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001861
Evan Cheng02b184d2010-06-25 22:42:03 +00001862bool ARMBaseInstrInfo::
John Brawn75d76e52017-06-28 14:11:15 +00001863isProfitableToIfCvt(MachineBasicBlock &TBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001864 unsigned TCycles, unsigned TExtra,
John Brawn75d76e52017-06-28 14:11:15 +00001865 MachineBasicBlock &FBB,
Evan Chengdebf9c52010-11-03 00:45:17 +00001866 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001867 BranchProbability Probability) const {
Artyom Skrobov283316b2017-03-14 13:38:45 +00001868 if (!TCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001869 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001870
Owen Anderson88af7d02010-09-28 18:32:13 +00001871 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001872 // Here we scale up each component of UnpredCost to avoid precision issue when
1873 // scaling TCycles/FCycles by Probability.
1874 const unsigned ScalingUpFactor = 1024;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001875
John Brawn75d76e52017-06-28 14:11:15 +00001876 unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
1877 unsigned UnpredCost;
1878 if (!Subtarget.hasBranchPredictor()) {
1879 // When we don't have a branch predictor it's always cheaper to not take a
1880 // branch than take it, so we have to take that into account.
1881 unsigned NotTakenBranchCost = 1;
1882 unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
1883 unsigned TUnpredCycles, FUnpredCycles;
1884 if (!FCycles) {
1885 // Triangle: TBB is the fallthrough
1886 TUnpredCycles = TCycles + NotTakenBranchCost;
1887 FUnpredCycles = TakenBranchCost;
1888 } else {
1889 // Diamond: TBB is the block that is branched to, FBB is the fallthrough
1890 TUnpredCycles = TCycles + TakenBranchCost;
1891 FUnpredCycles = FCycles + NotTakenBranchCost;
John Brawn97cc2832017-07-12 13:23:10 +00001892 // The branch at the end of FBB will disappear when it's predicated, so
1893 // discount it from PredCost.
1894 PredCost -= 1 * ScalingUpFactor;
John Brawn75d76e52017-06-28 14:11:15 +00001895 }
1896 // The total cost is the cost of each path scaled by their probabilites
1897 unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
1898 unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
1899 UnpredCost = TUnpredCost + FUnpredCost;
1900 // When predicating assume that the first IT can be folded away but later
1901 // ones cost one cycle each
1902 if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
1903 PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
1904 }
1905 } else {
1906 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1907 unsigned FUnpredCost =
1908 Probability.getCompl().scale(FCycles * ScalingUpFactor);
1909 UnpredCost = TUnpredCost + FUnpredCost;
1910 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1911 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
1912 }
1913
1914 return PredCost <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001915}
1916
Bob Wilsone8a549c2012-09-29 21:43:49 +00001917bool
1918ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1919 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001920 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001921 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001922 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001923}
1924
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001925/// getInstrPredicate - If instruction is predicated, returns its predicate
1926/// condition, otherwise returns AL. It also returns the condition code
1927/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001928ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1929 unsigned &PredReg) {
1930 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001931 if (PIdx == -1) {
1932 PredReg = 0;
1933 return ARMCC::AL;
1934 }
1935
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001936 PredReg = MI.getOperand(PIdx+1).getReg();
1937 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001938}
1939
Matthias Braunfa3872e2015-05-18 20:27:55 +00001940unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001941 if (Opc == ARM::B)
1942 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001943 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001944 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001945 if (Opc == ARM::t2B)
1946 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001947
1948 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001949}
1950
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001951MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001952 bool NewMI,
1953 unsigned OpIdx1,
1954 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001955 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001956 case ARM::MOVCCr:
1957 case ARM::t2MOVCCr: {
1958 // MOVCC can be commuted by inverting the condition.
1959 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001960 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001961 // MOVCC AL can't be inverted. Shouldn't happen.
1962 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001963 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001964 MachineInstr *CommutedMI =
1965 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1966 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001967 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001968 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001969 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
1970 .setImm(ARMCC::getOppositeCondition(CC));
1971 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001972 }
1973 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001974 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001975}
Evan Cheng780748d2009-07-28 05:48:47 +00001976
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001977/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001978/// return the defining instruction.
1979static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1980 const MachineRegisterInfo &MRI,
1981 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001982 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001983 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001984 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001985 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001986 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001987 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001988 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001989 // MI is folded into the MOVCC by predicating it.
1990 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001991 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001992 // Check if MI has any non-dead defs or physreg uses. This also detects
1993 // predicated instructions which will be reading CPSR.
1994 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1995 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001996 // Reject frame index operands, PEI can't handle the predicated pseudos.
1997 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001998 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001999 if (!MO.isReg())
2000 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002001 // MI can't have any tied operands, that would conflict with predication.
2002 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00002003 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002004 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00002005 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002006 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00002007 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002008 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002009 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00002010 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00002011 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002012 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00002013}
2014
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002015bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002016 SmallVectorImpl<MachineOperand> &Cond,
2017 unsigned &TrueOp, unsigned &FalseOp,
2018 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002019 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002020 "Unknown select instruction");
2021 // MOVCC operands:
2022 // 0: Def.
2023 // 1: True use.
2024 // 2: False use.
2025 // 3: Condition code.
2026 // 4: CPSR use.
2027 TrueOp = 1;
2028 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002029 Cond.push_back(MI.getOperand(3));
2030 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002031 // We can always fold a def.
2032 Optimizable = true;
2033 return false;
2034}
2035
Mehdi Amini22e59742015-01-13 07:07:13 +00002036MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002037ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00002038 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
2039 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002040 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002041 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002042 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2043 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002044 bool Invert = !DefMI;
2045 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002046 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002047 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00002048 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002049
Matthias Braun2f169f92013-10-04 16:52:56 +00002050 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002051 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2052 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00002053 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
2054 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00002055 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00002056
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002057 // Create a new predicated version of DefMI.
2058 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002059 MachineInstrBuilder NewMI =
2060 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002061
2062 // Copy all the DefMI operands, excluding its (null) predicate.
2063 const MCInstrDesc &DefDesc = DefMI->getDesc();
2064 for (unsigned i = 1, e = DefDesc.getNumOperands();
2065 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00002066 NewMI.add(DefMI->getOperand(i));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002067
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002068 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002069 if (Invert)
2070 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
2071 else
2072 NewMI.addImm(CondCode);
Diana Picus116bbab2017-01-13 09:58:52 +00002073 NewMI.add(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002074
2075 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
2076 if (NewMI->hasOptionalDef())
Diana Picus8a73f552017-01-13 10:18:01 +00002077 NewMI.add(condCodeOp());
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002078
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002079 // The output register value when the predicate is false is an implicit
2080 // register operand tied to the first def.
2081 // The tie makes the register allocator ensure the FalseReg is allocated the
2082 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002083 FalseReg.setImplicit();
Diana Picus116bbab2017-01-13 09:58:52 +00002084 NewMI.add(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00002085 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
2086
Mehdi Amini22e59742015-01-13 07:07:13 +00002087 // Update SeenMIs set: register newly created MI and erase removed DefMI.
2088 SeenMIs.insert(NewMI);
2089 SeenMIs.erase(DefMI);
2090
Pete Cooper2127b002015-04-30 23:57:47 +00002091 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
2092 // DefMI would be invalid when tranferred inside the loop. Checking for a
2093 // loop is expensive, but at least remove kill flags if they are in different
2094 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002095 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00002096 NewMI->clearKillInfo();
2097
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00002098 // The caller will erase MI, but not DefMI.
2099 DefMI->eraseFromParent();
2100 return NewMI;
2101}
2102
Andrew Trick924123a2011-09-21 02:20:46 +00002103/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
2104/// instruction is encoded with an 'S' bit is determined by the optional CPSR
2105/// def operand.
2106///
2107/// This will go away once we can teach tblgen how to set the optional CPSR def
2108/// operand itself.
2109struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00002110 uint16_t PseudoOpc;
2111 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002112};
2113
Craig Topper2fbd1302012-05-24 03:59:11 +00002114static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00002115 {ARM::ADDSri, ARM::ADDri},
2116 {ARM::ADDSrr, ARM::ADDrr},
2117 {ARM::ADDSrsi, ARM::ADDrsi},
2118 {ARM::ADDSrsr, ARM::ADDrsr},
2119
2120 {ARM::SUBSri, ARM::SUBri},
2121 {ARM::SUBSrr, ARM::SUBrr},
2122 {ARM::SUBSrsi, ARM::SUBrsi},
2123 {ARM::SUBSrsr, ARM::SUBrsr},
2124
2125 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00002126 {ARM::RSBSrsi, ARM::RSBrsi},
2127 {ARM::RSBSrsr, ARM::RSBrsr},
2128
Artyom Skrobov92c06532017-03-22 23:35:51 +00002129 {ARM::tADDSi3, ARM::tADDi3},
2130 {ARM::tADDSi8, ARM::tADDi8},
2131 {ARM::tADDSrr, ARM::tADDrr},
2132 {ARM::tADCS, ARM::tADC},
2133
2134 {ARM::tSUBSi3, ARM::tSUBi3},
2135 {ARM::tSUBSi8, ARM::tSUBi8},
2136 {ARM::tSUBSrr, ARM::tSUBrr},
2137 {ARM::tSBCS, ARM::tSBC},
2138
Andrew Trick924123a2011-09-21 02:20:46 +00002139 {ARM::t2ADDSri, ARM::t2ADDri},
2140 {ARM::t2ADDSrr, ARM::t2ADDrr},
2141 {ARM::t2ADDSrs, ARM::t2ADDrs},
2142
2143 {ARM::t2SUBSri, ARM::t2SUBri},
2144 {ARM::t2SUBSrr, ARM::t2SUBrr},
2145 {ARM::t2SUBSrs, ARM::t2SUBrs},
2146
2147 {ARM::t2RSBSri, ARM::t2RSBri},
2148 {ARM::t2RSBSrs, ARM::t2RSBrs},
2149};
2150
2151unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00002152 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
2153 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
2154 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00002155 return 0;
2156}
2157
Evan Cheng780748d2009-07-28 05:48:47 +00002158void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002159 MachineBasicBlock::iterator &MBBI,
2160 const DebugLoc &dl, unsigned DestReg,
2161 unsigned BaseReg, int NumBytes,
2162 ARMCC::CondCodes Pred, unsigned PredReg,
2163 const ARMBaseInstrInfo &TII,
2164 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002165 if (NumBytes == 0 && DestReg != BaseReg) {
2166 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002167 .addReg(BaseReg, RegState::Kill)
2168 .add(predOps(Pred, PredReg))
2169 .add(condCodeOp())
2170 .setMIFlags(MIFlags);
Tim Northoverc9432eb2013-11-04 23:04:15 +00002171 return;
2172 }
2173
Evan Cheng780748d2009-07-28 05:48:47 +00002174 bool isSub = NumBytes < 0;
2175 if (isSub) NumBytes = -NumBytes;
2176
2177 while (NumBytes) {
2178 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2179 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2180 assert(ThisVal && "Didn't extract field correctly");
2181
2182 // We will handle these bits from offset, clear them.
2183 NumBytes &= ~ThisVal;
2184
2185 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2186
2187 // Build the new ADD / SUB.
2188 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2189 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
Diana Picusbd66b7d2017-01-20 08:15:24 +00002190 .addReg(BaseReg, RegState::Kill)
2191 .addImm(ThisVal)
2192 .add(predOps(Pred, PredReg))
2193 .add(condCodeOp())
2194 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002195 BaseReg = DestReg;
2196 }
2197}
2198
Tim Northoverdee86042013-12-02 14:46:26 +00002199bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2200 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002201 unsigned NumBytes) {
2202 // This optimisation potentially adds lots of load and store
2203 // micro-operations, it's only really a great benefit to code-size.
Sanjay Patel924879a2015-08-04 15:49:57 +00002204 if (!MF.getFunction()->optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002205 return false;
2206
2207 // If only one register is pushed/popped, LLVM can use an LDR/STR
2208 // instead. We can't modify those so make sure we're dealing with an
2209 // instruction we understand.
2210 bool IsPop = isPopOpcode(MI->getOpcode());
2211 bool IsPush = isPushOpcode(MI->getOpcode());
2212 if (!IsPush && !IsPop)
2213 return false;
2214
2215 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2216 MI->getOpcode() == ARM::VLDMDIA_UPD;
2217 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2218 MI->getOpcode() == ARM::tPOP ||
2219 MI->getOpcode() == ARM::tPOP_RET;
2220
2221 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2222 MI->getOperand(1).getReg() == ARM::SP)) &&
2223 "trying to fold sp update into non-sp-updating push/pop");
2224
2225 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2226 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2227 // if this is violated.
2228 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2229 return false;
2230
2231 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2232 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2233 int RegListIdx = IsT1PushPop ? 2 : 4;
2234
2235 // Calculate the space we'll need in terms of registers.
Tim Northovera9cc3852016-10-26 20:01:00 +00002236 unsigned RegsNeeded;
2237 const TargetRegisterClass *RegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002238 if (IsVFPPushPop) {
Tim Northover93bcc662013-11-08 17:18:07 +00002239 RegsNeeded = NumBytes / 8;
Tim Northovera9cc3852016-10-26 20:01:00 +00002240 RegClass = &ARM::DPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002241 } else {
Tim Northover93bcc662013-11-08 17:18:07 +00002242 RegsNeeded = NumBytes / 4;
Tim Northovera9cc3852016-10-26 20:01:00 +00002243 RegClass = &ARM::GPRRegClass;
Tim Northover93bcc662013-11-08 17:18:07 +00002244 }
2245
2246 // We're going to have to strip all list operands off before
2247 // re-adding them since the order matters, so save the existing ones
2248 // for later.
2249 SmallVector<MachineOperand, 4> RegList;
Tim Northovera9cc3852016-10-26 20:01:00 +00002250
2251 // We're also going to need the first register transferred by this
2252 // instruction, which won't necessarily be the first register in the list.
2253 unsigned FirstRegEnc = -1;
Tim Northover93bcc662013-11-08 17:18:07 +00002254
Tim Northover93bcc662013-11-08 17:18:07 +00002255 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northovera9cc3852016-10-26 20:01:00 +00002256 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
2257 MachineOperand &MO = MI->getOperand(i);
2258 RegList.push_back(MO);
2259
2260 if (MO.isReg() && TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
2261 FirstRegEnc = TRI->getEncodingValue(MO.getReg());
2262 }
2263
Tim Northover45479dc2013-12-01 14:16:24 +00002264 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002265
2266 // Now try to find enough space in the reglist to allocate NumBytes.
Tim Northovera9cc3852016-10-26 20:01:00 +00002267 for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
2268 --CurRegEnc) {
2269 unsigned CurReg = RegClass->getRegister(CurRegEnc);
Tim Northover93bcc662013-11-08 17:18:07 +00002270 if (!IsPop) {
2271 // Pushing any register is completely harmless, mark the
2272 // register involved as undef since we don't care about it in
2273 // the slightest.
2274 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2275 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002276 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002277 continue;
2278 }
2279
Tim Northover45479dc2013-12-01 14:16:24 +00002280 // However, we can only pop an extra register if it's not live. For
2281 // registers live within the function we might clobber a return value
2282 // register; the other way a register can be live here is if it's
2283 // callee-saved.
2284 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002285 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2286 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002287 // VFP pops don't allow holes in the register list, so any skip is fatal
2288 // for our transformation. GPR pops do, so we should just keep looking.
2289 if (IsVFPPushPop)
2290 return false;
2291 else
2292 continue;
2293 }
Tim Northover93bcc662013-11-08 17:18:07 +00002294
2295 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002296 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2297 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002298 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002299 }
2300
2301 if (RegsNeeded > 0)
2302 return false;
2303
2304 // Finally we know we can profitably perform the optimisation so go
2305 // ahead: strip all existing registers off and add them back again
2306 // in the right order.
2307 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2308 MI->RemoveOperand(i);
2309
2310 // Add the complete list back in.
2311 MachineInstrBuilder MIB(MF, &*MI);
2312 for (int i = RegList.size() - 1; i >= 0; --i)
Diana Picus116bbab2017-01-13 09:58:52 +00002313 MIB.add(RegList[i]);
Tim Northover93bcc662013-11-08 17:18:07 +00002314
2315 return true;
2316}
2317
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002318bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2319 unsigned FrameReg, int &Offset,
2320 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002321 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002322 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002323 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2324 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002325
Evan Cheng780748d2009-07-28 05:48:47 +00002326 // Memory operands in inline assembly always use AddrMode2.
2327 if (Opcode == ARM::INLINEASM)
2328 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002329
Evan Cheng780748d2009-07-28 05:48:47 +00002330 if (Opcode == ARM::ADDri) {
2331 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2332 if (Offset == 0) {
2333 // Turn it into a move.
2334 MI.setDesc(TII.get(ARM::MOVr));
2335 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2336 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002337 Offset = 0;
2338 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002339 } else if (Offset < 0) {
2340 Offset = -Offset;
2341 isSub = true;
2342 MI.setDesc(TII.get(ARM::SUBri));
2343 }
2344
2345 // Common case: small offset, fits into instruction.
2346 if (ARM_AM::getSOImmVal(Offset) != -1) {
2347 // Replace the FrameIndex with sp / fp
2348 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2349 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002350 Offset = 0;
2351 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002352 }
2353
2354 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2355 // as possible.
2356 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2357 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2358
2359 // We will handle these bits from offset, clear them.
2360 Offset &= ~ThisImmVal;
2361
2362 // Get the properly encoded SOImmVal field.
2363 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2364 "Bit extraction didn't work?");
2365 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2366 } else {
2367 unsigned ImmIdx = 0;
2368 int InstrOffs = 0;
2369 unsigned NumBits = 0;
2370 unsigned Scale = 1;
2371 switch (AddrMode) {
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002372 case ARMII::AddrMode_i12:
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002373 ImmIdx = FrameRegIdx + 1;
2374 InstrOffs = MI.getOperand(ImmIdx).getImm();
2375 NumBits = 12;
2376 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002377 case ARMII::AddrMode2:
Evan Cheng780748d2009-07-28 05:48:47 +00002378 ImmIdx = FrameRegIdx+2;
2379 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2380 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2381 InstrOffs *= -1;
2382 NumBits = 12;
2383 break;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002384 case ARMII::AddrMode3:
Evan Cheng780748d2009-07-28 05:48:47 +00002385 ImmIdx = FrameRegIdx+2;
2386 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2387 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2388 InstrOffs *= -1;
2389 NumBits = 8;
2390 break;
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002391 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002392 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002393 // Can't fold any offset even if it's zero.
2394 return false;
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002395 case ARMII::AddrMode5:
Evan Cheng780748d2009-07-28 05:48:47 +00002396 ImmIdx = FrameRegIdx+1;
2397 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2398 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2399 InstrOffs *= -1;
2400 NumBits = 8;
2401 Scale = 4;
2402 break;
Evan Cheng780748d2009-07-28 05:48:47 +00002403 default:
2404 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002405 }
2406
2407 Offset += InstrOffs * Scale;
2408 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2409 if (Offset < 0) {
2410 Offset = -Offset;
2411 isSub = true;
2412 }
2413
2414 // Attempt to fold address comp. if opcode has offset bits
2415 if (NumBits > 0) {
2416 // Common case: small offset, fits into instruction.
2417 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2418 int ImmedOffset = Offset / Scale;
2419 unsigned Mask = (1 << NumBits) - 1;
2420 if ((unsigned)Offset <= Mask * Scale) {
2421 // Replace the FrameIndex with sp
2422 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002423 // FIXME: When addrmode2 goes away, this will simplify (like the
2424 // T2 version), as the LDR.i12 versions don't need the encoding
2425 // tricks for the offset value.
2426 if (isSub) {
2427 if (AddrMode == ARMII::AddrMode_i12)
2428 ImmedOffset = -ImmedOffset;
2429 else
2430 ImmedOffset |= 1 << NumBits;
2431 }
Evan Cheng780748d2009-07-28 05:48:47 +00002432 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002433 Offset = 0;
2434 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002435 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002436
Evan Cheng780748d2009-07-28 05:48:47 +00002437 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2438 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002439 if (isSub) {
2440 if (AddrMode == ARMII::AddrMode_i12)
2441 ImmedOffset = -ImmedOffset;
2442 else
2443 ImmedOffset |= 1 << NumBits;
2444 }
Evan Cheng780748d2009-07-28 05:48:47 +00002445 ImmOp.ChangeToImmediate(ImmedOffset);
2446 Offset &= ~(Mask*Scale);
2447 }
2448 }
2449
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002450 Offset = (isSub) ? -Offset : Offset;
2451 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002452}
Bill Wendling7de9d522010-08-06 01:32:48 +00002453
Manman Ren6fa76dc2012-06-29 21:33:59 +00002454/// analyzeCompare - For a comparison instruction, return the source registers
2455/// in SrcReg and SrcReg2 if having two register operands, and the value it
2456/// compares against in CmpValue. Return true if the comparison instruction
2457/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002458bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2459 unsigned &SrcReg2, int &CmpMask,
2460 int &CmpValue) const {
2461 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002462 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002463 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002464 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002465 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002466 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002467 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002468 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002469 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002470 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002471 case ARM::CMPrr:
2472 case ARM::t2CMPrr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002473 SrcReg = MI.getOperand(0).getReg();
2474 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002475 CmpMask = ~0;
2476 CmpValue = 0;
2477 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002478 case ARM::TSTri:
2479 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002480 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002481 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002482 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002483 CmpValue = 0;
2484 return true;
2485 }
2486
2487 return false;
2488}
2489
Gabor Greifd36e3e82010-09-29 10:12:08 +00002490/// isSuitableForMask - Identify a suitable 'and' instruction that
2491/// operates on the given source register and applies the same mask
2492/// as a 'tst' instruction. Provide a limited look-through for copies.
2493/// When successful, MI will hold the found instruction.
2494static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002495 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002496 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002497 case ARM::ANDri:
2498 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002499 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002500 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002501 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002502 return true;
2503 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002504 }
2505
2506 return false;
2507}
2508
Manman Renb1b3db62012-06-29 22:06:19 +00002509/// getSwappedCondition - assume the flags are set by MI(a,b), return
2510/// the condition code if we modify the instructions such that flags are
2511/// set by MI(b,a).
2512inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2513 switch (CC) {
2514 default: return ARMCC::AL;
2515 case ARMCC::EQ: return ARMCC::EQ;
2516 case ARMCC::NE: return ARMCC::NE;
2517 case ARMCC::HS: return ARMCC::LS;
2518 case ARMCC::LO: return ARMCC::HI;
2519 case ARMCC::HI: return ARMCC::LO;
2520 case ARMCC::LS: return ARMCC::HS;
2521 case ARMCC::GE: return ARMCC::LE;
2522 case ARMCC::LT: return ARMCC::GT;
2523 case ARMCC::GT: return ARMCC::LT;
2524 case ARMCC::LE: return ARMCC::GE;
2525 }
2526}
2527
2528/// isRedundantFlagInstr - check whether the first instruction, whose only
2529/// purpose is to update flags, can be made redundant.
2530/// CMPrr can be made redundant by SUBrr if the operands are the same.
2531/// CMPri can be made redundant by SUBri if the operands are the same.
2532/// This function can be extended later on.
2533inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2534 unsigned SrcReg2, int ImmValue,
2535 MachineInstr *OI) {
2536 if ((CmpI->getOpcode() == ARM::CMPrr ||
2537 CmpI->getOpcode() == ARM::t2CMPrr) &&
2538 (OI->getOpcode() == ARM::SUBrr ||
2539 OI->getOpcode() == ARM::t2SUBrr) &&
2540 ((OI->getOperand(1).getReg() == SrcReg &&
2541 OI->getOperand(2).getReg() == SrcReg2) ||
2542 (OI->getOperand(1).getReg() == SrcReg2 &&
2543 OI->getOperand(2).getReg() == SrcReg)))
2544 return true;
2545
2546 if ((CmpI->getOpcode() == ARM::CMPri ||
2547 CmpI->getOpcode() == ARM::t2CMPri) &&
2548 (OI->getOpcode() == ARM::SUBri ||
2549 OI->getOpcode() == ARM::t2SUBri) &&
2550 OI->getOperand(1).getReg() == SrcReg &&
2551 OI->getOperand(2).getImm() == ImmValue)
2552 return true;
2553 return false;
2554}
2555
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002556static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
2557 switch (MI->getOpcode()) {
2558 default: return false;
2559 case ARM::tLSLri:
2560 case ARM::tLSRri:
2561 case ARM::tLSLrr:
2562 case ARM::tLSRrr:
2563 case ARM::tSUBrr:
2564 case ARM::tADDrr:
2565 case ARM::tADDi3:
2566 case ARM::tADDi8:
2567 case ARM::tSUBi3:
2568 case ARM::tSUBi8:
2569 case ARM::tMUL:
2570 IsThumb1 = true;
2571 LLVM_FALLTHROUGH;
2572 case ARM::RSBrr:
2573 case ARM::RSBri:
2574 case ARM::RSCrr:
2575 case ARM::RSCri:
2576 case ARM::ADDrr:
2577 case ARM::ADDri:
2578 case ARM::ADCrr:
2579 case ARM::ADCri:
2580 case ARM::SUBrr:
2581 case ARM::SUBri:
2582 case ARM::SBCrr:
2583 case ARM::SBCri:
2584 case ARM::t2RSBri:
2585 case ARM::t2ADDrr:
2586 case ARM::t2ADDri:
2587 case ARM::t2ADCrr:
2588 case ARM::t2ADCri:
2589 case ARM::t2SUBrr:
2590 case ARM::t2SUBri:
2591 case ARM::t2SBCrr:
2592 case ARM::t2SBCri:
2593 case ARM::ANDrr:
2594 case ARM::ANDri:
2595 case ARM::t2ANDrr:
2596 case ARM::t2ANDri:
2597 case ARM::ORRrr:
2598 case ARM::ORRri:
2599 case ARM::t2ORRrr:
2600 case ARM::t2ORRri:
2601 case ARM::EORrr:
2602 case ARM::EORri:
2603 case ARM::t2EORrr:
2604 case ARM::t2EORri:
2605 case ARM::t2LSRri:
2606 case ARM::t2LSRrr:
2607 case ARM::t2LSLri:
2608 case ARM::t2LSLrr:
2609 return true;
2610 }
2611}
2612
Manman Ren6fa76dc2012-06-29 21:33:59 +00002613/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2614/// comparison into one that sets the zero bit in the flags register;
2615/// Remove a redundant Compare instruction if an earlier instruction can set the
2616/// flags in the same way as Compare.
2617/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2618/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2619/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002620bool ARMBaseInstrInfo::optimizeCompareInstr(
2621 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2622 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002623 // Get the unique definition of SrcReg.
2624 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2625 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002626
Gabor Greifadbbb932010-09-21 12:01:15 +00002627 // Masked compares sometimes use the same register as the corresponding 'and'.
2628 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002629 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002630 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002631 for (MachineRegisterInfo::use_instr_iterator
2632 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2633 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002634 if (UI->getParent() != CmpInstr.getParent())
2635 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002636 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002637 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002638 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002639 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002640 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002641 break;
2642 }
2643 if (!MI) return false;
2644 }
2645 }
2646
Manman Rendc8ad002012-05-11 01:30:47 +00002647 // Get ready to iterate backward from CmpInstr.
2648 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002649 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002650
2651 // Early exit if CmpInstr is at the beginning of the BB.
2652 if (I == B) return false;
2653
Manman Rendc8ad002012-05-11 01:30:47 +00002654 // There are two possible candidates which can be changed to set CPSR:
2655 // One is MI, the other is a SUB instruction.
2656 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2657 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002658 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002659 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002660 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002661 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002662 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002663 // Conservatively refuse to convert an instruction which isn't in the same
2664 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002665 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2666 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002667 if (CmpInstr.getOpcode() == ARM::CMPri ||
2668 CmpInstr.getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002669 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002670 else
2671 return false;
2672 }
2673
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002674 bool IsThumb1 = false;
2675 if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
2676 return false;
2677
2678 // We also want to do this peephole for cases like this: if (a*b == 0),
2679 // and optimise away the CMP instruction from the generated code sequence:
2680 // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
2681 // resulting from the select instruction, but these MOVS instructions for
2682 // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
2683 // However, if we only have MOVS instructions in between the CMP and the
2684 // other instruction (the MULS in this example), then the CPSR is dead so we
2685 // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
2686 // reordering and then continue the analysis hoping we can eliminate the
2687 // CMP. This peephole works on the vregs, so is still in SSA form. As a
2688 // consequence, the movs won't redefine/kill the MUL operands which would
2689 // make this reordering illegal.
2690 if (MI && IsThumb1) {
2691 --I;
2692 bool CanReorder = true;
2693 const bool HasStmts = I != E;
2694 for (; I != E; --I) {
2695 if (I->getOpcode() != ARM::tMOVi8) {
2696 CanReorder = false;
2697 break;
2698 }
2699 }
2700 if (HasStmts && CanReorder) {
2701 MI = MI->removeFromParent();
2702 E = CmpInstr;
2703 CmpInstr.getParent()->insert(E, MI);
2704 }
2705 I = CmpInstr;
2706 E = MI;
2707 }
2708
Manman Rendc8ad002012-05-11 01:30:47 +00002709 // Check that CPSR isn't set between the comparison instruction and the one we
2710 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002711 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002712 --I;
2713 for (; I != E; --I) {
2714 const MachineInstr &Instr = *I;
2715
Manman Renb1b3db62012-06-29 22:06:19 +00002716 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2717 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002718 // This instruction modifies or uses CPSR after the one we want to
2719 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002720 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002721
Manman Renb1b3db62012-06-29 22:06:19 +00002722 // Check whether CmpInstr can be made redundant by the current instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002723 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002724 Sub = &*I;
2725 break;
2726 }
2727
Evan Chengd757c882010-09-21 23:49:07 +00002728 if (I == B)
2729 // The 'and' is below the comparison instruction.
2730 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002731 }
2732
Manman Rendc8ad002012-05-11 01:30:47 +00002733 // Return false if no candidates exist.
2734 if (!MI && !Sub)
2735 return false;
2736
2737 // The single candidate is called MI.
2738 if (!MI) MI = Sub;
2739
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002740 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002741 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002742 return false;
2743
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002744 // Scan forward for the use of CPSR
2745 // When checking against MI: if it's a conditional code that requires
2746 // checking of the V bit or C bit, then this is not safe to do.
2747 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2748 // If we are done with the basic block, we need to check whether CPSR is
2749 // live-out.
2750 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2751 OperandsToUpdate;
2752 bool isSafe = false;
2753 I = CmpInstr;
2754 E = CmpInstr.getParent()->end();
2755 while (!isSafe && ++I != E) {
2756 const MachineInstr &Instr = *I;
2757 for (unsigned IO = 0, EO = Instr.getNumOperands();
2758 !isSafe && IO != EO; ++IO) {
2759 const MachineOperand &MO = Instr.getOperand(IO);
2760 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2761 isSafe = true;
2762 break;
2763 }
2764 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2765 continue;
2766 if (MO.isDef()) {
2767 isSafe = true;
2768 break;
2769 }
2770 // Condition code is after the operand before CPSR except for VSELs.
2771 ARMCC::CondCodes CC;
2772 bool IsInstrVSel = true;
2773 switch (Instr.getOpcode()) {
2774 default:
2775 IsInstrVSel = false;
2776 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2777 break;
2778 case ARM::VSELEQD:
2779 case ARM::VSELEQS:
2780 CC = ARMCC::EQ;
2781 break;
2782 case ARM::VSELGTD:
2783 case ARM::VSELGTS:
2784 CC = ARMCC::GT;
2785 break;
2786 case ARM::VSELGED:
2787 case ARM::VSELGES:
2788 CC = ARMCC::GE;
2789 break;
2790 case ARM::VSELVSS:
2791 case ARM::VSELVSD:
2792 CC = ARMCC::VS;
2793 break;
2794 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002795
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002796 if (Sub) {
2797 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2798 if (NewCC == ARMCC::AL)
2799 return false;
2800 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2801 // on CMP needs to be updated to be based on SUB.
2802 // Push the condition code operands to OperandsToUpdate.
2803 // If it is safe to remove CmpInstr, the condition code of these
2804 // operands will be modified.
2805 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2806 Sub->getOperand(2).getReg() == SrcReg) {
2807 // VSel doesn't support condition code update.
2808 if (IsInstrVSel)
Manman Rendc8ad002012-05-11 01:30:47 +00002809 return false;
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002810 OperandsToUpdate.push_back(
2811 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2812 }
2813 } else {
2814 // No Sub, so this is x = <op> y, z; cmp x, 0.
2815 switch (CC) {
2816 case ARMCC::EQ: // Z
2817 case ARMCC::NE: // Z
2818 case ARMCC::MI: // N
2819 case ARMCC::PL: // N
2820 case ARMCC::AL: // none
2821 // CPSR can be used multiple times, we should continue.
2822 break;
2823 case ARMCC::HS: // C
2824 case ARMCC::LO: // C
2825 case ARMCC::VS: // V
2826 case ARMCC::VC: // V
2827 case ARMCC::HI: // C Z
2828 case ARMCC::LS: // C Z
2829 case ARMCC::GE: // N V
2830 case ARMCC::LT: // N V
2831 case ARMCC::GT: // Z N V
2832 case ARMCC::LE: // Z N V
2833 // The instruction uses the V bit or C bit which is not safe.
2834 return false;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002835 }
Evan Cheng425489d2011-03-23 22:52:04 +00002836 }
2837 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002838 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002839
2840 // If CPSR is not killed nor re-defined, we should check whether it is
2841 // live-out. If it is live-out, do not optimize.
2842 if (!isSafe) {
2843 MachineBasicBlock *MBB = CmpInstr.getParent();
2844 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2845 SE = MBB->succ_end(); SI != SE; ++SI)
2846 if ((*SI)->isLiveIn(ARM::CPSR))
2847 return false;
Cameron Zwarich0829b302011-04-15 20:45:00 +00002848 }
Sjoerd Meijer2db2a942017-01-20 13:10:12 +00002849
2850 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2851 // set CPSR so this is represented as an explicit output)
2852 if (!IsThumb1) {
2853 MI->getOperand(5).setReg(ARM::CPSR);
2854 MI->getOperand(5).setIsDef(true);
2855 }
2856 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
2857 CmpInstr.eraseFromParent();
2858
2859 // Modify the condition code of operands in OperandsToUpdate.
2860 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2861 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
2862 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2863 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
2864
2865 return true;
Bill Wendling7de9d522010-08-06 01:32:48 +00002866}
Evan Cheng367a5df2010-09-09 18:18:55 +00002867
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002868bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2869 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002870 MachineRegisterInfo *MRI) const {
2871 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002872 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002873 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2874 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002875 if (!DefMI.getOperand(1).isImm())
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002876 // Could be t2MOVi32imm <ga:xx>
2877 return false;
2878
2879 if (!MRI->hasOneNonDBGUse(Reg))
2880 return false;
2881
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002882 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002883 if (DefMCID.hasOptionalDef()) {
2884 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002885 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00002886 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2887 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2888 // to delete DefMI.
2889 return false;
2890 }
2891
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002892 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002893 if (UseMCID.hasOptionalDef()) {
2894 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002895 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00002896 // If the instruction sets the flag, do not attempt this optimization
2897 // since it may change the semantics of the code.
2898 return false;
2899 }
2900
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002901 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002902 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002903 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002904 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002905 bool Commute = false;
2906 switch (UseOpc) {
2907 default: return false;
2908 case ARM::SUBrr:
2909 case ARM::ADDrr:
2910 case ARM::ORRrr:
2911 case ARM::EORrr:
2912 case ARM::t2SUBrr:
2913 case ARM::t2ADDrr:
2914 case ARM::t2ORRrr:
2915 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002916 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002917 switch (UseOpc) {
2918 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002919 case ARM::ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002920 case ARM::SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002921 if (UseOpc == ARM::SUBrr && Commute)
2922 return false;
2923
2924 // ADD/SUB are special because they're essentially the same operation, so
2925 // we can handle a larger range of immediates.
2926 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
2927 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
2928 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
2929 ImmVal = -ImmVal;
2930 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
2931 } else
2932 return false;
2933 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2934 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2935 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002936 case ARM::ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002937 case ARM::EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002938 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2939 return false;
2940 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2941 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2942 switch (UseOpc) {
2943 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002944 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2945 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2946 }
2947 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002948 case ARM::t2ADDrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002949 case ARM::t2SUBrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002950 if (UseOpc == ARM::t2SUBrr && Commute)
2951 return false;
2952
2953 // ADD/SUB are special because they're essentially the same operation, so
2954 // we can handle a larger range of immediates.
2955 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2956 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
2957 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
2958 ImmVal = -ImmVal;
2959 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
2960 } else
2961 return false;
2962 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2963 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2964 break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002965 case ARM::t2ORRrr:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00002966 case ARM::t2EORrr:
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002967 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2968 return false;
2969 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2970 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2971 switch (UseOpc) {
2972 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002973 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2974 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2975 }
2976 break;
2977 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002978 }
2979 }
2980
2981 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002982 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
2983 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002984 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Diana Picus8a73f552017-01-13 10:18:01 +00002985 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
2986 NewReg)
2987 .addReg(Reg1, getKillRegState(isKill))
2988 .addImm(SOImmValV1)
2989 .add(predOps(ARMCC::AL))
2990 .add(condCodeOp());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002991 UseMI.setDesc(get(NewUseOpc));
2992 UseMI.getOperand(1).setReg(NewReg);
2993 UseMI.getOperand(1).setIsKill();
2994 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
2995 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002996 return true;
2997}
2998
Bob Wilsone8a549c2012-09-29 21:43:49 +00002999static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003000 const MachineInstr &MI) {
3001 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003002 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003003 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003004 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
3005 assert(UOps >= 0 && "bad # UOps");
3006 return UOps;
3007 }
3008
3009 case ARM::LDRrs:
3010 case ARM::LDRBrs:
3011 case ARM::STRrs:
3012 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003013 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003014 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3015 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3016 if (!isSub &&
3017 (ShImm == 0 ||
3018 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3019 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3020 return 1;
3021 return 2;
3022 }
3023
3024 case ARM::LDRH:
3025 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003026 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00003027 return 1;
3028
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003029 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003030 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3031 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3032 if (!isSub &&
3033 (ShImm == 0 ||
3034 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3035 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3036 return 1;
3037 return 2;
3038 }
3039
3040 case ARM::LDRSB:
3041 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003042 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003043
3044 case ARM::LDRSB_POST:
3045 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003046 unsigned Rt = MI.getOperand(0).getReg();
3047 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003048 return (Rt == Rm) ? 4 : 3;
3049 }
3050
3051 case ARM::LDR_PRE_REG:
3052 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003053 unsigned Rt = MI.getOperand(0).getReg();
3054 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003055 if (Rt == Rm)
3056 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003057 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003058 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3059 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3060 if (!isSub &&
3061 (ShImm == 0 ||
3062 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3063 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3064 return 2;
3065 return 3;
3066 }
3067
3068 case ARM::STR_PRE_REG:
3069 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003070 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003071 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3072 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3073 if (!isSub &&
3074 (ShImm == 0 ||
3075 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3076 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3077 return 2;
3078 return 3;
3079 }
3080
3081 case ARM::LDRH_PRE:
3082 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003083 unsigned Rt = MI.getOperand(0).getReg();
3084 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003085 if (!Rm)
3086 return 2;
3087 if (Rt == Rm)
3088 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003089 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003090 }
3091
3092 case ARM::LDR_POST_REG:
3093 case ARM::LDRB_POST_REG:
3094 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003095 unsigned Rt = MI.getOperand(0).getReg();
3096 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003097 return (Rt == Rm) ? 3 : 2;
3098 }
3099
3100 case ARM::LDR_PRE_IMM:
3101 case ARM::LDRB_PRE_IMM:
3102 case ARM::LDR_POST_IMM:
3103 case ARM::LDRB_POST_IMM:
3104 case ARM::STRB_POST_IMM:
3105 case ARM::STRB_POST_REG:
3106 case ARM::STRB_PRE_IMM:
3107 case ARM::STRH_POST:
3108 case ARM::STR_POST_IMM:
3109 case ARM::STR_POST_REG:
3110 case ARM::STR_PRE_IMM:
3111 return 2;
3112
3113 case ARM::LDRSB_PRE:
3114 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003115 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003116 if (Rm == 0)
3117 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003118 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003119 if (Rt == Rm)
3120 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003121 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003122 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3123 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3124 if (!isSub &&
3125 (ShImm == 0 ||
3126 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3127 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3128 return 3;
3129 return 4;
3130 }
3131
3132 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003133 unsigned Rt = MI.getOperand(0).getReg();
3134 unsigned Rn = MI.getOperand(2).getReg();
3135 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003136 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003137 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3138 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003139 return (Rt == Rn) ? 3 : 2;
3140 }
3141
3142 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003143 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003144 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003145 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
3146 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003147 return 2;
3148 }
3149
3150 case ARM::LDRD_POST:
3151 case ARM::t2LDRD_POST:
3152 return 3;
3153
3154 case ARM::STRD_POST:
3155 case ARM::t2STRD_POST:
3156 return 4;
3157
3158 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003159 unsigned Rt = MI.getOperand(0).getReg();
3160 unsigned Rn = MI.getOperand(3).getReg();
3161 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003162 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003163 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3164 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003165 return (Rt == Rn) ? 4 : 3;
3166 }
3167
3168 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003169 unsigned Rt = MI.getOperand(0).getReg();
3170 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003171 return (Rt == Rn) ? 4 : 3;
3172 }
3173
3174 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003175 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003176 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003177 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
3178 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003179 return 3;
3180 }
3181
3182 case ARM::t2STRD_PRE:
3183 return 3;
3184
3185 case ARM::t2LDR_POST:
3186 case ARM::t2LDRB_POST:
3187 case ARM::t2LDRB_PRE:
3188 case ARM::t2LDRSBi12:
3189 case ARM::t2LDRSBi8:
3190 case ARM::t2LDRSBpci:
3191 case ARM::t2LDRSBs:
3192 case ARM::t2LDRH_POST:
3193 case ARM::t2LDRH_PRE:
3194 case ARM::t2LDRSBT:
3195 case ARM::t2LDRSB_POST:
3196 case ARM::t2LDRSB_PRE:
3197 case ARM::t2LDRSH_POST:
3198 case ARM::t2LDRSH_PRE:
3199 case ARM::t2LDRSHi12:
3200 case ARM::t2LDRSHi8:
3201 case ARM::t2LDRSHpci:
3202 case ARM::t2LDRSHs:
3203 return 2;
3204
3205 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003206 unsigned Rt = MI.getOperand(0).getReg();
3207 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003208 return (Rt == Rn) ? 3 : 2;
3209 }
3210
3211 case ARM::t2STRB_POST:
3212 case ARM::t2STRB_PRE:
3213 case ARM::t2STRBs:
3214 case ARM::t2STRDi8:
3215 case ARM::t2STRH_POST:
3216 case ARM::t2STRH_PRE:
3217 case ARM::t2STRHs:
3218 case ARM::t2STR_POST:
3219 case ARM::t2STR_PRE:
3220 case ARM::t2STRs:
3221 return 2;
3222 }
3223}
3224
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003225// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3226// can't be easily determined return 0 (missing MachineMemOperand).
3227//
3228// FIXME: The current MachineInstr design does not support relying on machine
3229// mem operands to determine the width of a memory access. Instead, we expect
3230// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003231// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003232// two reasons:
3233//
3234// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3235// operands. This is much more dangerous than using the MachineMemOperand
3236// sizes because CodeGen passes can insert/remove optional machine operands. In
3237// fact, it's totally incorrect for preRA passes and appears to be wrong for
3238// postRA passes as well.
3239//
3240// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3241// machine model that calls this should handle the unknown (zero size) case.
3242//
3243// Long term, we should require a target hook that verifies MachineMemOperand
3244// sizes during MC lowering. That target hook should be local to MC lowering
3245// because we can't ensure that it is aware of other MI forms. Doing this will
3246// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003247unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003248 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003249 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3250 E = MI.memoperands_end();
3251 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003252 Size += (*I)->getSize();
3253 }
3254 return Size / 4;
3255}
3256
Diana Picus92423ce2016-06-27 09:08:23 +00003257static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3258 unsigned NumRegs) {
3259 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3260 switch (Opc) {
3261 default:
3262 break;
3263 case ARM::VLDMDIA_UPD:
3264 case ARM::VLDMDDB_UPD:
3265 case ARM::VLDMSIA_UPD:
3266 case ARM::VLDMSDB_UPD:
3267 case ARM::VSTMDIA_UPD:
3268 case ARM::VSTMDDB_UPD:
3269 case ARM::VSTMSIA_UPD:
3270 case ARM::VSTMSDB_UPD:
3271 case ARM::LDMIA_UPD:
3272 case ARM::LDMDA_UPD:
3273 case ARM::LDMDB_UPD:
3274 case ARM::LDMIB_UPD:
3275 case ARM::STMIA_UPD:
3276 case ARM::STMDA_UPD:
3277 case ARM::STMDB_UPD:
3278 case ARM::STMIB_UPD:
3279 case ARM::tLDMIA_UPD:
3280 case ARM::tSTMIA_UPD:
3281 case ARM::t2LDMIA_UPD:
3282 case ARM::t2LDMDB_UPD:
3283 case ARM::t2STMIA_UPD:
3284 case ARM::t2STMDB_UPD:
3285 ++UOps; // One for base register writeback.
3286 break;
3287 case ARM::LDMIA_RET:
3288 case ARM::tPOP_RET:
3289 case ARM::t2LDMIA_RET:
3290 UOps += 2; // One for base reg wb, one for write to pc.
3291 break;
3292 }
3293 return UOps;
3294}
3295
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003296unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3297 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003298 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003299 return 1;
3300
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003301 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003302 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003303 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003304 if (ItinUOps >= 0) {
3305 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3306 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3307
Andrew Trickf161e392012-07-02 18:10:42 +00003308 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003309 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003310
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003311 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003312 switch (Opc) {
3313 default:
3314 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003315 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003316 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003317 return 2;
3318
3319 // The number of uOps for load / store multiple are determined by the number
3320 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003321 //
Evan Chengbf407072010-09-10 01:29:16 +00003322 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3323 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003324 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003325 //
Evan Chengbf407072010-09-10 01:29:16 +00003326 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003327 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3328 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3329 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003330 case ARM::VLDMDIA_UPD:
3331 case ARM::VLDMDDB_UPD:
3332 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003333 case ARM::VLDMSIA_UPD:
3334 case ARM::VLDMSDB_UPD:
3335 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003336 case ARM::VSTMDIA_UPD:
3337 case ARM::VSTMDDB_UPD:
3338 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003339 case ARM::VSTMSIA_UPD:
3340 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003341 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003342 return (NumRegs / 2) + (NumRegs % 2) + 1;
3343 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003344
3345 case ARM::LDMIA_RET:
3346 case ARM::LDMIA:
3347 case ARM::LDMDA:
3348 case ARM::LDMDB:
3349 case ARM::LDMIB:
3350 case ARM::LDMIA_UPD:
3351 case ARM::LDMDA_UPD:
3352 case ARM::LDMDB_UPD:
3353 case ARM::LDMIB_UPD:
3354 case ARM::STMIA:
3355 case ARM::STMDA:
3356 case ARM::STMDB:
3357 case ARM::STMIB:
3358 case ARM::STMIA_UPD:
3359 case ARM::STMDA_UPD:
3360 case ARM::STMDB_UPD:
3361 case ARM::STMIB_UPD:
3362 case ARM::tLDMIA:
3363 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003364 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003365 case ARM::tPOP_RET:
3366 case ARM::tPOP:
3367 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003368 case ARM::t2LDMIA_RET:
3369 case ARM::t2LDMIA:
3370 case ARM::t2LDMDB:
3371 case ARM::t2LDMIA_UPD:
3372 case ARM::t2LDMDB_UPD:
3373 case ARM::t2STMIA:
3374 case ARM::t2STMDB:
3375 case ARM::t2STMIA_UPD:
3376 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003377 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003378 switch (Subtarget.getLdStMultipleTiming()) {
3379 case ARMSubtarget::SingleIssuePlusExtras:
3380 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3381 case ARMSubtarget::SingleIssue:
3382 // Assume the worst.
3383 return NumRegs;
3384 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003385 if (NumRegs < 4)
3386 return 2;
3387 // 4 registers would be issued: 2, 2.
3388 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003389 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003390 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003391 ++UOps;
3392 return UOps;
3393 }
3394 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3395 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003396 // If there are odd number of registers or if it's not 64-bit aligned,
3397 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003398 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3399 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003400 ++UOps;
3401 return UOps;
3402 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003403 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003404 }
3405 }
Diana Picus92423ce2016-06-27 09:08:23 +00003406 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003407}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003408
3409int
Evan Cheng412e37b2010-10-07 23:12:15 +00003410ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003411 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003412 unsigned DefClass,
3413 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003414 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003415 if (RegNo <= 0)
3416 // Def is the address writeback.
3417 return ItinData->getOperandCycle(DefClass, DefIdx);
3418
3419 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003420 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003421 // (regno / 2) + (regno % 2) + 1
3422 DefCycle = RegNo / 2 + 1;
3423 if (RegNo % 2)
3424 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003425 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003426 DefCycle = RegNo;
3427 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003428
Evan Cheng6cc775f2011-06-28 19:10:37 +00003429 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003430 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003431 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003432 case ARM::VLDMSIA_UPD:
3433 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003434 isSLoad = true;
3435 break;
3436 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003437
Evan Cheng412e37b2010-10-07 23:12:15 +00003438 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3439 // then it takes an extra cycle.
3440 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3441 ++DefCycle;
3442 } else {
3443 // Assume the worst.
3444 DefCycle = RegNo + 2;
3445 }
3446
3447 return DefCycle;
3448}
3449
Javed Absar4ae7e8122017-06-02 08:53:19 +00003450bool ARMBaseInstrInfo::isLDMBaseRegInList(const MachineInstr &MI) const {
3451 unsigned BaseReg = MI.getOperand(0).getReg();
3452 for (unsigned i = 1, sz = MI.getNumOperands(); i < sz; ++i) {
3453 const auto &Op = MI.getOperand(i);
3454 if (Op.isReg() && Op.getReg() == BaseReg)
3455 return true;
3456 }
3457 return false;
3458}
3459unsigned
3460ARMBaseInstrInfo::getLDMVariableDefsSize(const MachineInstr &MI) const {
3461 // ins GPR:$Rn, pred:$p (2xOp), reglist:$regs, variable_ops
3462 // (outs GPR:$wb), (ins GPR:$Rn, pred:$p (2xOp), reglist:$regs, variable_ops)
3463 return MI.getNumOperands() + 1 - MI.getDesc().getNumOperands();
3464}
3465
Evan Cheng412e37b2010-10-07 23:12:15 +00003466int
3467ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003468 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003469 unsigned DefClass,
3470 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003471 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003472 if (RegNo <= 0)
3473 // Def is the address writeback.
3474 return ItinData->getOperandCycle(DefClass, DefIdx);
3475
3476 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003477 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003478 // 4 registers would be issued: 1, 2, 1.
3479 // 5 registers would be issued: 1, 2, 2.
3480 DefCycle = RegNo / 2;
3481 if (DefCycle < 1)
3482 DefCycle = 1;
3483 // Result latency is issue cycle + 2: E2.
3484 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003485 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003486 DefCycle = (RegNo / 2);
3487 // If there are odd number of registers or if it's not 64-bit aligned,
3488 // then it takes an extra AGU (Address Generation Unit) cycle.
3489 if ((RegNo % 2) || DefAlign < 8)
3490 ++DefCycle;
3491 // Result latency is AGU cycles + 2.
3492 DefCycle += 2;
3493 } else {
3494 // Assume the worst.
3495 DefCycle = RegNo + 2;
3496 }
3497
3498 return DefCycle;
3499}
3500
3501int
3502ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003503 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003504 unsigned UseClass,
3505 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003506 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003507 if (RegNo <= 0)
3508 return ItinData->getOperandCycle(UseClass, UseIdx);
3509
3510 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003511 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003512 // (regno / 2) + (regno % 2) + 1
3513 UseCycle = RegNo / 2 + 1;
3514 if (RegNo % 2)
3515 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003516 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003517 UseCycle = RegNo;
3518 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003519
Evan Cheng6cc775f2011-06-28 19:10:37 +00003520 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003521 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003522 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003523 case ARM::VSTMSIA_UPD:
3524 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003525 isSStore = true;
3526 break;
3527 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003528
Evan Cheng412e37b2010-10-07 23:12:15 +00003529 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3530 // then it takes an extra cycle.
3531 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3532 ++UseCycle;
3533 } else {
3534 // Assume the worst.
3535 UseCycle = RegNo + 2;
3536 }
3537
3538 return UseCycle;
3539}
3540
3541int
3542ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003543 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003544 unsigned UseClass,
3545 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003546 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003547 if (RegNo <= 0)
3548 return ItinData->getOperandCycle(UseClass, UseIdx);
3549
3550 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003551 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003552 UseCycle = RegNo / 2;
3553 if (UseCycle < 2)
3554 UseCycle = 2;
3555 // Read in E3.
3556 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003557 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003558 UseCycle = (RegNo / 2);
3559 // If there are odd number of registers or if it's not 64-bit aligned,
3560 // then it takes an extra AGU (Address Generation Unit) cycle.
3561 if ((RegNo % 2) || UseAlign < 8)
3562 ++UseCycle;
3563 } else {
3564 // Assume the worst.
3565 UseCycle = 1;
3566 }
3567 return UseCycle;
3568}
3569
3570int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003571ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003572 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003573 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003574 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003575 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003576 unsigned DefClass = DefMCID.getSchedClass();
3577 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003578
Evan Cheng6cc775f2011-06-28 19:10:37 +00003579 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003580 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3581
3582 // This may be a def / use of a variable_ops instruction, the operand
3583 // latency might be determinable dynamically. Let the target try to
3584 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003585 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003586 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003587 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003588 default:
3589 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3590 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003591
3592 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003593 case ARM::VLDMDIA_UPD:
3594 case ARM::VLDMDDB_UPD:
3595 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003596 case ARM::VLDMSIA_UPD:
3597 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003598 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003599 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003600
3601 case ARM::LDMIA_RET:
3602 case ARM::LDMIA:
3603 case ARM::LDMDA:
3604 case ARM::LDMDB:
3605 case ARM::LDMIB:
3606 case ARM::LDMIA_UPD:
3607 case ARM::LDMDA_UPD:
3608 case ARM::LDMDB_UPD:
3609 case ARM::LDMIB_UPD:
3610 case ARM::tLDMIA:
3611 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003612 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003613 case ARM::t2LDMIA_RET:
3614 case ARM::t2LDMIA:
3615 case ARM::t2LDMDB:
3616 case ARM::t2LDMIA_UPD:
3617 case ARM::t2LDMDB_UPD:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00003618 LdmBypass = true;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003619 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003620 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003621 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003622
3623 if (DefCycle == -1)
3624 // We can't seem to determine the result latency of the def, assume it's 2.
3625 DefCycle = 2;
3626
3627 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003628 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003629 default:
3630 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3631 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003632
3633 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003634 case ARM::VSTMDIA_UPD:
3635 case ARM::VSTMDDB_UPD:
3636 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003637 case ARM::VSTMSIA_UPD:
3638 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003639 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003640 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003641
3642 case ARM::STMIA:
3643 case ARM::STMDA:
3644 case ARM::STMDB:
3645 case ARM::STMIB:
3646 case ARM::STMIA_UPD:
3647 case ARM::STMDA_UPD:
3648 case ARM::STMDB_UPD:
3649 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003650 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003651 case ARM::tPOP_RET:
3652 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003653 case ARM::t2STMIA:
3654 case ARM::t2STMDB:
3655 case ARM::t2STMIA_UPD:
3656 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003657 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003658 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003659 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003660
3661 if (UseCycle == -1)
3662 // Assume it's read in the first stage.
3663 UseCycle = 1;
3664
3665 UseCycle = DefCycle - UseCycle + 1;
3666 if (UseCycle > 0) {
3667 if (LdmBypass) {
3668 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3669 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003670 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003671 UseClass, UseIdx))
3672 --UseCycle;
3673 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003674 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003675 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003676 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003677 }
3678
3679 return UseCycle;
3680}
3681
Evan Cheng7fae11b2011-12-14 02:11:42 +00003682static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003683 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003684 unsigned &DefIdx, unsigned &Dist) {
3685 Dist = 0;
3686
3687 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003688 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003689 assert(II->isInsideBundle() && "Empty bundle?");
3690
3691 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003692 while (II->isInsideBundle()) {
3693 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3694 if (Idx != -1)
3695 break;
3696 --II;
3697 ++Dist;
3698 }
3699
3700 assert(Idx != -1 && "Cannot find bundled definition!");
3701 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003702 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003703}
3704
3705static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003706 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003707 unsigned &UseIdx, unsigned &Dist) {
3708 Dist = 0;
3709
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003710 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003711 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003712 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003713
3714 // FIXME: This doesn't properly handle multiple uses.
3715 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003716 while (II != E && II->isInsideBundle()) {
3717 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3718 if (Idx != -1)
3719 break;
3720 if (II->getOpcode() != ARM::t2IT)
3721 ++Dist;
3722 ++II;
3723 }
3724
Evan Chengda103bf2011-12-14 20:00:08 +00003725 if (Idx == -1) {
3726 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003727 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003728 }
3729
Evan Cheng7fae11b2011-12-14 02:11:42 +00003730 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003731 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003732}
3733
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003734/// Return the number of cycles to add to (or subtract from) the static
3735/// itinerary based on the def opcode and alignment. The caller will ensure that
3736/// adjusted latency is at least one cycle.
3737static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003738 const MachineInstr &DefMI,
3739 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003740 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003741 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003742 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3743 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003744 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003745 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003746 case ARM::LDRrs:
3747 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003748 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003749 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3750 if (ShImm == 0 ||
3751 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003752 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003753 break;
3754 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003755 case ARM::t2LDRs:
3756 case ARM::t2LDRBs:
3757 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003758 case ARM::t2LDRSHs: {
3759 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003760 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003761 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003762 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003763 break;
3764 }
3765 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003766 } else if (Subtarget.isSwift()) {
3767 // FIXME: Properly handle all of the latency adjustments for address
3768 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003769 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003770 default: break;
3771 case ARM::LDRrs:
3772 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003773 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003774 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3775 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3776 if (!isSub &&
3777 (ShImm == 0 ||
3778 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3779 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3780 Adjust -= 2;
3781 else if (!isSub &&
3782 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3783 --Adjust;
3784 break;
3785 }
3786 case ARM::t2LDRs:
3787 case ARM::t2LDRBs:
3788 case ARM::t2LDRHs:
3789 case ARM::t2LDRSHs: {
3790 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003791 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003792 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3793 Adjust -= 2;
3794 break;
3795 }
3796 }
Evan Chengff310732010-10-28 06:47:08 +00003797 }
3798
Diana Picus92423ce2016-06-27 09:08:23 +00003799 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003800 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003801 default: break;
3802 case ARM::VLD1q8:
3803 case ARM::VLD1q16:
3804 case ARM::VLD1q32:
3805 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003806 case ARM::VLD1q8wb_fixed:
3807 case ARM::VLD1q16wb_fixed:
3808 case ARM::VLD1q32wb_fixed:
3809 case ARM::VLD1q64wb_fixed:
3810 case ARM::VLD1q8wb_register:
3811 case ARM::VLD1q16wb_register:
3812 case ARM::VLD1q32wb_register:
3813 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003814 case ARM::VLD2d8:
3815 case ARM::VLD2d16:
3816 case ARM::VLD2d32:
3817 case ARM::VLD2q8:
3818 case ARM::VLD2q16:
3819 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003820 case ARM::VLD2d8wb_fixed:
3821 case ARM::VLD2d16wb_fixed:
3822 case ARM::VLD2d32wb_fixed:
3823 case ARM::VLD2q8wb_fixed:
3824 case ARM::VLD2q16wb_fixed:
3825 case ARM::VLD2q32wb_fixed:
3826 case ARM::VLD2d8wb_register:
3827 case ARM::VLD2d16wb_register:
3828 case ARM::VLD2d32wb_register:
3829 case ARM::VLD2q8wb_register:
3830 case ARM::VLD2q16wb_register:
3831 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003832 case ARM::VLD3d8:
3833 case ARM::VLD3d16:
3834 case ARM::VLD3d32:
3835 case ARM::VLD1d64T:
3836 case ARM::VLD3d8_UPD:
3837 case ARM::VLD3d16_UPD:
3838 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003839 case ARM::VLD1d64Twb_fixed:
3840 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003841 case ARM::VLD3q8_UPD:
3842 case ARM::VLD3q16_UPD:
3843 case ARM::VLD3q32_UPD:
3844 case ARM::VLD4d8:
3845 case ARM::VLD4d16:
3846 case ARM::VLD4d32:
3847 case ARM::VLD1d64Q:
3848 case ARM::VLD4d8_UPD:
3849 case ARM::VLD4d16_UPD:
3850 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003851 case ARM::VLD1d64Qwb_fixed:
3852 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003853 case ARM::VLD4q8_UPD:
3854 case ARM::VLD4q16_UPD:
3855 case ARM::VLD4q32_UPD:
3856 case ARM::VLD1DUPq8:
3857 case ARM::VLD1DUPq16:
3858 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003859 case ARM::VLD1DUPq8wb_fixed:
3860 case ARM::VLD1DUPq16wb_fixed:
3861 case ARM::VLD1DUPq32wb_fixed:
3862 case ARM::VLD1DUPq8wb_register:
3863 case ARM::VLD1DUPq16wb_register:
3864 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003865 case ARM::VLD2DUPd8:
3866 case ARM::VLD2DUPd16:
3867 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003868 case ARM::VLD2DUPd8wb_fixed:
3869 case ARM::VLD2DUPd16wb_fixed:
3870 case ARM::VLD2DUPd32wb_fixed:
3871 case ARM::VLD2DUPd8wb_register:
3872 case ARM::VLD2DUPd16wb_register:
3873 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003874 case ARM::VLD4DUPd8:
3875 case ARM::VLD4DUPd16:
3876 case ARM::VLD4DUPd32:
3877 case ARM::VLD4DUPd8_UPD:
3878 case ARM::VLD4DUPd16_UPD:
3879 case ARM::VLD4DUPd32_UPD:
3880 case ARM::VLD1LNd8:
3881 case ARM::VLD1LNd16:
3882 case ARM::VLD1LNd32:
3883 case ARM::VLD1LNd8_UPD:
3884 case ARM::VLD1LNd16_UPD:
3885 case ARM::VLD1LNd32_UPD:
3886 case ARM::VLD2LNd8:
3887 case ARM::VLD2LNd16:
3888 case ARM::VLD2LNd32:
3889 case ARM::VLD2LNq16:
3890 case ARM::VLD2LNq32:
3891 case ARM::VLD2LNd8_UPD:
3892 case ARM::VLD2LNd16_UPD:
3893 case ARM::VLD2LNd32_UPD:
3894 case ARM::VLD2LNq16_UPD:
3895 case ARM::VLD2LNq32_UPD:
3896 case ARM::VLD4LNd8:
3897 case ARM::VLD4LNd16:
3898 case ARM::VLD4LNd32:
3899 case ARM::VLD4LNq16:
3900 case ARM::VLD4LNq32:
3901 case ARM::VLD4LNd8_UPD:
3902 case ARM::VLD4LNd16_UPD:
3903 case ARM::VLD4LNd32_UPD:
3904 case ARM::VLD4LNq16_UPD:
3905 case ARM::VLD4LNq32_UPD:
3906 // If the address is not 64-bit aligned, the latencies of these
3907 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003908 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003909 break;
3910 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003911 }
3912 return Adjust;
3913}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003914
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003915int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3916 const MachineInstr &DefMI,
3917 unsigned DefIdx,
3918 const MachineInstr &UseMI,
3919 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003920 // No operand latency. The caller may fall back to getInstrLatency.
3921 if (!ItinData || ItinData->isEmpty())
3922 return -1;
3923
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003924 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003925 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003926
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003927 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003928 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003929 if (DefMI.isBundle())
3930 ResolvedDefMI =
3931 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
3932 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
3933 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003934 return 1;
3935 }
3936
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003937 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003938 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003939 if (UseMI.isBundle()) {
3940 ResolvedUseMI =
3941 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
3942 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00003943 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003944 }
3945
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003946 return getOperandLatencyImpl(
3947 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
3948 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
3949}
3950
3951int ARMBaseInstrInfo::getOperandLatencyImpl(
3952 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
3953 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
3954 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
3955 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003956 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003957 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003958 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003959 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003960 }
3961
3962 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003963 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003964 return 0;
3965
3966 // Otherwise it takes the instruction latency (generally one).
3967 unsigned Latency = getInstrLatency(ItinData, DefMI);
3968
3969 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3970 // its uses. Instructions which are otherwise scheduled between them may
3971 // incur a code size penalty (not able to use the CPSR setting 16-bit
3972 // instructions).
3973 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003974 const MachineFunction *MF = DefMI.getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003975 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003976 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003977 --Latency;
3978 }
3979 return Latency;
3980 }
3981
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003982 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00003983 return -1;
3984
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003985 unsigned DefAlign = DefMI.hasOneMemOperand()
3986 ? (*DefMI.memoperands_begin())->getAlignment()
3987 : 0;
3988 unsigned UseAlign = UseMI.hasOneMemOperand()
3989 ? (*UseMI.memoperands_begin())->getAlignment()
3990 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003991
3992 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003993 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
3994 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003995 // Unable to find operand latency. The caller may resort to getInstrLatency.
3996 if (Latency < 0)
3997 return Latency;
3998
3999 // Adjust for IT block position.
4000 int Adj = DefAdj + UseAdj;
4001
4002 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
4003 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4004 if (Adj >= 0 || (int)Latency > -Adj) {
4005 return Latency + Adj;
4006 }
4007 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00004008 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004009}
4010
4011int
4012ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4013 SDNode *DefNode, unsigned DefIdx,
4014 SDNode *UseNode, unsigned UseIdx) const {
4015 if (!DefNode->isMachineOpcode())
4016 return 1;
4017
Evan Cheng6cc775f2011-06-28 19:10:37 +00004018 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00004019
Evan Cheng6cc775f2011-06-28 19:10:37 +00004020 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00004021 return 0;
4022
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004023 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004024 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004025
Evan Cheng6c1414f2010-10-29 18:09:28 +00004026 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00004027 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00004028 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
4029 int Threshold = 1 + Adj;
4030 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00004031 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004032
Evan Cheng6cc775f2011-06-28 19:10:37 +00004033 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004034 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
4035 unsigned DefAlign = !DefMN->memoperands_empty()
4036 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
4037 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
4038 unsigned UseAlign = !UseMN->memoperands_empty()
4039 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00004040 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
4041 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00004042
4043 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00004044 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
4045 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00004046 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
4047 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00004048 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00004049 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004050 case ARM::LDRrs:
4051 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00004052 unsigned ShOpVal =
4053 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4054 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4055 if (ShImm == 0 ||
4056 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4057 --Latency;
4058 break;
4059 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00004060 case ARM::t2LDRs:
4061 case ARM::t2LDRBs:
4062 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00004063 case ARM::t2LDRSHs: {
4064 // Thumb2 mode: lsl only.
4065 unsigned ShAmt =
4066 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4067 if (ShAmt == 0 || ShAmt == 2)
4068 --Latency;
4069 break;
4070 }
4071 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00004072 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
4073 // FIXME: Properly handle all of the latency adjustments for address
4074 // writeback.
4075 switch (DefMCID.getOpcode()) {
4076 default: break;
4077 case ARM::LDRrs:
4078 case ARM::LDRBrs: {
4079 unsigned ShOpVal =
4080 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
4081 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
4082 if (ShImm == 0 ||
4083 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
4084 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4085 Latency -= 2;
4086 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4087 --Latency;
4088 break;
4089 }
4090 case ARM::t2LDRs:
4091 case ARM::t2LDRBs:
4092 case ARM::t2LDRHs:
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004093 case ARM::t2LDRSHs:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004094 // Thumb2 mode: lsl 0-3 only.
4095 Latency -= 2;
4096 break;
4097 }
Evan Chengff310732010-10-28 06:47:08 +00004098 }
4099
Diana Picus92423ce2016-06-27 09:08:23 +00004100 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00004101 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004102 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004103 case ARM::VLD1q8:
4104 case ARM::VLD1q16:
4105 case ARM::VLD1q32:
4106 case ARM::VLD1q64:
4107 case ARM::VLD1q8wb_register:
4108 case ARM::VLD1q16wb_register:
4109 case ARM::VLD1q32wb_register:
4110 case ARM::VLD1q64wb_register:
4111 case ARM::VLD1q8wb_fixed:
4112 case ARM::VLD1q16wb_fixed:
4113 case ARM::VLD1q32wb_fixed:
4114 case ARM::VLD1q64wb_fixed:
4115 case ARM::VLD2d8:
4116 case ARM::VLD2d16:
4117 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004118 case ARM::VLD2q8Pseudo:
4119 case ARM::VLD2q16Pseudo:
4120 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004121 case ARM::VLD2d8wb_fixed:
4122 case ARM::VLD2d16wb_fixed:
4123 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00004124 case ARM::VLD2q8PseudoWB_fixed:
4125 case ARM::VLD2q16PseudoWB_fixed:
4126 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00004127 case ARM::VLD2d8wb_register:
4128 case ARM::VLD2d16wb_register:
4129 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00004130 case ARM::VLD2q8PseudoWB_register:
4131 case ARM::VLD2q16PseudoWB_register:
4132 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004133 case ARM::VLD3d8Pseudo:
4134 case ARM::VLD3d16Pseudo:
4135 case ARM::VLD3d32Pseudo:
4136 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004137 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004138 case ARM::VLD3d8Pseudo_UPD:
4139 case ARM::VLD3d16Pseudo_UPD:
4140 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004141 case ARM::VLD3q8Pseudo_UPD:
4142 case ARM::VLD3q16Pseudo_UPD:
4143 case ARM::VLD3q32Pseudo_UPD:
4144 case ARM::VLD3q8oddPseudo:
4145 case ARM::VLD3q16oddPseudo:
4146 case ARM::VLD3q32oddPseudo:
4147 case ARM::VLD3q8oddPseudo_UPD:
4148 case ARM::VLD3q16oddPseudo_UPD:
4149 case ARM::VLD3q32oddPseudo_UPD:
4150 case ARM::VLD4d8Pseudo:
4151 case ARM::VLD4d16Pseudo:
4152 case ARM::VLD4d32Pseudo:
4153 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00004154 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004155 case ARM::VLD4d8Pseudo_UPD:
4156 case ARM::VLD4d16Pseudo_UPD:
4157 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004158 case ARM::VLD4q8Pseudo_UPD:
4159 case ARM::VLD4q16Pseudo_UPD:
4160 case ARM::VLD4q32Pseudo_UPD:
4161 case ARM::VLD4q8oddPseudo:
4162 case ARM::VLD4q16oddPseudo:
4163 case ARM::VLD4q32oddPseudo:
4164 case ARM::VLD4q8oddPseudo_UPD:
4165 case ARM::VLD4q16oddPseudo_UPD:
4166 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00004167 case ARM::VLD1DUPq8:
4168 case ARM::VLD1DUPq16:
4169 case ARM::VLD1DUPq32:
4170 case ARM::VLD1DUPq8wb_fixed:
4171 case ARM::VLD1DUPq16wb_fixed:
4172 case ARM::VLD1DUPq32wb_fixed:
4173 case ARM::VLD1DUPq8wb_register:
4174 case ARM::VLD1DUPq16wb_register:
4175 case ARM::VLD1DUPq32wb_register:
4176 case ARM::VLD2DUPd8:
4177 case ARM::VLD2DUPd16:
4178 case ARM::VLD2DUPd32:
4179 case ARM::VLD2DUPd8wb_fixed:
4180 case ARM::VLD2DUPd16wb_fixed:
4181 case ARM::VLD2DUPd32wb_fixed:
4182 case ARM::VLD2DUPd8wb_register:
4183 case ARM::VLD2DUPd16wb_register:
4184 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00004185 case ARM::VLD4DUPd8Pseudo:
4186 case ARM::VLD4DUPd16Pseudo:
4187 case ARM::VLD4DUPd32Pseudo:
4188 case ARM::VLD4DUPd8Pseudo_UPD:
4189 case ARM::VLD4DUPd16Pseudo_UPD:
4190 case ARM::VLD4DUPd32Pseudo_UPD:
4191 case ARM::VLD1LNq8Pseudo:
4192 case ARM::VLD1LNq16Pseudo:
4193 case ARM::VLD1LNq32Pseudo:
4194 case ARM::VLD1LNq8Pseudo_UPD:
4195 case ARM::VLD1LNq16Pseudo_UPD:
4196 case ARM::VLD1LNq32Pseudo_UPD:
4197 case ARM::VLD2LNd8Pseudo:
4198 case ARM::VLD2LNd16Pseudo:
4199 case ARM::VLD2LNd32Pseudo:
4200 case ARM::VLD2LNq16Pseudo:
4201 case ARM::VLD2LNq32Pseudo:
4202 case ARM::VLD2LNd8Pseudo_UPD:
4203 case ARM::VLD2LNd16Pseudo_UPD:
4204 case ARM::VLD2LNd32Pseudo_UPD:
4205 case ARM::VLD2LNq16Pseudo_UPD:
4206 case ARM::VLD2LNq32Pseudo_UPD:
4207 case ARM::VLD4LNd8Pseudo:
4208 case ARM::VLD4LNd16Pseudo:
4209 case ARM::VLD4LNd32Pseudo:
4210 case ARM::VLD4LNq16Pseudo:
4211 case ARM::VLD4LNq32Pseudo:
4212 case ARM::VLD4LNd8Pseudo_UPD:
4213 case ARM::VLD4LNd16Pseudo_UPD:
4214 case ARM::VLD4LNd32Pseudo_UPD:
4215 case ARM::VLD4LNq16Pseudo_UPD:
4216 case ARM::VLD4LNq32Pseudo_UPD:
4217 // If the address is not 64-bit aligned, the latencies of these
4218 // instructions increases by one.
4219 ++Latency;
4220 break;
4221 }
4222
Evan Chengff310732010-10-28 06:47:08 +00004223 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004224}
Evan Cheng63c76082010-10-19 18:58:51 +00004225
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004226unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4227 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4228 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004229 return 0;
4230
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004231 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004232 return 0;
4233
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004234 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004235
Javed Absar4ae7e8122017-06-02 08:53:19 +00004236 if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4237 !Subtarget.cheapPredicableCPSRDef())) {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004238 // When predicated, CPSR is an additional source operand for CPSR updating
4239 // instructions, this apparently increases their latencies.
4240 return 1;
4241 }
4242 return 0;
4243}
4244
Andrew Trick45446062012-06-05 21:11:27 +00004245unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004246 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004247 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004248 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4249 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004250 return 1;
4251
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004252 // An instruction scheduler typically runs on unbundled instructions, however
4253 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004254 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004255 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004256 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4257 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004258 while (++I != E && I->isInsideBundle()) {
4259 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004260 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004261 }
4262 return Latency;
4263 }
4264
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004265 const MCInstrDesc &MCID = MI.getDesc();
Javed Absar4ae7e8122017-06-02 08:53:19 +00004266 if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
4267 !Subtarget.cheapPredicableCPSRDef()))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004268 // When predicated, CPSR is an additional source operand for CPSR updating
4269 // instructions, this apparently increases their latencies.
4270 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004271 }
4272 // Be sure to call getStageLatency for an empty itinerary in case it has a
4273 // valid MinLatency property.
4274 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004275 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004276
4277 unsigned Class = MCID.getSchedClass();
4278
4279 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004280 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004281 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004282
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004283 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004284 unsigned Latency = ItinData->getStageLatency(Class);
4285
4286 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004287 unsigned DefAlign =
4288 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4289 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004290 if (Adj >= 0 || (int)Latency > -Adj) {
4291 return Latency + Adj;
4292 }
4293 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004294}
4295
4296int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4297 SDNode *Node) const {
4298 if (!Node->isMachineOpcode())
4299 return 1;
4300
4301 if (!ItinData || ItinData->isEmpty())
4302 return 1;
4303
4304 unsigned Opcode = Node->getMachineOpcode();
4305 switch (Opcode) {
4306 default:
4307 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004308 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004309 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004310 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004311 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004312}
4313
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004314bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4315 const MachineRegisterInfo *MRI,
4316 const MachineInstr &DefMI,
4317 unsigned DefIdx,
4318 const MachineInstr &UseMI,
4319 unsigned UseIdx) const {
4320 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4321 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004322 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004323 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004324 return true;
4325
4326 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004327 unsigned Latency =
4328 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004329 if (Latency <= 3)
4330 return false;
4331 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4332 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4333}
Evan Chenge96b8d72010-10-26 02:08:50 +00004334
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004335bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4336 const MachineInstr &DefMI,
4337 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004338 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004339 if (!ItinData || ItinData->isEmpty())
4340 return false;
4341
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004342 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004343 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004344 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004345 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4346 return (DefCycle != -1 && DefCycle <= 2);
4347 }
4348 return false;
4349}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004350
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004351bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004352 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004353 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004354 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4355 return false;
4356 }
4357 return true;
4358}
4359
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004360// LoadStackGuard has so far only been implemented for MachO. Different code
4361// sequence is needed for other targets.
4362void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4363 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004364 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004365 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4366 "ROPI/RWPI not currently supported with stack guard");
4367
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004368 MachineBasicBlock &MBB = *MI->getParent();
4369 DebugLoc DL = MI->getDebugLoc();
4370 unsigned Reg = MI->getOperand(0).getReg();
4371 const GlobalValue *GV =
4372 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4373 MachineInstrBuilder MIB;
4374
4375 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4376 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4377
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004378 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004379 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4380 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004381 auto Flags = MachineMemOperand::MOLoad |
4382 MachineMemOperand::MODereferenceable |
4383 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004384 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004385 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004386 MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004387 }
4388
4389 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004390 MIB.addReg(Reg, RegState::Kill)
4391 .addImm(0)
4392 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end())
4393 .add(predOps(ARMCC::AL));
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004394}
4395
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004396bool
4397ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4398 unsigned &AddSubOpc,
4399 bool &NegAcc, bool &HasLane) const {
4400 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4401 if (I == MLxEntryMap.end())
4402 return false;
4403
4404 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4405 MulOpc = Entry.MulOpc;
4406 AddSubOpc = Entry.AddSubOpc;
4407 NegAcc = Entry.NegAcc;
4408 HasLane = Entry.HasLane;
4409 return true;
4410}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004411
4412//===----------------------------------------------------------------------===//
4413// Execution domains.
4414//===----------------------------------------------------------------------===//
4415//
4416// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4417// and some can go down both. The vmov instructions go down the VFP pipeline,
4418// but they can be changed to vorr equivalents that are executed by the NEON
4419// pipeline.
4420//
4421// We use the following execution domain numbering:
4422//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004423enum ARMExeDomain {
4424 ExeGeneric = 0,
4425 ExeVFP = 1,
4426 ExeNEON = 2
4427};
Eugene Zelenkoe6cf4372017-01-26 23:40:06 +00004428
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004429//
4430// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4431//
4432std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004433ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004434 // If we don't have access to NEON instructions then we won't be able
4435 // to swizzle anything to the NEON domain. Check to make sure.
4436 if (Subtarget.hasNEON()) {
4437 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4438 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004439 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004440 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004441
Eric Christopher7e70aba2015-03-07 00:12:22 +00004442 // CortexA9 is particularly picky about mixing the two and wants these
4443 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004444 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4445 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4446 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004447 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4448 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004449 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004450 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004451
4452 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004453 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004454
4455 // Certain instructions can go either way on Cortex-A8.
4456 // Treat them as NEON instructions.
4457 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004458 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004459
4460 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004461 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004462
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004463 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004464}
4465
Tim Northover771f1602012-08-29 16:36:07 +00004466static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4467 unsigned SReg, unsigned &Lane) {
4468 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4469 Lane = 0;
4470
4471 if (DReg != ARM::NoRegister)
4472 return DReg;
4473
4474 Lane = 1;
4475 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4476
4477 assert(DReg && "S-register with no D super-register?");
4478 return DReg;
4479}
4480
Andrew Trickd9296ec2012-10-10 05:43:01 +00004481/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004482/// set ImplicitSReg to a register number that must be marked as implicit-use or
4483/// zero if no register needs to be defined as implicit-use.
4484///
4485/// If the function cannot determine if an SPR should be marked implicit use or
4486/// not, it returns false.
4487///
4488/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004489/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004490/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4491/// lane of the DPR).
4492///
4493/// If the other SPR is defined, an implicit-use of it should be added. Else,
4494/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004495///
James Molloyea052562012-09-18 08:31:15 +00004496static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004497 MachineInstr &MI, unsigned DReg,
4498 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004499 // If the DPR is defined or used already, the other SPR lane will be chained
4500 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004501 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004502 ImplicitSReg = 0;
4503 return true;
4504 }
4505
4506 // Otherwise we need to go searching to see if the SPR is set explicitly.
4507 ImplicitSReg = TRI->getSubReg(DReg,
4508 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4509 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004510 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004511
4512 if (LQR == MachineBasicBlock::LQR_Live)
4513 return true;
4514 else if (LQR == MachineBasicBlock::LQR_Unknown)
4515 return false;
4516
4517 // If the register is known not to be live, there is no need to add an
4518 // implicit-use.
4519 ImplicitSReg = 0;
4520 return true;
4521}
Tim Northover771f1602012-08-29 16:36:07 +00004522
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004523void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4524 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004525 unsigned DstReg, SrcReg, DReg;
4526 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004527 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004528 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004529 switch (MI.getOpcode()) {
4530 default:
4531 llvm_unreachable("cannot handle opcode!");
4532 break;
4533 case ARM::VMOVD:
4534 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004535 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004536
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004537 // Zap the predicate operands.
4538 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004539
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004540 // Make sure we've got NEON instructions.
4541 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004542
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004543 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4544 DstReg = MI.getOperand(0).getReg();
4545 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004546
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004547 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4548 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004549
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004550 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4551 MI.setDesc(get(ARM::VORRd));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004552 MIB.addReg(DstReg, RegState::Define)
4553 .addReg(SrcReg)
4554 .addReg(SrcReg)
4555 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004556 break;
4557 case ARM::VMOVRS:
4558 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004559 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004560 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004561
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004562 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4563 DstReg = MI.getOperand(0).getReg();
4564 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004565
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004566 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4567 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004568
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004569 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004570
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004571 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4572 // Note that DSrc has been widened and the other lane may be undef, which
4573 // contaminates the entire register.
4574 MI.setDesc(get(ARM::VGETLNi32));
Diana Picus4f8c3e12017-01-13 09:37:56 +00004575 MIB.addReg(DstReg, RegState::Define)
4576 .addReg(DReg, RegState::Undef)
4577 .addImm(Lane)
4578 .add(predOps(ARMCC::AL));
Tim Northoverf6618152012-08-17 11:32:52 +00004579
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004580 // The old source should be an implicit use, otherwise we might think it
4581 // was dead before here.
4582 MIB.addReg(SrcReg, RegState::Implicit);
4583 break;
4584 case ARM::VMOVSR: {
4585 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004586 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004587 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004588
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004589 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4590 DstReg = MI.getOperand(0).getReg();
4591 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004592
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004593 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004594
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004595 unsigned ImplicitSReg;
4596 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004597 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004598
4599 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4600 MI.RemoveOperand(i - 1);
4601
4602 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4603 // Again DDst may be undefined at the beginning of this instruction.
4604 MI.setDesc(get(ARM::VSETLNi32));
4605 MIB.addReg(DReg, RegState::Define)
4606 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4607 .addReg(SrcReg)
Diana Picus4f8c3e12017-01-13 09:37:56 +00004608 .addImm(Lane)
4609 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004610
4611 // The narrower destination must be marked as set to keep previous chains
4612 // in place.
4613 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4614 if (ImplicitSReg != 0)
4615 MIB.addReg(ImplicitSReg, RegState::Implicit);
4616 break;
James Molloyea052562012-09-18 08:31:15 +00004617 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004618 case ARM::VMOVS: {
4619 if (Domain != ExeNEON)
4620 break;
4621
4622 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004623 DstReg = MI.getOperand(0).getReg();
4624 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004625
Tim Northoverca9f3842012-08-30 10:17:45 +00004626 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4627 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4628 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4629
James Molloyea052562012-09-18 08:31:15 +00004630 unsigned ImplicitSReg;
4631 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4632 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004633
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004634 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4635 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004636
Tim Northoverca9f3842012-08-30 10:17:45 +00004637 if (DSrc == DDst) {
4638 // Destination can be:
4639 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004640 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004641 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004642 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
Diana Picus4f8c3e12017-01-13 09:37:56 +00004643 .addImm(SrcLane)
4644 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004645
4646 // Neither the source or the destination are naturally represented any
4647 // more, so add them in manually.
4648 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4649 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004650 if (ImplicitSReg != 0)
4651 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004652 break;
4653 }
4654
4655 // In general there's no single instruction that can perform an S <-> S
4656 // move in NEON space, but a pair of VEXT instructions *can* do the
4657 // job. It turns out that the VEXTs needed will only use DSrc once, with
4658 // the position based purely on the combination of lane-0 and lane-1
4659 // involved. For example
4660 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4661 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4662 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4663 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4664 //
4665 // Pattern of the MachineInstrs is:
4666 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4667 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004668 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4669 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004670
4671 // On the first instruction, both DSrc and DDst may be <undef> if present.
4672 // Specifically when the original instruction didn't have them as an
4673 // <imp-use>.
4674 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004675 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004676 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4677
4678 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004679 CurUndef = !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004680 NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
4681 .addImm(1)
4682 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004683
4684 if (SrcLane == DstLane)
4685 NewMIB.addReg(SrcReg, RegState::Implicit);
4686
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004687 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004688 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004689
4690 // On the second instruction, DDst has definitely been defined above, so
4691 // it is not <undef>. DSrc, if present, can be <undef> as above.
4692 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004693 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004694 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4695
4696 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004697 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Diana Picus4f8c3e12017-01-13 09:37:56 +00004698 MIB.addReg(CurReg, getUndefRegState(CurUndef))
4699 .addImm(1)
4700 .add(predOps(ARMCC::AL));
Tim Northoverca9f3842012-08-30 10:17:45 +00004701
4702 if (SrcLane != DstLane)
4703 MIB.addReg(SrcReg, RegState::Implicit);
4704
4705 // As before, the original destination is no longer represented, add it
4706 // implicitly.
4707 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004708 if (ImplicitSReg != 0)
4709 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004710 break;
4711 }
Tim Northoverf6618152012-08-17 11:32:52 +00004712 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004713}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004714
Bob Wilsone8a549c2012-09-29 21:43:49 +00004715//===----------------------------------------------------------------------===//
4716// Partial register updates
4717//===----------------------------------------------------------------------===//
4718//
4719// Swift renames NEON registers with 64-bit granularity. That means any
4720// instruction writing an S-reg implicitly reads the containing D-reg. The
4721// problem is mostly avoided by translating f32 operations to v2f32 operations
4722// on D-registers, but f32 loads are still a problem.
4723//
4724// These instructions can load an f32 into a NEON register:
4725//
4726// VLDRS - Only writes S, partial D update.
4727// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4728// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4729//
4730// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004731unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4732 const MachineInstr &MI, unsigned OpNum,
4733 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004734 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4735 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004736 return 0;
4737
4738 assert(TRI && "Need TRI instance");
4739
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004740 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004741 if (MO.readsReg())
4742 return 0;
4743 unsigned Reg = MO.getReg();
4744 int UseOp = -1;
4745
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004746 switch (MI.getOpcode()) {
4747 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004748 case ARM::VLDRS:
4749 case ARM::FCONSTS:
4750 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004751 case ARM::VMOVv8i8:
4752 case ARM::VMOVv4i16:
4753 case ARM::VMOVv2i32:
4754 case ARM::VMOVv2f32:
4755 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004756 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004757 break;
4758
4759 // Explicitly reads the dependency.
4760 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004761 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004762 break;
4763 default:
4764 return 0;
4765 }
4766
4767 // If this instruction actually reads a value from Reg, there is no unwanted
4768 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004769 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00004770 return 0;
4771
4772 // We must be able to clobber the whole D-reg.
4773 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4774 // Virtual register must be a foo:ssub_0<def,undef> operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004775 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004776 return 0;
4777 } else if (ARM::SPRRegClass.contains(Reg)) {
4778 // Physical register: MI must define the full D-reg.
4779 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4780 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004781 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004782 return 0;
4783 }
4784
4785 // MI has an unwanted D-register dependency.
4786 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00004787 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004788}
4789
4790// Break a partial register dependency after getPartialRegUpdateClearance
4791// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004792void ARMBaseInstrInfo::breakPartialRegDependency(
4793 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4794 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004795 assert(TRI && "Need TRI instance");
4796
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004797 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004798 unsigned Reg = MO.getReg();
4799 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4800 "Can't break virtual register dependencies.");
4801 unsigned DReg = Reg;
4802
4803 // If MI defines an S-reg, find the corresponding D super-register.
4804 if (ARM::SPRRegClass.contains(Reg)) {
4805 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4806 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4807 }
4808
4809 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004810 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004811
4812 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4813 // the full D-register by loading the same value to both lanes. The
4814 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004815 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004816 // too big regressions.
4817
4818 // Insert the dependency-breaking FCONSTD before MI.
4819 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Diana Picus4f8c3e12017-01-13 09:37:56 +00004820 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4821 .addImm(96)
4822 .add(predOps(ARMCC::AL));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004823 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004824}
4825
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004826bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004827 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004828}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004829
4830bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004831 if (MI->getNumOperands() < 4)
4832 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004833 unsigned ShOpVal = MI->getOperand(3).getImm();
4834 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4835 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4836 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4837 ((ShImm == 1 || ShImm == 2) &&
4838 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4839 return true;
4840
4841 return false;
4842}
Quentin Colombetd358e842014-08-22 18:05:22 +00004843
4844bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4845 const MachineInstr &MI, unsigned DefIdx,
4846 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4847 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4848 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4849
4850 switch (MI.getOpcode()) {
4851 case ARM::VMOVDRR:
4852 // dX = VMOVDRR rY, rZ
4853 // is the same as:
4854 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4855 // Populate the InputRegs accordingly.
4856 // rY
4857 const MachineOperand *MOReg = &MI.getOperand(1);
4858 InputRegs.push_back(
4859 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4860 // rZ
4861 MOReg = &MI.getOperand(2);
4862 InputRegs.push_back(
4863 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4864 return true;
4865 }
4866 llvm_unreachable("Target dependent opcode missing");
4867}
4868
4869bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4870 const MachineInstr &MI, unsigned DefIdx,
4871 RegSubRegPairAndIdx &InputReg) const {
4872 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4873 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4874
4875 switch (MI.getOpcode()) {
4876 case ARM::VMOVRRD:
4877 // rX, rY = VMOVRRD dZ
4878 // is the same as:
4879 // rX = EXTRACT_SUBREG dZ, ssub_0
4880 // rY = EXTRACT_SUBREG dZ, ssub_1
4881 const MachineOperand &MOReg = MI.getOperand(2);
4882 InputReg.Reg = MOReg.getReg();
4883 InputReg.SubReg = MOReg.getSubReg();
4884 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4885 return true;
4886 }
4887 llvm_unreachable("Target dependent opcode missing");
4888}
4889
4890bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4891 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4892 RegSubRegPairAndIdx &InsertedReg) const {
4893 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4894 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4895
4896 switch (MI.getOpcode()) {
4897 case ARM::VSETLNi32:
4898 // dX = VSETLNi32 dY, rZ, imm
4899 const MachineOperand &MOBaseReg = MI.getOperand(1);
4900 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4901 const MachineOperand &MOIndex = MI.getOperand(3);
4902 BaseReg.Reg = MOBaseReg.getReg();
4903 BaseReg.SubReg = MOBaseReg.getSubReg();
4904
4905 InsertedReg.Reg = MOInsertedReg.getReg();
4906 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4907 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4908 return true;
4909 }
4910 llvm_unreachable("Target dependent opcode missing");
4911}