blob: 0e30d6e2d41b1c5e5a4887681e080bd29b06c4bd [file] [log] [blame]
Chris Lattner0d5644b2003-01-13 00:26:36 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman10468d82005-04-21 22:55:34 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner910b82f2002-10-28 23:55:33 +00008//
Chris Lattnerf6932b72005-01-19 06:53:34 +00009// This file implements the TargetInstrInfo class.
Chris Lattner910b82f2002-10-28 23:55:33 +000010//
11//===----------------------------------------------------------------------===//
12
David Blaikie3f833ed2017-11-08 01:01:31 +000013#include "llvm/CodeGen/TargetInstrInfo.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000014#include "llvm/CodeGen/MachineFrameInfo.h"
Lang Hames39609992013-11-29 03:07:54 +000015#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000016#include "llvm/CodeGen/MachineMemOperand.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/CodeGen/PseudoSourceValue.h"
19#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Lang Hames39609992013-11-29 03:07:54 +000020#include "llvm/CodeGen/StackMaps.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000021#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetLowering.h"
23#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun88e21312015-06-13 03:42:11 +000024#include "llvm/CodeGen/TargetSchedule.h"
Andrew Trick10d5be42013-11-17 01:36:23 +000025#include "llvm/IR/DataLayout.h"
Evan Cheng49d4c0b2010-10-06 06:27:31 +000026#include "llvm/MC/MCAsmInfo.h"
Evan Cheng8264e272011-06-29 01:14:12 +000027#include "llvm/MC/MCInstrItineraries.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000028#include "llvm/Support/CommandLine.h"
Chris Lattner01614192009-08-02 04:58:19 +000029#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000030#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000031#include "llvm/Target/TargetMachine.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000032#include <cctype>
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000033
Chris Lattnerf6932b72005-01-19 06:53:34 +000034using namespace llvm;
Chris Lattner910b82f2002-10-28 23:55:33 +000035
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000036static cl::opt<bool> DisableHazardRecognizer(
37 "disable-sched-hazard", cl::Hidden, cl::init(false),
38 cl::desc("Disable hazard detection during preRA scheduling"));
Chris Lattnere98a3c32009-08-02 05:20:37 +000039
Chris Lattner0d5644b2003-01-13 00:26:36 +000040TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner910b82f2002-10-28 23:55:33 +000041}
42
Evan Cheng8d71a752011-06-27 21:26:13 +000043const TargetRegisterClass*
Evan Cheng6cc775f2011-06-28 19:10:37 +000044TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000045 const TargetRegisterInfo *TRI,
46 const MachineFunction &MF) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +000047 if (OpNum >= MCID.getNumOperands())
Craig Topperc0196b12014-04-14 00:51:57 +000048 return nullptr;
Evan Cheng8d71a752011-06-27 21:26:13 +000049
Evan Cheng6cc775f2011-06-28 19:10:37 +000050 short RegClass = MCID.OpInfo[OpNum].RegClass;
51 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000052 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng8d71a752011-06-27 21:26:13 +000053
54 // Instructions like INSERT_SUBREG do not have fixed register classes.
55 if (RegClass < 0)
Craig Topperc0196b12014-04-14 00:51:57 +000056 return nullptr;
Evan Cheng8d71a752011-06-27 21:26:13 +000057
58 // Otherwise just look it up normally.
59 return TRI->getRegClass(RegClass);
60}
61
Chris Lattner01614192009-08-02 04:58:19 +000062/// insertNoop - Insert a noop into the instruction stream at the specified
63/// point.
Andrew Trickc416ba62010-12-24 04:28:06 +000064void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattner01614192009-08-02 04:58:19 +000065 MachineBasicBlock::iterator MI) const {
66 llvm_unreachable("Target didn't implement insertNoop!");
67}
68
Alex Bradbury5518cbf2017-09-28 09:31:46 +000069static bool isAsmComment(const char *Str, const MCAsmInfo &MAI) {
70 return strncmp(Str, MAI.getCommentString().data(),
71 MAI.getCommentString().size()) == 0;
72}
73
Chris Lattnere98a3c32009-08-02 05:20:37 +000074/// Measure the specified inline asm to determine an approximation of its
75/// length.
Jim Grosbacha3df87f2011-03-24 18:46:34 +000076/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnere98a3c32009-08-02 05:20:37 +000077/// count as an instruction.
78/// Any other non-whitespace text is considered an instruction, with
Jim Grosbacha3df87f2011-03-24 18:46:34 +000079/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnere98a3c32009-08-02 05:20:37 +000080/// Variable-length instructions are not handled here; this function
81/// may be overloaded in the target code to do that.
Alex Bradbury5518cbf2017-09-28 09:31:46 +000082/// We implement a special case of the .space directive which takes only a
83/// single integer argument in base 10 that is the size in bytes. This is a
84/// restricted form of the GAS directive in that we only interpret
85/// simple--i.e. not a logical or arithmetic expression--size values without
86/// the optional fill value. This is primarily used for creating arbitrary
87/// sized inline asm blocks for testing purposes.
Matt Arsenaultca64ef22019-05-22 16:28:41 +000088unsigned TargetInstrInfo::getInlineAsmLength(
89 const char *Str,
90 const MCAsmInfo &MAI, const TargetSubtargetInfo *STI) const {
Chris Lattnere98a3c32009-08-02 05:20:37 +000091 // Count the number of instructions in the asm.
Alex Bradbury5518cbf2017-09-28 09:31:46 +000092 bool AtInsnStart = true;
93 unsigned Length = 0;
Matt Arsenaultca64ef22019-05-22 16:28:41 +000094 const unsigned MaxInstLength = MAI.getMaxInstLength(STI);
Chris Lattnere98a3c32009-08-02 05:20:37 +000095 for (; *Str; ++Str) {
Jim Grosbacha3df87f2011-03-24 18:46:34 +000096 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
Matt Arsenaultaccddac2016-07-01 23:26:50 +000097 strlen(MAI.getSeparatorString())) == 0) {
Alex Bradbury5518cbf2017-09-28 09:31:46 +000098 AtInsnStart = true;
99 } else if (isAsmComment(Str, MAI)) {
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000100 // Stop counting as an instruction after a comment until the next
101 // separator.
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000102 AtInsnStart = false;
Chris Lattnere98a3c32009-08-02 05:20:37 +0000103 }
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000104
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000105 if (AtInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
Matt Arsenaultca64ef22019-05-22 16:28:41 +0000106 unsigned AddLength = MaxInstLength;
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000107 if (strncmp(Str, ".space", 6) == 0) {
108 char *EStr;
109 int SpaceSize;
110 SpaceSize = strtol(Str + 6, &EStr, 10);
111 SpaceSize = SpaceSize < 0 ? 0 : SpaceSize;
112 while (*EStr != '\n' && std::isspace(static_cast<unsigned char>(*EStr)))
113 ++EStr;
114 if (*EStr == '\0' || *EStr == '\n' ||
115 isAsmComment(EStr, MAI)) // Successfully parsed .space argument
116 AddLength = SpaceSize;
117 }
118 Length += AddLength;
119 AtInsnStart = false;
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000120 }
Chris Lattnere98a3c32009-08-02 05:20:37 +0000121 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000122
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000123 return Length;
Chris Lattnere98a3c32009-08-02 05:20:37 +0000124}
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000125
126/// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
127/// after it, replacing it with an unconditional branch to NewDest.
128void
129TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
130 MachineBasicBlock *NewDest) const {
131 MachineBasicBlock *MBB = Tail->getParent();
132
133 // Remove all the old successors of MBB from the CFG.
134 while (!MBB->succ_empty())
135 MBB->removeSuccessor(MBB->succ_begin());
136
Justin Bognerec5ea362016-03-25 18:38:48 +0000137 // Save off the debug loc before erasing the instruction.
138 DebugLoc DL = Tail->getDebugLoc();
139
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000140 // Remove all the dead instructions from the end of MBB.
141 MBB->erase(Tail, MBB->end());
142
143 // If MBB isn't immediately before MBB, insert a branch to it.
144 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000145 insertBranch(*MBB, NewDest, nullptr, SmallVector<MachineOperand, 0>(), DL);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000146 MBB->addSuccessor(NewDest);
147}
148
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000149MachineInstr *TargetInstrInfo::commuteInstructionImpl(MachineInstr &MI,
150 bool NewMI, unsigned Idx1,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000151 unsigned Idx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000152 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000153 bool HasDef = MCID.getNumDefs();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 if (HasDef && !MI.getOperand(0).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000155 // No idea how to commute this instruction. Target should implement its own.
Craig Topperc0196b12014-04-14 00:51:57 +0000156 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000157
Richard Trieue778e872015-09-28 22:54:43 +0000158 unsigned CommutableOpIdx1 = Idx1; (void)CommutableOpIdx1;
159 unsigned CommutableOpIdx2 = Idx2; (void)CommutableOpIdx2;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000160 assert(findCommutedOpIndices(MI, CommutableOpIdx1, CommutableOpIdx2) &&
161 CommutableOpIdx1 == Idx1 && CommutableOpIdx2 == Idx2 &&
162 "TargetInstrInfo::CommuteInstructionImpl(): not commutable operands.");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000163 assert(MI.getOperand(Idx1).isReg() && MI.getOperand(Idx2).isReg() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000164 "This only knows how to commute register operands so far");
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000165
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000166 unsigned Reg0 = HasDef ? MI.getOperand(0).getReg() : 0;
167 unsigned Reg1 = MI.getOperand(Idx1).getReg();
168 unsigned Reg2 = MI.getOperand(Idx2).getReg();
169 unsigned SubReg0 = HasDef ? MI.getOperand(0).getSubReg() : 0;
170 unsigned SubReg1 = MI.getOperand(Idx1).getSubReg();
171 unsigned SubReg2 = MI.getOperand(Idx2).getSubReg();
172 bool Reg1IsKill = MI.getOperand(Idx1).isKill();
173 bool Reg2IsKill = MI.getOperand(Idx2).isKill();
174 bool Reg1IsUndef = MI.getOperand(Idx1).isUndef();
175 bool Reg2IsUndef = MI.getOperand(Idx2).isUndef();
176 bool Reg1IsInternal = MI.getOperand(Idx1).isInternalRead();
177 bool Reg2IsInternal = MI.getOperand(Idx2).isInternalRead();
Geoff Berryd37dc772018-01-29 18:47:48 +0000178 // Avoid calling isRenamable for virtual registers since we assert that
179 // renamable property is only queried/set for physical registers.
180 bool Reg1IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg1)
181 ? MI.getOperand(Idx1).isRenamable()
182 : false;
183 bool Reg2IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg2)
184 ? MI.getOperand(Idx2).isRenamable()
185 : false;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000186 // If destination is tied to either of the commuted source register, then
187 // it must be updated.
188 if (HasDef && Reg0 == Reg1 &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000189 MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000190 Reg2IsKill = false;
191 Reg0 = Reg2;
192 SubReg0 = SubReg2;
193 } else if (HasDef && Reg0 == Reg2 &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000194 MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000195 Reg1IsKill = false;
196 Reg0 = Reg1;
197 SubReg0 = SubReg1;
198 }
199
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000200 MachineInstr *CommutedMI = nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000201 if (NewMI) {
202 // Create a new instruction.
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000203 MachineFunction &MF = *MI.getMF();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000204 CommutedMI = MF.CloneMachineInstr(&MI);
205 } else {
206 CommutedMI = &MI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000207 }
208
209 if (HasDef) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000210 CommutedMI->getOperand(0).setReg(Reg0);
211 CommutedMI->getOperand(0).setSubReg(SubReg0);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000212 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000213 CommutedMI->getOperand(Idx2).setReg(Reg1);
214 CommutedMI->getOperand(Idx1).setReg(Reg2);
215 CommutedMI->getOperand(Idx2).setSubReg(SubReg1);
216 CommutedMI->getOperand(Idx1).setSubReg(SubReg2);
217 CommutedMI->getOperand(Idx2).setIsKill(Reg1IsKill);
218 CommutedMI->getOperand(Idx1).setIsKill(Reg2IsKill);
219 CommutedMI->getOperand(Idx2).setIsUndef(Reg1IsUndef);
220 CommutedMI->getOperand(Idx1).setIsUndef(Reg2IsUndef);
221 CommutedMI->getOperand(Idx2).setIsInternalRead(Reg1IsInternal);
222 CommutedMI->getOperand(Idx1).setIsInternalRead(Reg2IsInternal);
Geoff Berryd37dc772018-01-29 18:47:48 +0000223 // Avoid calling setIsRenamable for virtual registers since we assert that
224 // renamable property is only queried/set for physical registers.
225 if (TargetRegisterInfo::isPhysicalRegister(Reg1))
226 CommutedMI->getOperand(Idx2).setIsRenamable(Reg1IsRenamable);
227 if (TargetRegisterInfo::isPhysicalRegister(Reg2))
228 CommutedMI->getOperand(Idx1).setIsRenamable(Reg2IsRenamable);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000229 return CommutedMI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000230}
231
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000232MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr &MI, bool NewMI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000233 unsigned OpIdx1,
234 unsigned OpIdx2) const {
235 // If OpIdx1 or OpIdx2 is not specified, then this method is free to choose
236 // any commutable operand, which is done in findCommutedOpIndices() method
237 // called below.
238 if ((OpIdx1 == CommuteAnyOperandIndex || OpIdx2 == CommuteAnyOperandIndex) &&
239 !findCommutedOpIndices(MI, OpIdx1, OpIdx2)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240 assert(MI.isCommutable() &&
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000241 "Precondition violation: MI must be commutable.");
242 return nullptr;
243 }
244 return commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
245}
246
247bool TargetInstrInfo::fixCommutedOpIndices(unsigned &ResultIdx1,
248 unsigned &ResultIdx2,
249 unsigned CommutableOpIdx1,
250 unsigned CommutableOpIdx2) {
251 if (ResultIdx1 == CommuteAnyOperandIndex &&
252 ResultIdx2 == CommuteAnyOperandIndex) {
253 ResultIdx1 = CommutableOpIdx1;
254 ResultIdx2 = CommutableOpIdx2;
255 } else if (ResultIdx1 == CommuteAnyOperandIndex) {
256 if (ResultIdx2 == CommutableOpIdx1)
257 ResultIdx1 = CommutableOpIdx2;
258 else if (ResultIdx2 == CommutableOpIdx2)
259 ResultIdx1 = CommutableOpIdx1;
260 else
261 return false;
262 } else if (ResultIdx2 == CommuteAnyOperandIndex) {
263 if (ResultIdx1 == CommutableOpIdx1)
264 ResultIdx2 = CommutableOpIdx2;
265 else if (ResultIdx1 == CommutableOpIdx2)
266 ResultIdx2 = CommutableOpIdx1;
267 else
268 return false;
269 } else
270 // Check that the result operand indices match the given commutable
271 // operand indices.
272 return (ResultIdx1 == CommutableOpIdx1 && ResultIdx2 == CommutableOpIdx2) ||
273 (ResultIdx1 == CommutableOpIdx2 && ResultIdx2 == CommutableOpIdx1);
274
275 return true;
276}
277
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000278bool TargetInstrInfo::findCommutedOpIndices(MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000279 unsigned &SrcOpIdx1,
280 unsigned &SrcOpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000281 assert(!MI.isBundle() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000282 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
283
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000284 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000285 if (!MCID.isCommutable())
286 return false;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000287
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000288 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
289 // is not true, then the target must implement this.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000290 unsigned CommutableOpIdx1 = MCID.getNumDefs();
291 unsigned CommutableOpIdx2 = CommutableOpIdx1 + 1;
292 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2,
293 CommutableOpIdx1, CommutableOpIdx2))
294 return false;
295
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000296 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000297 // No idea.
298 return false;
299 return true;
300}
301
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000302bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
303 if (!MI.isTerminator()) return false;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000304
305 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000306 if (MI.isBranch() && !MI.isBarrier())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000307 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000308 if (!MI.isPredicable())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000309 return true;
310 return !isPredicated(MI);
311}
312
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000313bool TargetInstrInfo::PredicateInstruction(
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000314 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000315 bool MadeChange = false;
316
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000317 assert(!MI.isBundle() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000318 "TargetInstrInfo::PredicateInstruction() can't handle bundles");
319
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000320 const MCInstrDesc &MCID = MI.getDesc();
321 if (!MI.isPredicable())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000322 return false;
323
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000324 for (unsigned j = 0, i = 0, e = MI.getNumOperands(); i != e; ++i) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000325 if (MCID.OpInfo[i].isPredicate()) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000326 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000327 if (MO.isReg()) {
328 MO.setReg(Pred[j].getReg());
329 MadeChange = true;
330 } else if (MO.isImm()) {
331 MO.setImm(Pred[j].getImm());
332 MadeChange = true;
333 } else if (MO.isMBB()) {
334 MO.setMBB(Pred[j].getMBB());
335 MadeChange = true;
336 }
337 ++j;
338 }
339 }
340 return MadeChange;
341}
342
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000343bool TargetInstrInfo::hasLoadFromStackSlot(
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000344 const MachineInstr &MI,
345 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000346 size_t StartSize = Accesses.size();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000347 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
348 oe = MI.memoperands_end();
349 o != oe; ++o) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000350 if ((*o)->isLoad() &&
351 dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
352 Accesses.push_back(*o);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000353 }
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000354 return Accesses.size() != StartSize;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000355}
356
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000357bool TargetInstrInfo::hasStoreToStackSlot(
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000358 const MachineInstr &MI,
359 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000360 size_t StartSize = Accesses.size();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000361 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
362 oe = MI.memoperands_end();
363 o != oe; ++o) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000364 if ((*o)->isStore() &&
365 dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
366 Accesses.push_back(*o);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000367 }
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000368 return Accesses.size() != StartSize;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000369}
370
Andrew Trick10d5be42013-11-17 01:36:23 +0000371bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC,
372 unsigned SubIdx, unsigned &Size,
373 unsigned &Offset,
Eric Christopher7585fb22015-03-19 23:06:21 +0000374 const MachineFunction &MF) const {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000375 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Andrew Trick10d5be42013-11-17 01:36:23 +0000376 if (!SubIdx) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000377 Size = TRI->getSpillSize(*RC);
Andrew Trick10d5be42013-11-17 01:36:23 +0000378 Offset = 0;
379 return true;
380 }
Eric Christopher7585fb22015-03-19 23:06:21 +0000381 unsigned BitSize = TRI->getSubRegIdxSize(SubIdx);
Bjorn Petterssonc8b782c2018-08-09 15:19:07 +0000382 // Convert bit size to byte size.
Andrew Trick10d5be42013-11-17 01:36:23 +0000383 if (BitSize % 8)
384 return false;
385
Eric Christopher7585fb22015-03-19 23:06:21 +0000386 int BitOffset = TRI->getSubRegIdxOffset(SubIdx);
Andrew Trick10d5be42013-11-17 01:36:23 +0000387 if (BitOffset < 0 || BitOffset % 8)
388 return false;
389
390 Size = BitSize /= 8;
391 Offset = (unsigned)BitOffset / 8;
392
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000393 assert(TRI->getSpillSize(*RC) >= (Offset + Size) && "bad subregister range");
Andrew Trick10d5be42013-11-17 01:36:23 +0000394
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000395 if (!MF.getDataLayout().isLittleEndian()) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000396 Offset = TRI->getSpillSize(*RC) - (Offset + Size);
Andrew Trick10d5be42013-11-17 01:36:23 +0000397 }
398 return true;
399}
400
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000401void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB,
402 MachineBasicBlock::iterator I,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000403 unsigned DestReg, unsigned SubIdx,
404 const MachineInstr &Orig,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000405 const TargetRegisterInfo &TRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000406 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000407 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
408 MBB.insert(I, MI);
409}
410
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000411bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0,
412 const MachineInstr &MI1,
413 const MachineRegisterInfo *MRI) const {
414 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000415}
416
Matthias Braun55bc9b32017-08-22 23:56:30 +0000417MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
418 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000419 assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated");
Matthias Braun55bc9b32017-08-22 23:56:30 +0000420 MachineFunction &MF = *MBB.getParent();
421 return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000422}
423
424// If the COPY instruction in MI can be folded to a stack operation, return
425// the register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000426static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000427 unsigned FoldIdx) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000428 assert(MI.isCopy() && "MI must be a COPY instruction");
429 if (MI.getNumOperands() != 2)
Craig Topperc0196b12014-04-14 00:51:57 +0000430 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000431 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
432
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000433 const MachineOperand &FoldOp = MI.getOperand(FoldIdx);
434 const MachineOperand &LiveOp = MI.getOperand(1 - FoldIdx);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000435
436 if (FoldOp.getSubReg() || LiveOp.getSubReg())
Craig Topperc0196b12014-04-14 00:51:57 +0000437 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000438
439 unsigned FoldReg = FoldOp.getReg();
440 unsigned LiveReg = LiveOp.getReg();
441
442 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
443 "Cannot fold physregs");
444
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000445 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000446 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
447
448 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
Craig Topperc0196b12014-04-14 00:51:57 +0000449 return RC->contains(LiveOp.getReg()) ? RC : nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000450
451 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
452 return RC;
453
454 // FIXME: Allow folding when register classes are memory compatible.
Craig Topperc0196b12014-04-14 00:51:57 +0000455 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000456}
457
Hans Wennborg9b9a5352017-04-21 21:48:41 +0000458void TargetInstrInfo::getNoop(MCInst &NopInst) const {
459 llvm_unreachable("Not implemented");
Rafael Espindola6865d6f2014-09-15 18:32:58 +0000460}
461
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000462static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000463 ArrayRef<unsigned> Ops, int FrameIndex,
Lang Hames39609992013-11-29 03:07:54 +0000464 const TargetInstrInfo &TII) {
465 unsigned StartIdx = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000466 switch (MI.getOpcode()) {
Philip Reames570dd002016-08-23 21:21:43 +0000467 case TargetOpcode::STACKMAP: {
468 // StackMapLiveValues are foldable
Sanjoy Das6d3c9132016-08-30 01:38:59 +0000469 StartIdx = StackMapOpers(&MI).getVarIdx();
Lang Hames39609992013-11-29 03:07:54 +0000470 break;
Philip Reames570dd002016-08-23 21:21:43 +0000471 }
Lang Hames39609992013-11-29 03:07:54 +0000472 case TargetOpcode::PATCHPOINT: {
Philip Reames570dd002016-08-23 21:21:43 +0000473 // For PatchPoint, the call args are not foldable (even if reported in the
474 // stackmap e.g. via anyregcc).
Sanjoy Das6d3c9132016-08-30 01:38:59 +0000475 StartIdx = PatchPointOpers(&MI).getVarIdx();
Lang Hames39609992013-11-29 03:07:54 +0000476 break;
477 }
Philip Reames2b1084a2016-08-31 15:12:17 +0000478 case TargetOpcode::STATEPOINT: {
479 // For statepoints, fold deopt and gc arguments, but not call arguments.
480 StartIdx = StatepointOpers(&MI).getVarIdx();
481 break;
482 }
Lang Hames39609992013-11-29 03:07:54 +0000483 default:
484 llvm_unreachable("unexpected stackmap opcode");
485 }
486
487 // Return false if any operands requested for folding are not foldable (not
488 // part of the stackmap's live values).
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000489 for (unsigned Op : Ops) {
490 if (Op < StartIdx)
Craig Topperc0196b12014-04-14 00:51:57 +0000491 return nullptr;
Lang Hames39609992013-11-29 03:07:54 +0000492 }
493
494 MachineInstr *NewMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000495 MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
Lang Hames39609992013-11-29 03:07:54 +0000496 MachineInstrBuilder MIB(MF, NewMI);
497
498 // No need to fold return, the meta data, and function arguments
499 for (unsigned i = 0; i < StartIdx; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000500 MIB.add(MI.getOperand(i));
Lang Hames39609992013-11-29 03:07:54 +0000501
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000502 for (unsigned i = StartIdx; i < MI.getNumOperands(); ++i) {
503 MachineOperand &MO = MI.getOperand(i);
David Majnemer0d955d02016-08-11 22:21:41 +0000504 if (is_contained(Ops, i)) {
Lang Hames39609992013-11-29 03:07:54 +0000505 unsigned SpillSize;
506 unsigned SpillOffset;
507 // Compute the spill slot size and offset.
508 const TargetRegisterClass *RC =
509 MF.getRegInfo().getRegClass(MO.getReg());
Eric Christopher7585fb22015-03-19 23:06:21 +0000510 bool Valid =
511 TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize, SpillOffset, MF);
Lang Hames39609992013-11-29 03:07:54 +0000512 if (!Valid)
513 report_fatal_error("cannot spill patchpoint subregister operand");
514 MIB.addImm(StackMaps::IndirectMemRefOp);
515 MIB.addImm(SpillSize);
516 MIB.addFrameIndex(FrameIndex);
Lang Hames2ce64a72013-12-07 03:30:59 +0000517 MIB.addImm(SpillOffset);
Lang Hames39609992013-11-29 03:07:54 +0000518 }
519 else
Diana Picus116bbab2017-01-13 09:58:52 +0000520 MIB.add(MO);
Lang Hames39609992013-11-29 03:07:54 +0000521 }
522 return NewMI;
523}
524
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000525MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
526 ArrayRef<unsigned> Ops, int FI,
Jonas Paulssonfdc4ea32019-06-08 06:19:15 +0000527 LiveIntervals *LIS,
528 VirtRegMap *VRM) const {
Justin Lebar0af80cd2016-07-15 18:26:59 +0000529 auto Flags = MachineMemOperand::MONone;
Sanjay Patel232669d2017-10-02 15:02:06 +0000530 for (unsigned OpIdx : Ops)
531 Flags |= MI.getOperand(OpIdx).isDef() ? MachineMemOperand::MOStore
532 : MachineMemOperand::MOLoad;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000533
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000534 MachineBasicBlock *MBB = MI.getParent();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000535 assert(MBB && "foldMemoryOperand needs an inserted instruction");
536 MachineFunction &MF = *MBB->getParent();
537
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000538 // If we're not folding a load into a subreg, the size of the load is the
539 // size of the spill slot. But if we are, we need to figure out what the
540 // actual load size is.
541 int64_t MemSize = 0;
542 const MachineFrameInfo &MFI = MF.getFrameInfo();
543 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
544
545 if (Flags & MachineMemOperand::MOStore) {
546 MemSize = MFI.getObjectSize(FI);
547 } else {
Sanjay Patel232669d2017-10-02 15:02:06 +0000548 for (unsigned OpIdx : Ops) {
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000549 int64_t OpSize = MFI.getObjectSize(FI);
550
Sanjay Patel232669d2017-10-02 15:02:06 +0000551 if (auto SubReg = MI.getOperand(OpIdx).getSubReg()) {
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000552 unsigned SubRegSize = TRI->getSubRegIdxSize(SubReg);
553 if (SubRegSize > 0 && !(SubRegSize % 8))
554 OpSize = SubRegSize / 8;
555 }
556
557 MemSize = std::max(MemSize, OpSize);
558 }
559 }
560
561 assert(MemSize && "Did not expect a zero-sized stack slot");
562
Craig Topperc0196b12014-04-14 00:51:57 +0000563 MachineInstr *NewMI = nullptr;
Lang Hames39609992013-11-29 03:07:54 +0000564
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000565 if (MI.getOpcode() == TargetOpcode::STACKMAP ||
Philip Reames2b1084a2016-08-31 15:12:17 +0000566 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
567 MI.getOpcode() == TargetOpcode::STATEPOINT) {
Lang Hames39609992013-11-29 03:07:54 +0000568 // Fold stackmap/patchpoint.
569 NewMI = foldPatchpoint(MF, MI, Ops, FI, *this);
Keno Fischere70b31f2015-06-08 20:09:58 +0000570 if (NewMI)
571 MBB->insert(MI, NewMI);
Lang Hames39609992013-11-29 03:07:54 +0000572 } else {
573 // Ask the target to do the actual folding.
Jonas Paulssonfdc4ea32019-06-08 06:19:15 +0000574 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS, VRM);
Lang Hames39609992013-11-29 03:07:54 +0000575 }
Keno Fischere70b31f2015-06-08 20:09:58 +0000576
Lang Hames39609992013-11-29 03:07:54 +0000577 if (NewMI) {
Chandler Carruthc73c0302018-08-16 21:30:05 +0000578 NewMI->setMemRefs(MF, MI.memoperands());
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000579 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
580 assert((!(Flags & MachineMemOperand::MOStore) ||
581 NewMI->mayStore()) &&
582 "Folded a def to a non-store!");
583 assert((!(Flags & MachineMemOperand::MOLoad) ||
584 NewMI->mayLoad()) &&
585 "Folded a use to a non-load!");
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000586 assert(MFI.getObjectOffset(FI) != -1);
Alex Lorenze40c8a22015-08-11 23:09:45 +0000587 MachineMemOperand *MMO = MF.getMachineMemOperand(
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000588 MachinePointerInfo::getFixedStack(MF, FI), Flags, MemSize,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000589 MFI.getObjectAlignment(FI));
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000590 NewMI->addMemOperand(MF, MMO);
591
Keno Fischere70b31f2015-06-08 20:09:58 +0000592 return NewMI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000593 }
594
595 // Straight COPY may fold as load/store.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000596 if (!MI.isCopy() || Ops.size() != 1)
Craig Topperc0196b12014-04-14 00:51:57 +0000597 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000598
599 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
600 if (!RC)
Craig Topperc0196b12014-04-14 00:51:57 +0000601 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000602
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000603 const MachineOperand &MO = MI.getOperand(1 - Ops[0]);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000604 MachineBasicBlock::iterator Pos = MI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000605
606 if (Flags == MachineMemOperand::MOStore)
607 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
608 else
609 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
Duncan P. N. Exon Smithaae6f3c2016-07-01 16:38:28 +0000610 return &*--Pos;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000611}
612
Sanjay Patel7998d372017-10-02 14:03:17 +0000613MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
614 ArrayRef<unsigned> Ops,
615 MachineInstr &LoadMI,
616 LiveIntervals *LIS) const {
617 assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!");
618#ifndef NDEBUG
Sanjay Patel232669d2017-10-02 15:02:06 +0000619 for (unsigned OpIdx : Ops)
620 assert(MI.getOperand(OpIdx).isUse() && "Folding load into def!");
Sanjay Patel7998d372017-10-02 14:03:17 +0000621#endif
Sanjay Patel232669d2017-10-02 15:02:06 +0000622
Sanjay Patel7998d372017-10-02 14:03:17 +0000623 MachineBasicBlock &MBB = *MI.getParent();
624 MachineFunction &MF = *MBB.getParent();
625
626 // Ask the target to do the actual folding.
627 MachineInstr *NewMI = nullptr;
628 int FrameIndex = 0;
629
630 if ((MI.getOpcode() == TargetOpcode::STACKMAP ||
631 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
632 MI.getOpcode() == TargetOpcode::STATEPOINT) &&
633 isLoadFromStackSlot(LoadMI, FrameIndex)) {
634 // Fold stackmap/patchpoint.
635 NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this);
636 if (NewMI)
637 NewMI = &*MBB.insert(MI, NewMI);
638 } else {
639 // Ask the target to do the actual folding.
640 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS);
641 }
642
643 if (!NewMI)
644 return nullptr;
645
646 // Copy the memoperands from the load to the folded instruction.
647 if (MI.memoperands_empty()) {
Chandler Carruthc73c0302018-08-16 21:30:05 +0000648 NewMI->setMemRefs(MF, LoadMI.memoperands());
Sanjay Patel7998d372017-10-02 14:03:17 +0000649 } else {
650 // Handle the rare case of folding multiple loads.
Chandler Carruthc73c0302018-08-16 21:30:05 +0000651 NewMI->setMemRefs(MF, MI.memoperands());
Sanjay Patel7998d372017-10-02 14:03:17 +0000652 for (MachineInstr::mmo_iterator I = LoadMI.memoperands_begin(),
653 E = LoadMI.memoperands_end();
654 I != E; ++I) {
655 NewMI->addMemOperand(MF, *I);
656 }
657 }
658 return NewMI;
659}
660
Chad Rosier03a47302015-09-21 15:09:11 +0000661bool TargetInstrInfo::hasReassociableOperands(
662 const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
663 const MachineOperand &Op1 = Inst.getOperand(1);
664 const MachineOperand &Op2 = Inst.getOperand(2);
665 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
666
667 // We need virtual register definitions for the operands that we will
668 // reassociate.
669 MachineInstr *MI1 = nullptr;
670 MachineInstr *MI2 = nullptr;
671 if (Op1.isReg() && TargetRegisterInfo::isVirtualRegister(Op1.getReg()))
672 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
673 if (Op2.isReg() && TargetRegisterInfo::isVirtualRegister(Op2.getReg()))
674 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
675
676 // And they need to be in the trace (otherwise, they won't have a depth).
Rafael Espindola84921b92015-10-24 23:11:13 +0000677 return MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB;
Chad Rosier03a47302015-09-21 15:09:11 +0000678}
679
680bool TargetInstrInfo::hasReassociableSibling(const MachineInstr &Inst,
681 bool &Commuted) const {
682 const MachineBasicBlock *MBB = Inst.getParent();
683 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
684 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg());
685 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
686 unsigned AssocOpcode = Inst.getOpcode();
687
688 // If only one operand has the same opcode and it's the second source operand,
689 // the operands must be commuted.
690 Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode;
691 if (Commuted)
692 std::swap(MI1, MI2);
693
694 // 1. The previous instruction must be the same type as Inst.
695 // 2. The previous instruction must have virtual register definitions for its
696 // operands in the same basic block as Inst.
697 // 3. The previous instruction's result must only be used by Inst.
Rafael Espindola84921b92015-10-24 23:11:13 +0000698 return MI1->getOpcode() == AssocOpcode &&
699 hasReassociableOperands(*MI1, MBB) &&
700 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
Chad Rosier03a47302015-09-21 15:09:11 +0000701}
702
703// 1. The operation must be associative and commutative.
704// 2. The instruction must have virtual register definitions for its
705// operands in the same basic block.
706// 3. The instruction must have a reassociable sibling.
707bool TargetInstrInfo::isReassociationCandidate(const MachineInstr &Inst,
708 bool &Commuted) const {
Rafael Espindola84921b92015-10-24 23:11:13 +0000709 return isAssociativeAndCommutative(Inst) &&
710 hasReassociableOperands(Inst, Inst.getParent()) &&
711 hasReassociableSibling(Inst, Commuted);
Chad Rosier03a47302015-09-21 15:09:11 +0000712}
713
714// The concept of the reassociation pass is that these operations can benefit
715// from this kind of transformation:
716//
717// A = ? op ?
718// B = A op X (Prev)
719// C = B op Y (Root)
720// -->
721// A = ? op ?
722// B = X op Y
723// C = A op B
724//
725// breaking the dependency between A and B, allowing them to be executed in
726// parallel (or back-to-back in a pipeline) instead of depending on each other.
727
728// FIXME: This has the potential to be expensive (compile time) while not
729// improving the code at all. Some ways to limit the overhead:
730// 1. Track successful transforms; bail out if hit rate gets too low.
731// 2. Only enable at -O3 or some other non-default optimization level.
732// 3. Pre-screen pattern candidates here: if an operand of the previous
733// instruction is known to not increase the critical path, then don't match
734// that pattern.
735bool TargetInstrInfo::getMachineCombinerPatterns(
736 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000737 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Chad Rosier03a47302015-09-21 15:09:11 +0000738 bool Commute;
739 if (isReassociationCandidate(Root, Commute)) {
740 // We found a sequence of instructions that may be suitable for a
741 // reassociation of operands to increase ILP. Specify each commutation
742 // possibility for the Prev instruction in the sequence and let the
743 // machine combiner decide if changing the operands is worthwhile.
744 if (Commute) {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000745 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_YB);
746 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_YB);
Chad Rosier03a47302015-09-21 15:09:11 +0000747 } else {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000748 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_BY);
749 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_BY);
Chad Rosier03a47302015-09-21 15:09:11 +0000750 }
751 return true;
752 }
753
754 return false;
755}
Sanjay Patel7998d372017-10-02 14:03:17 +0000756
Gerolf Hoflehner01b3a6182016-04-24 05:14:01 +0000757/// Return true when a code sequence can improve loop throughput.
758bool
759TargetInstrInfo::isThroughputPattern(MachineCombinerPattern Pattern) const {
760 return false;
761}
Sanjay Patel7998d372017-10-02 14:03:17 +0000762
Chad Rosier03a47302015-09-21 15:09:11 +0000763/// Attempt the reassociation transformation to reduce critical path length.
764/// See the above comments before getMachineCombinerPatterns().
765void TargetInstrInfo::reassociateOps(
766 MachineInstr &Root, MachineInstr &Prev,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000767 MachineCombinerPattern Pattern,
Chad Rosier03a47302015-09-21 15:09:11 +0000768 SmallVectorImpl<MachineInstr *> &InsInstrs,
769 SmallVectorImpl<MachineInstr *> &DelInstrs,
770 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000771 MachineFunction *MF = Root.getMF();
Chad Rosier03a47302015-09-21 15:09:11 +0000772 MachineRegisterInfo &MRI = MF->getRegInfo();
773 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
774 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
775 const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI);
776
777 // This array encodes the operand index for each parameter because the
778 // operands may be commuted. Each row corresponds to a pattern value,
779 // and each column specifies the index of A, B, X, Y.
780 unsigned OpIdx[4][4] = {
781 { 1, 1, 2, 2 },
782 { 1, 2, 2, 1 },
783 { 2, 1, 1, 2 },
784 { 2, 2, 1, 1 }
785 };
786
Sanjay Patel387e66e2015-11-05 19:34:57 +0000787 int Row;
788 switch (Pattern) {
789 case MachineCombinerPattern::REASSOC_AX_BY: Row = 0; break;
790 case MachineCombinerPattern::REASSOC_AX_YB: Row = 1; break;
791 case MachineCombinerPattern::REASSOC_XA_BY: Row = 2; break;
792 case MachineCombinerPattern::REASSOC_XA_YB: Row = 3; break;
793 default: llvm_unreachable("unexpected MachineCombinerPattern");
794 }
795
796 MachineOperand &OpA = Prev.getOperand(OpIdx[Row][0]);
797 MachineOperand &OpB = Root.getOperand(OpIdx[Row][1]);
798 MachineOperand &OpX = Prev.getOperand(OpIdx[Row][2]);
799 MachineOperand &OpY = Root.getOperand(OpIdx[Row][3]);
Chad Rosier03a47302015-09-21 15:09:11 +0000800 MachineOperand &OpC = Root.getOperand(0);
801
802 unsigned RegA = OpA.getReg();
803 unsigned RegB = OpB.getReg();
804 unsigned RegX = OpX.getReg();
805 unsigned RegY = OpY.getReg();
806 unsigned RegC = OpC.getReg();
807
808 if (TargetRegisterInfo::isVirtualRegister(RegA))
809 MRI.constrainRegClass(RegA, RC);
810 if (TargetRegisterInfo::isVirtualRegister(RegB))
811 MRI.constrainRegClass(RegB, RC);
812 if (TargetRegisterInfo::isVirtualRegister(RegX))
813 MRI.constrainRegClass(RegX, RC);
814 if (TargetRegisterInfo::isVirtualRegister(RegY))
815 MRI.constrainRegClass(RegY, RC);
816 if (TargetRegisterInfo::isVirtualRegister(RegC))
817 MRI.constrainRegClass(RegC, RC);
818
819 // Create a new virtual register for the result of (X op Y) instead of
820 // recycling RegB because the MachineCombiner's computation of the critical
821 // path requires a new register definition rather than an existing one.
822 unsigned NewVR = MRI.createVirtualRegister(RC);
823 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
824
825 unsigned Opcode = Root.getOpcode();
826 bool KillA = OpA.isKill();
827 bool KillX = OpX.isKill();
828 bool KillY = OpY.isKill();
829
830 // Create new instructions for insertion.
831 MachineInstrBuilder MIB1 =
832 BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR)
833 .addReg(RegX, getKillRegState(KillX))
834 .addReg(RegY, getKillRegState(KillY));
835 MachineInstrBuilder MIB2 =
836 BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC)
837 .addReg(RegA, getKillRegState(KillA))
838 .addReg(NewVR, getKillRegState(true));
839
840 setSpecialOperandAttr(Root, Prev, *MIB1, *MIB2);
841
842 // Record new instructions for insertion and old instructions for deletion.
843 InsInstrs.push_back(MIB1);
844 InsInstrs.push_back(MIB2);
845 DelInstrs.push_back(&Prev);
846 DelInstrs.push_back(&Root);
847}
848
849void TargetInstrInfo::genAlternativeCodeSequence(
Sanjay Patel387e66e2015-11-05 19:34:57 +0000850 MachineInstr &Root, MachineCombinerPattern Pattern,
Chad Rosier03a47302015-09-21 15:09:11 +0000851 SmallVectorImpl<MachineInstr *> &InsInstrs,
852 SmallVectorImpl<MachineInstr *> &DelInstrs,
853 DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000854 MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
Chad Rosier03a47302015-09-21 15:09:11 +0000855
856 // Select the previous instruction in the sequence based on the input pattern.
857 MachineInstr *Prev = nullptr;
858 switch (Pattern) {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000859 case MachineCombinerPattern::REASSOC_AX_BY:
860 case MachineCombinerPattern::REASSOC_XA_BY:
Chad Rosier03a47302015-09-21 15:09:11 +0000861 Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
862 break;
Sanjay Patel387e66e2015-11-05 19:34:57 +0000863 case MachineCombinerPattern::REASSOC_AX_YB:
864 case MachineCombinerPattern::REASSOC_XA_YB:
Chad Rosier03a47302015-09-21 15:09:11 +0000865 Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
866 break;
867 default:
868 break;
869 }
870
871 assert(Prev && "Unknown pattern for machine combiner");
872
873 reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg);
Chad Rosier03a47302015-09-21 15:09:11 +0000874}
875
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000876bool TargetInstrInfo::isReallyTriviallyReMaterializableGeneric(
877 const MachineInstr &MI, AliasAnalysis *AA) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000878 const MachineFunction &MF = *MI.getMF();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000879 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000880
881 // Remat clients assume operand 0 is the defined register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000882 if (!MI.getNumOperands() || !MI.getOperand(0).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000883 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000884 unsigned DefReg = MI.getOperand(0).getReg();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000885
886 // A sub-register definition can only be rematerialized if the instruction
887 // doesn't read the other parts of the register. Otherwise it is really a
888 // read-modify-write operation on the full virtual register which cannot be
889 // moved safely.
890 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000891 MI.getOperand(0).getSubReg() && MI.readsVirtualRegister(DefReg))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000892 return false;
893
894 // A load from a fixed stack slot can be rematerialized. This may be
895 // redundant with subsequent checks, but it's target-independent,
896 // simple, and a common case.
897 int FrameIdx = 0;
Eric Christopher9d916792014-07-23 22:12:03 +0000898 if (isLoadFromStackSlot(MI, FrameIdx) &&
Matthias Braun941a7052016-07-28 18:40:00 +0000899 MF.getFrameInfo().isImmutableObjectIndex(FrameIdx))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000900 return true;
901
902 // Avoid instructions obviously unsafe for remat.
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000903 if (MI.isNotDuplicable() || MI.mayStore() || MI.mayRaiseFPException() ||
904 MI.hasUnmodeledSideEffects())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000905 return false;
906
907 // Don't remat inline asm. We have no idea how expensive it is
908 // even if it's side effect free.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000909 if (MI.isInlineAsm())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000910 return false;
911
912 // Avoid instructions which load from potentially varying memory.
Justin Lebard98cf002016-09-10 01:03:20 +0000913 if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000914 return false;
915
916 // If any of the registers accessed are non-constant, conservatively assume
917 // the instruction is not rematerializable.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000918 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
919 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000920 if (!MO.isReg()) continue;
921 unsigned Reg = MO.getReg();
922 if (Reg == 0)
923 continue;
924
925 // Check for a well-behaved physical register.
926 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
927 if (MO.isUse()) {
928 // If the physreg has no defs anywhere, it's just an ambient register
929 // and we can freely move its uses. Alternatively, if it's allocatable,
930 // it could get allocated to something with a def during allocation.
Matthias Braunde8c1b32016-10-28 18:05:09 +0000931 if (!MRI.isConstantPhysReg(Reg))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000932 return false;
933 } else {
934 // A physreg def. We can't remat it.
935 return false;
936 }
937 continue;
938 }
939
940 // Only allow one virtual-register def. There may be multiple defs of the
941 // same virtual register, though.
942 if (MO.isDef() && Reg != DefReg)
943 return false;
944
945 // Don't allow any virtual-register uses. Rematting an instruction with
946 // virtual register uses would length the live ranges of the uses, which
947 // is not necessarily a good idea, certainly not "trivial".
948 if (MO.isUse())
949 return false;
950 }
951
952 // Everything checked out.
953 return true;
954}
955
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000956int TargetInstrInfo::getSPAdjust(const MachineInstr &MI) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000957 const MachineFunction *MF = MI.getMF();
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000958 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
959 bool StackGrowsDown =
960 TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
961
Matthias Braunfa3872e2015-05-18 20:27:55 +0000962 unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
963 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000964
Serge Pavlov49acf9c2017-04-13 14:10:52 +0000965 if (!isFrameInstr(MI))
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000966 return 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000967
Serge Pavlov49acf9c2017-04-13 14:10:52 +0000968 int SPAdj = TFI->alignSPAdjust(getFrameSize(MI));
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000969
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000970 if ((!StackGrowsDown && MI.getOpcode() == FrameSetupOpcode) ||
971 (StackGrowsDown && MI.getOpcode() == FrameDestroyOpcode))
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000972 SPAdj = -SPAdj;
973
974 return SPAdj;
975}
976
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000977/// isSchedulingBoundary - Test if the given instruction should be
978/// considered a scheduling boundary. This primarily includes labels
979/// and terminators.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000980bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000981 const MachineBasicBlock *MBB,
982 const MachineFunction &MF) const {
983 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000984 if (MI.isTerminator() || MI.isPosition())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000985 return true;
986
987 // Don't attempt to schedule around any instruction that defines
988 // a stack-oriented pointer, as it's unlikely to be profitable. This
989 // saves compile time, because it doesn't require every single
990 // stack slot reference to depend on the instruction that does the
991 // modification.
Eric Christopherfc6de422014-08-05 02:39:49 +0000992 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
993 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000994 return MI.modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000995}
996
997// Provide a global flag for disabling the PreRA hazard recognizer that targets
998// may choose to honor.
999bool TargetInstrInfo::usePreRAHazardRecognizer() const {
1000 return !DisableHazardRecognizer;
1001}
1002
1003// Default implementation of CreateTargetRAHazardRecognizer.
1004ScheduleHazardRecognizer *TargetInstrInfo::
Eric Christopherf047bfd2014-06-13 22:38:52 +00001005CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001006 const ScheduleDAG *DAG) const {
1007 // Dummy hazard recognizer allows all instructions to issue.
1008 return new ScheduleHazardRecognizer();
1009}
1010
1011// Default implementation of CreateTargetMIHazardRecognizer.
1012ScheduleHazardRecognizer *TargetInstrInfo::
1013CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
1014 const ScheduleDAG *DAG) const {
1015 return (ScheduleHazardRecognizer *)
Austin Kerbow6e6480e2019-05-07 22:09:04 +00001016 new ScoreboardHazardRecognizer(II, DAG, "machine-scheduler");
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001017}
1018
1019// Default implementation of CreateTargetPostRAHazardRecognizer.
1020ScheduleHazardRecognizer *TargetInstrInfo::
1021CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
1022 const ScheduleDAG *DAG) const {
1023 return (ScheduleHazardRecognizer *)
1024 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
1025}
1026
1027//===----------------------------------------------------------------------===//
1028// SelectionDAG latency interface.
1029//===----------------------------------------------------------------------===//
1030
1031int
1032TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1033 SDNode *DefNode, unsigned DefIdx,
1034 SDNode *UseNode, unsigned UseIdx) const {
1035 if (!ItinData || ItinData->isEmpty())
1036 return -1;
1037
1038 if (!DefNode->isMachineOpcode())
1039 return -1;
1040
1041 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
1042 if (!UseNode->isMachineOpcode())
1043 return ItinData->getOperandCycle(DefClass, DefIdx);
1044 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
1045 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1046}
1047
1048int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1049 SDNode *N) const {
1050 if (!ItinData || ItinData->isEmpty())
1051 return 1;
1052
1053 if (!N->isMachineOpcode())
1054 return 1;
1055
1056 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
1057}
1058
1059//===----------------------------------------------------------------------===//
1060// MachineInstr latency interface.
1061//===----------------------------------------------------------------------===//
1062
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001063unsigned TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1064 const MachineInstr &MI) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001065 if (!ItinData || ItinData->isEmpty())
1066 return 1;
1067
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001068 unsigned Class = MI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001069 int UOps = ItinData->Itineraries[Class].NumMicroOps;
1070 if (UOps >= 0)
1071 return UOps;
1072
1073 // The # of u-ops is dynamically determined. The specific target should
1074 // override this function to return the right number.
1075 return 1;
1076}
1077
1078/// Return the default expected latency for a def based on it's opcode.
Pete Cooper11759452014-09-02 17:43:54 +00001079unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel &SchedModel,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001080 const MachineInstr &DefMI) const {
1081 if (DefMI.isTransient())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001082 return 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 if (DefMI.mayLoad())
Pete Cooper11759452014-09-02 17:43:54 +00001084 return SchedModel.LoadLatency;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001085 if (isHighLatencyDef(DefMI.getOpcode()))
Pete Cooper11759452014-09-02 17:43:54 +00001086 return SchedModel.HighLatency;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001087 return 1;
1088}
1089
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001090unsigned TargetInstrInfo::getPredicationCost(const MachineInstr &) const {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00001091 return 0;
1092}
1093
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001094unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1095 const MachineInstr &MI,
1096 unsigned *PredCost) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001097 // Default to one cycle for no itinerary. However, an "empty" itinerary may
1098 // still have a MinLatency property, which getStageLatency checks.
1099 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001100 return MI.mayLoad() ? 2 : 1;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001101
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001102 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001103}
1104
Matthias Braun88e21312015-06-13 03:42:11 +00001105bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001106 const MachineInstr &DefMI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001107 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00001108 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001109 if (!ItinData || ItinData->isEmpty())
1110 return false;
1111
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001112 unsigned DefClass = DefMI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001113 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1114 return (DefCycle != -1 && DefCycle <= 1);
1115}
1116
1117/// Both DefMI and UseMI must be valid. By default, call directly to the
1118/// itinerary. This may be overriden by the target.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001119int TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1120 const MachineInstr &DefMI,
1121 unsigned DefIdx,
1122 const MachineInstr &UseMI,
1123 unsigned UseIdx) const {
1124 unsigned DefClass = DefMI.getDesc().getSchedClass();
1125 unsigned UseClass = UseMI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001126 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1127}
1128
1129/// If we can determine the operand latency from the def only, without itinerary
1130/// lookup, do so. Otherwise return -1.
1131int TargetInstrInfo::computeDefOperandLatency(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001132 const InstrItineraryData *ItinData, const MachineInstr &DefMI) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001133
1134 // Let the target hook getInstrLatency handle missing itineraries.
1135 if (!ItinData)
1136 return getInstrLatency(ItinData, DefMI);
1137
Andrew Trickde2109e2013-06-15 04:49:57 +00001138 if(ItinData->isEmpty())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001139 return defaultDefLatency(ItinData->SchedModel, DefMI);
1140
1141 // ...operand lookup required
1142 return -1;
1143}
1144
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001145bool TargetInstrInfo::getRegSequenceInputs(
1146 const MachineInstr &MI, unsigned DefIdx,
1147 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
Quentin Colombet8427df92014-08-12 17:11:26 +00001148 assert((MI.isRegSequence() ||
1149 MI.isRegSequenceLike()) && "Instruction do not have the proper type");
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001150
1151 if (!MI.isRegSequence())
1152 return getRegSequenceLikeInputs(MI, DefIdx, InputRegs);
1153
1154 // We are looking at:
1155 // Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
1156 assert(DefIdx == 0 && "REG_SEQUENCE only has one def");
1157 for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
1158 OpIdx += 2) {
1159 const MachineOperand &MOReg = MI.getOperand(OpIdx);
Matthias Braunea4359e2018-01-11 22:30:43 +00001160 if (MOReg.isUndef())
1161 continue;
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001162 const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1);
1163 assert(MOSubIdx.isImm() &&
1164 "One of the subindex of the reg_sequence is not an immediate");
1165 // Record Reg:SubReg, SubIdx.
1166 InputRegs.push_back(RegSubRegPairAndIdx(MOReg.getReg(), MOReg.getSubReg(),
1167 (unsigned)MOSubIdx.getImm()));
1168 }
1169 return true;
1170}
Quentin Colombet7e75cba2014-08-20 21:51:26 +00001171
1172bool TargetInstrInfo::getExtractSubregInputs(
1173 const MachineInstr &MI, unsigned DefIdx,
1174 RegSubRegPairAndIdx &InputReg) const {
1175 assert((MI.isExtractSubreg() ||
1176 MI.isExtractSubregLike()) && "Instruction do not have the proper type");
1177
1178 if (!MI.isExtractSubreg())
1179 return getExtractSubregLikeInputs(MI, DefIdx, InputReg);
1180
1181 // We are looking at:
1182 // Def = EXTRACT_SUBREG v0.sub1, sub0.
1183 assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def");
1184 const MachineOperand &MOReg = MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00001185 if (MOReg.isUndef())
1186 return false;
Quentin Colombet7e75cba2014-08-20 21:51:26 +00001187 const MachineOperand &MOSubIdx = MI.getOperand(2);
1188 assert(MOSubIdx.isImm() &&
1189 "The subindex of the extract_subreg is not an immediate");
1190
1191 InputReg.Reg = MOReg.getReg();
1192 InputReg.SubReg = MOReg.getSubReg();
1193 InputReg.SubIdx = (unsigned)MOSubIdx.getImm();
1194 return true;
1195}
Quentin Colombet7e3da662014-08-20 23:49:36 +00001196
1197bool TargetInstrInfo::getInsertSubregInputs(
1198 const MachineInstr &MI, unsigned DefIdx,
1199 RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const {
1200 assert((MI.isInsertSubreg() ||
1201 MI.isInsertSubregLike()) && "Instruction do not have the proper type");
1202
1203 if (!MI.isInsertSubreg())
1204 return getInsertSubregLikeInputs(MI, DefIdx, BaseReg, InsertedReg);
1205
1206 // We are looking at:
1207 // Def = INSERT_SEQUENCE v0, v1, sub0.
1208 assert(DefIdx == 0 && "INSERT_SUBREG only has one def");
1209 const MachineOperand &MOBaseReg = MI.getOperand(1);
1210 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00001211 if (MOInsertedReg.isUndef())
1212 return false;
Quentin Colombet7e3da662014-08-20 23:49:36 +00001213 const MachineOperand &MOSubIdx = MI.getOperand(3);
1214 assert(MOSubIdx.isImm() &&
1215 "One of the subindex of the reg_sequence is not an immediate");
1216 BaseReg.Reg = MOBaseReg.getReg();
1217 BaseReg.SubReg = MOBaseReg.getSubReg();
1218
1219 InsertedReg.Reg = MOInsertedReg.getReg();
1220 InsertedReg.SubReg = MOInsertedReg.getSubReg();
1221 InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm();
1222 return true;
1223}