blob: 963f8178b5095b96d7aab3c36ae2d0b4b25cfbd1 [file] [log] [blame]
Chris Lattner0d5644b2003-01-13 00:26:36 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner910b82f2002-10-28 23:55:33 +00009//
Chris Lattnerf6932b72005-01-19 06:53:34 +000010// This file implements the TargetInstrInfo class.
Chris Lattner910b82f2002-10-28 23:55:33 +000011//
12//===----------------------------------------------------------------------===//
13
David Blaikie3f833ed2017-11-08 01:01:31 +000014#include "llvm/CodeGen/TargetInstrInfo.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Lang Hames39609992013-11-29 03:07:54 +000016#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000017#include "llvm/CodeGen/MachineMemOperand.h"
18#include "llvm/CodeGen/MachineRegisterInfo.h"
19#include "llvm/CodeGen/PseudoSourceValue.h"
20#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Lang Hames39609992013-11-29 03:07:54 +000021#include "llvm/CodeGen/StackMaps.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetLowering.h"
24#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun88e21312015-06-13 03:42:11 +000025#include "llvm/CodeGen/TargetSchedule.h"
Andrew Trick10d5be42013-11-17 01:36:23 +000026#include "llvm/IR/DataLayout.h"
Evan Cheng49d4c0b2010-10-06 06:27:31 +000027#include "llvm/MC/MCAsmInfo.h"
Evan Cheng8264e272011-06-29 01:14:12 +000028#include "llvm/MC/MCInstrItineraries.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner01614192009-08-02 04:58:19 +000030#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000031#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000032#include "llvm/Target/TargetMachine.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000033#include <cctype>
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000034
Chris Lattnerf6932b72005-01-19 06:53:34 +000035using namespace llvm;
Chris Lattner910b82f2002-10-28 23:55:33 +000036
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000037static cl::opt<bool> DisableHazardRecognizer(
38 "disable-sched-hazard", cl::Hidden, cl::init(false),
39 cl::desc("Disable hazard detection during preRA scheduling"));
Chris Lattnere98a3c32009-08-02 05:20:37 +000040
Chris Lattner0d5644b2003-01-13 00:26:36 +000041TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner910b82f2002-10-28 23:55:33 +000042}
43
Evan Cheng8d71a752011-06-27 21:26:13 +000044const TargetRegisterClass*
Evan Cheng6cc775f2011-06-28 19:10:37 +000045TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000046 const TargetRegisterInfo *TRI,
47 const MachineFunction &MF) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +000048 if (OpNum >= MCID.getNumOperands())
Craig Topperc0196b12014-04-14 00:51:57 +000049 return nullptr;
Evan Cheng8d71a752011-06-27 21:26:13 +000050
Evan Cheng6cc775f2011-06-28 19:10:37 +000051 short RegClass = MCID.OpInfo[OpNum].RegClass;
52 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000053 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng8d71a752011-06-27 21:26:13 +000054
55 // Instructions like INSERT_SUBREG do not have fixed register classes.
56 if (RegClass < 0)
Craig Topperc0196b12014-04-14 00:51:57 +000057 return nullptr;
Evan Cheng8d71a752011-06-27 21:26:13 +000058
59 // Otherwise just look it up normally.
60 return TRI->getRegClass(RegClass);
61}
62
Chris Lattner01614192009-08-02 04:58:19 +000063/// insertNoop - Insert a noop into the instruction stream at the specified
64/// point.
Andrew Trickc416ba62010-12-24 04:28:06 +000065void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattner01614192009-08-02 04:58:19 +000066 MachineBasicBlock::iterator MI) const {
67 llvm_unreachable("Target didn't implement insertNoop!");
68}
69
Alex Bradbury5518cbf2017-09-28 09:31:46 +000070static bool isAsmComment(const char *Str, const MCAsmInfo &MAI) {
71 return strncmp(Str, MAI.getCommentString().data(),
72 MAI.getCommentString().size()) == 0;
73}
74
Chris Lattnere98a3c32009-08-02 05:20:37 +000075/// Measure the specified inline asm to determine an approximation of its
76/// length.
Jim Grosbacha3df87f2011-03-24 18:46:34 +000077/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnere98a3c32009-08-02 05:20:37 +000078/// count as an instruction.
79/// Any other non-whitespace text is considered an instruction, with
Jim Grosbacha3df87f2011-03-24 18:46:34 +000080/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnere98a3c32009-08-02 05:20:37 +000081/// Variable-length instructions are not handled here; this function
82/// may be overloaded in the target code to do that.
Alex Bradbury5518cbf2017-09-28 09:31:46 +000083/// We implement a special case of the .space directive which takes only a
84/// single integer argument in base 10 that is the size in bytes. This is a
85/// restricted form of the GAS directive in that we only interpret
86/// simple--i.e. not a logical or arithmetic expression--size values without
87/// the optional fill value. This is primarily used for creating arbitrary
88/// sized inline asm blocks for testing purposes.
Chris Lattnere98a3c32009-08-02 05:20:37 +000089unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattnere9a75a62009-08-22 21:43:10 +000090 const MCAsmInfo &MAI) 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;
Chris Lattnere98a3c32009-08-02 05:20:37 +000094 for (; *Str; ++Str) {
Jim Grosbacha3df87f2011-03-24 18:46:34 +000095 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
Matt Arsenaultaccddac2016-07-01 23:26:50 +000096 strlen(MAI.getSeparatorString())) == 0) {
Alex Bradbury5518cbf2017-09-28 09:31:46 +000097 AtInsnStart = true;
98 } else if (isAsmComment(Str, MAI)) {
Matt Arsenaultaccddac2016-07-01 23:26:50 +000099 // Stop counting as an instruction after a comment until the next
100 // separator.
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000101 AtInsnStart = false;
Chris Lattnere98a3c32009-08-02 05:20:37 +0000102 }
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000103
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000104 if (AtInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
105 unsigned AddLength = MAI.getMaxInstLength();
106 if (strncmp(Str, ".space", 6) == 0) {
107 char *EStr;
108 int SpaceSize;
109 SpaceSize = strtol(Str + 6, &EStr, 10);
110 SpaceSize = SpaceSize < 0 ? 0 : SpaceSize;
111 while (*EStr != '\n' && std::isspace(static_cast<unsigned char>(*EStr)))
112 ++EStr;
113 if (*EStr == '\0' || *EStr == '\n' ||
114 isAsmComment(EStr, MAI)) // Successfully parsed .space argument
115 AddLength = SpaceSize;
116 }
117 Length += AddLength;
118 AtInsnStart = false;
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000119 }
Chris Lattnere98a3c32009-08-02 05:20:37 +0000120 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000121
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000122 return Length;
Chris Lattnere98a3c32009-08-02 05:20:37 +0000123}
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000124
125/// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
126/// after it, replacing it with an unconditional branch to NewDest.
127void
128TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
129 MachineBasicBlock *NewDest) const {
130 MachineBasicBlock *MBB = Tail->getParent();
131
132 // Remove all the old successors of MBB from the CFG.
133 while (!MBB->succ_empty())
134 MBB->removeSuccessor(MBB->succ_begin());
135
Justin Bognerec5ea362016-03-25 18:38:48 +0000136 // Save off the debug loc before erasing the instruction.
137 DebugLoc DL = Tail->getDebugLoc();
138
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000139 // Remove all the dead instructions from the end of MBB.
140 MBB->erase(Tail, MBB->end());
141
142 // If MBB isn't immediately before MBB, insert a branch to it.
143 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000144 insertBranch(*MBB, NewDest, nullptr, SmallVector<MachineOperand, 0>(), DL);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000145 MBB->addSuccessor(NewDest);
146}
147
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148MachineInstr *TargetInstrInfo::commuteInstructionImpl(MachineInstr &MI,
149 bool NewMI, unsigned Idx1,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000150 unsigned Idx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000151 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000152 bool HasDef = MCID.getNumDefs();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000153 if (HasDef && !MI.getOperand(0).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000154 // No idea how to commute this instruction. Target should implement its own.
Craig Topperc0196b12014-04-14 00:51:57 +0000155 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000156
Richard Trieue778e872015-09-28 22:54:43 +0000157 unsigned CommutableOpIdx1 = Idx1; (void)CommutableOpIdx1;
158 unsigned CommutableOpIdx2 = Idx2; (void)CommutableOpIdx2;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000159 assert(findCommutedOpIndices(MI, CommutableOpIdx1, CommutableOpIdx2) &&
160 CommutableOpIdx1 == Idx1 && CommutableOpIdx2 == Idx2 &&
161 "TargetInstrInfo::CommuteInstructionImpl(): not commutable operands.");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000162 assert(MI.getOperand(Idx1).isReg() && MI.getOperand(Idx2).isReg() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000163 "This only knows how to commute register operands so far");
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000164
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000165 unsigned Reg0 = HasDef ? MI.getOperand(0).getReg() : 0;
166 unsigned Reg1 = MI.getOperand(Idx1).getReg();
167 unsigned Reg2 = MI.getOperand(Idx2).getReg();
168 unsigned SubReg0 = HasDef ? MI.getOperand(0).getSubReg() : 0;
169 unsigned SubReg1 = MI.getOperand(Idx1).getSubReg();
170 unsigned SubReg2 = MI.getOperand(Idx2).getSubReg();
171 bool Reg1IsKill = MI.getOperand(Idx1).isKill();
172 bool Reg2IsKill = MI.getOperand(Idx2).isKill();
173 bool Reg1IsUndef = MI.getOperand(Idx1).isUndef();
174 bool Reg2IsUndef = MI.getOperand(Idx2).isUndef();
175 bool Reg1IsInternal = MI.getOperand(Idx1).isInternalRead();
176 bool Reg2IsInternal = MI.getOperand(Idx2).isInternalRead();
Geoff Berryd37dc772018-01-29 18:47:48 +0000177 // Avoid calling isRenamable for virtual registers since we assert that
178 // renamable property is only queried/set for physical registers.
179 bool Reg1IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg1)
180 ? MI.getOperand(Idx1).isRenamable()
181 : false;
182 bool Reg2IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg2)
183 ? MI.getOperand(Idx2).isRenamable()
184 : false;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000185 // If destination is tied to either of the commuted source register, then
186 // it must be updated.
187 if (HasDef && Reg0 == Reg1 &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000188 MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000189 Reg2IsKill = false;
190 Reg0 = Reg2;
191 SubReg0 = SubReg2;
192 } else if (HasDef && Reg0 == Reg2 &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000193 MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000194 Reg1IsKill = false;
195 Reg0 = Reg1;
196 SubReg0 = SubReg1;
197 }
198
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000199 MachineInstr *CommutedMI = nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000200 if (NewMI) {
201 // Create a new instruction.
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000202 MachineFunction &MF = *MI.getMF();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000203 CommutedMI = MF.CloneMachineInstr(&MI);
204 } else {
205 CommutedMI = &MI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000206 }
207
208 if (HasDef) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000209 CommutedMI->getOperand(0).setReg(Reg0);
210 CommutedMI->getOperand(0).setSubReg(SubReg0);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000211 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000212 CommutedMI->getOperand(Idx2).setReg(Reg1);
213 CommutedMI->getOperand(Idx1).setReg(Reg2);
214 CommutedMI->getOperand(Idx2).setSubReg(SubReg1);
215 CommutedMI->getOperand(Idx1).setSubReg(SubReg2);
216 CommutedMI->getOperand(Idx2).setIsKill(Reg1IsKill);
217 CommutedMI->getOperand(Idx1).setIsKill(Reg2IsKill);
218 CommutedMI->getOperand(Idx2).setIsUndef(Reg1IsUndef);
219 CommutedMI->getOperand(Idx1).setIsUndef(Reg2IsUndef);
220 CommutedMI->getOperand(Idx2).setIsInternalRead(Reg1IsInternal);
221 CommutedMI->getOperand(Idx1).setIsInternalRead(Reg2IsInternal);
Geoff Berryd37dc772018-01-29 18:47:48 +0000222 // Avoid calling setIsRenamable for virtual registers since we assert that
223 // renamable property is only queried/set for physical registers.
224 if (TargetRegisterInfo::isPhysicalRegister(Reg1))
225 CommutedMI->getOperand(Idx2).setIsRenamable(Reg1IsRenamable);
226 if (TargetRegisterInfo::isPhysicalRegister(Reg2))
227 CommutedMI->getOperand(Idx1).setIsRenamable(Reg2IsRenamable);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000228 return CommutedMI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000229}
230
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000231MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr &MI, bool NewMI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000232 unsigned OpIdx1,
233 unsigned OpIdx2) const {
234 // If OpIdx1 or OpIdx2 is not specified, then this method is free to choose
235 // any commutable operand, which is done in findCommutedOpIndices() method
236 // called below.
237 if ((OpIdx1 == CommuteAnyOperandIndex || OpIdx2 == CommuteAnyOperandIndex) &&
238 !findCommutedOpIndices(MI, OpIdx1, OpIdx2)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 assert(MI.isCommutable() &&
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000240 "Precondition violation: MI must be commutable.");
241 return nullptr;
242 }
243 return commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
244}
245
246bool TargetInstrInfo::fixCommutedOpIndices(unsigned &ResultIdx1,
247 unsigned &ResultIdx2,
248 unsigned CommutableOpIdx1,
249 unsigned CommutableOpIdx2) {
250 if (ResultIdx1 == CommuteAnyOperandIndex &&
251 ResultIdx2 == CommuteAnyOperandIndex) {
252 ResultIdx1 = CommutableOpIdx1;
253 ResultIdx2 = CommutableOpIdx2;
254 } else if (ResultIdx1 == CommuteAnyOperandIndex) {
255 if (ResultIdx2 == CommutableOpIdx1)
256 ResultIdx1 = CommutableOpIdx2;
257 else if (ResultIdx2 == CommutableOpIdx2)
258 ResultIdx1 = CommutableOpIdx1;
259 else
260 return false;
261 } else if (ResultIdx2 == CommuteAnyOperandIndex) {
262 if (ResultIdx1 == CommutableOpIdx1)
263 ResultIdx2 = CommutableOpIdx2;
264 else if (ResultIdx1 == CommutableOpIdx2)
265 ResultIdx2 = CommutableOpIdx1;
266 else
267 return false;
268 } else
269 // Check that the result operand indices match the given commutable
270 // operand indices.
271 return (ResultIdx1 == CommutableOpIdx1 && ResultIdx2 == CommutableOpIdx2) ||
272 (ResultIdx1 == CommutableOpIdx2 && ResultIdx2 == CommutableOpIdx1);
273
274 return true;
275}
276
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000277bool TargetInstrInfo::findCommutedOpIndices(MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000278 unsigned &SrcOpIdx1,
279 unsigned &SrcOpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000280 assert(!MI.isBundle() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000281 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
282
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000283 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000284 if (!MCID.isCommutable())
285 return false;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000286
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000287 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
288 // is not true, then the target must implement this.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000289 unsigned CommutableOpIdx1 = MCID.getNumDefs();
290 unsigned CommutableOpIdx2 = CommutableOpIdx1 + 1;
291 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2,
292 CommutableOpIdx1, CommutableOpIdx2))
293 return false;
294
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000295 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000296 // No idea.
297 return false;
298 return true;
299}
300
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000301bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
302 if (!MI.isTerminator()) return false;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000303
304 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000305 if (MI.isBranch() && !MI.isBarrier())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000306 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000307 if (!MI.isPredicable())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000308 return true;
309 return !isPredicated(MI);
310}
311
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000312bool TargetInstrInfo::PredicateInstruction(
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000313 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000314 bool MadeChange = false;
315
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000316 assert(!MI.isBundle() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000317 "TargetInstrInfo::PredicateInstruction() can't handle bundles");
318
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000319 const MCInstrDesc &MCID = MI.getDesc();
320 if (!MI.isPredicable())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000321 return false;
322
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000323 for (unsigned j = 0, i = 0, e = MI.getNumOperands(); i != e; ++i) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000324 if (MCID.OpInfo[i].isPredicate()) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000325 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000326 if (MO.isReg()) {
327 MO.setReg(Pred[j].getReg());
328 MadeChange = true;
329 } else if (MO.isImm()) {
330 MO.setImm(Pred[j].getImm());
331 MadeChange = true;
332 } else if (MO.isMBB()) {
333 MO.setMBB(Pred[j].getMBB());
334 MadeChange = true;
335 }
336 ++j;
337 }
338 }
339 return MadeChange;
340}
341
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000342bool TargetInstrInfo::hasLoadFromStackSlot(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000343 const MachineMemOperand *&MMO,
344 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000345 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
346 oe = MI.memoperands_end();
347 o != oe; ++o) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000348 if ((*o)->isLoad()) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000349 if (const FixedStackPseudoSourceValue *Value =
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000350 dyn_cast_or_null<FixedStackPseudoSourceValue>(
351 (*o)->getPseudoValue())) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000352 FrameIndex = Value->getFrameIndex();
353 MMO = *o;
354 return true;
355 }
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000356 }
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000357 }
358 return false;
359}
360
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000361bool TargetInstrInfo::hasStoreToStackSlot(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000362 const MachineMemOperand *&MMO,
363 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000364 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
365 oe = MI.memoperands_end();
366 o != oe; ++o) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000367 if ((*o)->isStore()) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000368 if (const FixedStackPseudoSourceValue *Value =
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000369 dyn_cast_or_null<FixedStackPseudoSourceValue>(
370 (*o)->getPseudoValue())) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000371 FrameIndex = Value->getFrameIndex();
372 MMO = *o;
373 return true;
374 }
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000375 }
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000376 }
377 return false;
378}
379
Andrew Trick10d5be42013-11-17 01:36:23 +0000380bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC,
381 unsigned SubIdx, unsigned &Size,
382 unsigned &Offset,
Eric Christopher7585fb22015-03-19 23:06:21 +0000383 const MachineFunction &MF) const {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000384 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Andrew Trick10d5be42013-11-17 01:36:23 +0000385 if (!SubIdx) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000386 Size = TRI->getSpillSize(*RC);
Andrew Trick10d5be42013-11-17 01:36:23 +0000387 Offset = 0;
388 return true;
389 }
Eric Christopher7585fb22015-03-19 23:06:21 +0000390 unsigned BitSize = TRI->getSubRegIdxSize(SubIdx);
Andrew Trick10d5be42013-11-17 01:36:23 +0000391 // Convert bit size to byte size to be consistent with
392 // MCRegisterClass::getSize().
393 if (BitSize % 8)
394 return false;
395
Eric Christopher7585fb22015-03-19 23:06:21 +0000396 int BitOffset = TRI->getSubRegIdxOffset(SubIdx);
Andrew Trick10d5be42013-11-17 01:36:23 +0000397 if (BitOffset < 0 || BitOffset % 8)
398 return false;
399
400 Size = BitSize /= 8;
401 Offset = (unsigned)BitOffset / 8;
402
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000403 assert(TRI->getSpillSize(*RC) >= (Offset + Size) && "bad subregister range");
Andrew Trick10d5be42013-11-17 01:36:23 +0000404
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000405 if (!MF.getDataLayout().isLittleEndian()) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000406 Offset = TRI->getSpillSize(*RC) - (Offset + Size);
Andrew Trick10d5be42013-11-17 01:36:23 +0000407 }
408 return true;
409}
410
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000411void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB,
412 MachineBasicBlock::iterator I,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000413 unsigned DestReg, unsigned SubIdx,
414 const MachineInstr &Orig,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000415 const TargetRegisterInfo &TRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000416 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000417 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
418 MBB.insert(I, MI);
419}
420
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000421bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0,
422 const MachineInstr &MI1,
423 const MachineRegisterInfo *MRI) const {
424 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000425}
426
Matthias Braun55bc9b32017-08-22 23:56:30 +0000427MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
428 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000429 assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated");
Matthias Braun55bc9b32017-08-22 23:56:30 +0000430 MachineFunction &MF = *MBB.getParent();
431 return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000432}
433
434// If the COPY instruction in MI can be folded to a stack operation, return
435// the register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000436static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000437 unsigned FoldIdx) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000438 assert(MI.isCopy() && "MI must be a COPY instruction");
439 if (MI.getNumOperands() != 2)
Craig Topperc0196b12014-04-14 00:51:57 +0000440 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000441 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
442
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000443 const MachineOperand &FoldOp = MI.getOperand(FoldIdx);
444 const MachineOperand &LiveOp = MI.getOperand(1 - FoldIdx);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000445
446 if (FoldOp.getSubReg() || LiveOp.getSubReg())
Craig Topperc0196b12014-04-14 00:51:57 +0000447 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000448
449 unsigned FoldReg = FoldOp.getReg();
450 unsigned LiveReg = LiveOp.getReg();
451
452 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
453 "Cannot fold physregs");
454
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000455 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000456 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
457
458 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
Craig Topperc0196b12014-04-14 00:51:57 +0000459 return RC->contains(LiveOp.getReg()) ? RC : nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000460
461 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
462 return RC;
463
464 // FIXME: Allow folding when register classes are memory compatible.
Craig Topperc0196b12014-04-14 00:51:57 +0000465 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000466}
467
Hans Wennborg9b9a5352017-04-21 21:48:41 +0000468void TargetInstrInfo::getNoop(MCInst &NopInst) const {
469 llvm_unreachable("Not implemented");
Rafael Espindola6865d6f2014-09-15 18:32:58 +0000470}
471
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000472static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000473 ArrayRef<unsigned> Ops, int FrameIndex,
Lang Hames39609992013-11-29 03:07:54 +0000474 const TargetInstrInfo &TII) {
475 unsigned StartIdx = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000476 switch (MI.getOpcode()) {
Philip Reames570dd002016-08-23 21:21:43 +0000477 case TargetOpcode::STACKMAP: {
478 // StackMapLiveValues are foldable
Sanjoy Das6d3c9132016-08-30 01:38:59 +0000479 StartIdx = StackMapOpers(&MI).getVarIdx();
Lang Hames39609992013-11-29 03:07:54 +0000480 break;
Philip Reames570dd002016-08-23 21:21:43 +0000481 }
Lang Hames39609992013-11-29 03:07:54 +0000482 case TargetOpcode::PATCHPOINT: {
Philip Reames570dd002016-08-23 21:21:43 +0000483 // For PatchPoint, the call args are not foldable (even if reported in the
484 // stackmap e.g. via anyregcc).
Sanjoy Das6d3c9132016-08-30 01:38:59 +0000485 StartIdx = PatchPointOpers(&MI).getVarIdx();
Lang Hames39609992013-11-29 03:07:54 +0000486 break;
487 }
Philip Reames2b1084a2016-08-31 15:12:17 +0000488 case TargetOpcode::STATEPOINT: {
489 // For statepoints, fold deopt and gc arguments, but not call arguments.
490 StartIdx = StatepointOpers(&MI).getVarIdx();
491 break;
492 }
Lang Hames39609992013-11-29 03:07:54 +0000493 default:
494 llvm_unreachable("unexpected stackmap opcode");
495 }
496
497 // Return false if any operands requested for folding are not foldable (not
498 // part of the stackmap's live values).
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000499 for (unsigned Op : Ops) {
500 if (Op < StartIdx)
Craig Topperc0196b12014-04-14 00:51:57 +0000501 return nullptr;
Lang Hames39609992013-11-29 03:07:54 +0000502 }
503
504 MachineInstr *NewMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000505 MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
Lang Hames39609992013-11-29 03:07:54 +0000506 MachineInstrBuilder MIB(MF, NewMI);
507
508 // No need to fold return, the meta data, and function arguments
509 for (unsigned i = 0; i < StartIdx; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000510 MIB.add(MI.getOperand(i));
Lang Hames39609992013-11-29 03:07:54 +0000511
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000512 for (unsigned i = StartIdx; i < MI.getNumOperands(); ++i) {
513 MachineOperand &MO = MI.getOperand(i);
David Majnemer0d955d02016-08-11 22:21:41 +0000514 if (is_contained(Ops, i)) {
Lang Hames39609992013-11-29 03:07:54 +0000515 unsigned SpillSize;
516 unsigned SpillOffset;
517 // Compute the spill slot size and offset.
518 const TargetRegisterClass *RC =
519 MF.getRegInfo().getRegClass(MO.getReg());
Eric Christopher7585fb22015-03-19 23:06:21 +0000520 bool Valid =
521 TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize, SpillOffset, MF);
Lang Hames39609992013-11-29 03:07:54 +0000522 if (!Valid)
523 report_fatal_error("cannot spill patchpoint subregister operand");
524 MIB.addImm(StackMaps::IndirectMemRefOp);
525 MIB.addImm(SpillSize);
526 MIB.addFrameIndex(FrameIndex);
Lang Hames2ce64a72013-12-07 03:30:59 +0000527 MIB.addImm(SpillOffset);
Lang Hames39609992013-11-29 03:07:54 +0000528 }
529 else
Diana Picus116bbab2017-01-13 09:58:52 +0000530 MIB.add(MO);
Lang Hames39609992013-11-29 03:07:54 +0000531 }
532 return NewMI;
533}
534
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000535MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
536 ArrayRef<unsigned> Ops, int FI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000537 LiveIntervals *LIS) const {
Justin Lebar0af80cd2016-07-15 18:26:59 +0000538 auto Flags = MachineMemOperand::MONone;
Sanjay Patel232669d2017-10-02 15:02:06 +0000539 for (unsigned OpIdx : Ops)
540 Flags |= MI.getOperand(OpIdx).isDef() ? MachineMemOperand::MOStore
541 : MachineMemOperand::MOLoad;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000542
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000543 MachineBasicBlock *MBB = MI.getParent();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000544 assert(MBB && "foldMemoryOperand needs an inserted instruction");
545 MachineFunction &MF = *MBB->getParent();
546
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000547 // If we're not folding a load into a subreg, the size of the load is the
548 // size of the spill slot. But if we are, we need to figure out what the
549 // actual load size is.
550 int64_t MemSize = 0;
551 const MachineFrameInfo &MFI = MF.getFrameInfo();
552 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
553
554 if (Flags & MachineMemOperand::MOStore) {
555 MemSize = MFI.getObjectSize(FI);
556 } else {
Sanjay Patel232669d2017-10-02 15:02:06 +0000557 for (unsigned OpIdx : Ops) {
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000558 int64_t OpSize = MFI.getObjectSize(FI);
559
Sanjay Patel232669d2017-10-02 15:02:06 +0000560 if (auto SubReg = MI.getOperand(OpIdx).getSubReg()) {
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000561 unsigned SubRegSize = TRI->getSubRegIdxSize(SubReg);
562 if (SubRegSize > 0 && !(SubRegSize % 8))
563 OpSize = SubRegSize / 8;
564 }
565
566 MemSize = std::max(MemSize, OpSize);
567 }
568 }
569
570 assert(MemSize && "Did not expect a zero-sized stack slot");
571
Craig Topperc0196b12014-04-14 00:51:57 +0000572 MachineInstr *NewMI = nullptr;
Lang Hames39609992013-11-29 03:07:54 +0000573
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000574 if (MI.getOpcode() == TargetOpcode::STACKMAP ||
Philip Reames2b1084a2016-08-31 15:12:17 +0000575 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
576 MI.getOpcode() == TargetOpcode::STATEPOINT) {
Lang Hames39609992013-11-29 03:07:54 +0000577 // Fold stackmap/patchpoint.
578 NewMI = foldPatchpoint(MF, MI, Ops, FI, *this);
Keno Fischere70b31f2015-06-08 20:09:58 +0000579 if (NewMI)
580 MBB->insert(MI, NewMI);
Lang Hames39609992013-11-29 03:07:54 +0000581 } else {
582 // Ask the target to do the actual folding.
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000583 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS);
Lang Hames39609992013-11-29 03:07:54 +0000584 }
Keno Fischere70b31f2015-06-08 20:09:58 +0000585
Lang Hames39609992013-11-29 03:07:54 +0000586 if (NewMI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000587 NewMI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000588 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
589 assert((!(Flags & MachineMemOperand::MOStore) ||
590 NewMI->mayStore()) &&
591 "Folded a def to a non-store!");
592 assert((!(Flags & MachineMemOperand::MOLoad) ||
593 NewMI->mayLoad()) &&
594 "Folded a use to a non-load!");
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000595 assert(MFI.getObjectOffset(FI) != -1);
Alex Lorenze40c8a22015-08-11 23:09:45 +0000596 MachineMemOperand *MMO = MF.getMachineMemOperand(
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000597 MachinePointerInfo::getFixedStack(MF, FI), Flags, MemSize,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000598 MFI.getObjectAlignment(FI));
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000599 NewMI->addMemOperand(MF, MMO);
600
Keno Fischere70b31f2015-06-08 20:09:58 +0000601 return NewMI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000602 }
603
604 // Straight COPY may fold as load/store.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000605 if (!MI.isCopy() || Ops.size() != 1)
Craig Topperc0196b12014-04-14 00:51:57 +0000606 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000607
608 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
609 if (!RC)
Craig Topperc0196b12014-04-14 00:51:57 +0000610 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000611
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000612 const MachineOperand &MO = MI.getOperand(1 - Ops[0]);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000613 MachineBasicBlock::iterator Pos = MI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000614
615 if (Flags == MachineMemOperand::MOStore)
616 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
617 else
618 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
Duncan P. N. Exon Smithaae6f3c2016-07-01 16:38:28 +0000619 return &*--Pos;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000620}
621
Sanjay Patel7998d372017-10-02 14:03:17 +0000622MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
623 ArrayRef<unsigned> Ops,
624 MachineInstr &LoadMI,
625 LiveIntervals *LIS) const {
626 assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!");
627#ifndef NDEBUG
Sanjay Patel232669d2017-10-02 15:02:06 +0000628 for (unsigned OpIdx : Ops)
629 assert(MI.getOperand(OpIdx).isUse() && "Folding load into def!");
Sanjay Patel7998d372017-10-02 14:03:17 +0000630#endif
Sanjay Patel232669d2017-10-02 15:02:06 +0000631
Sanjay Patel7998d372017-10-02 14:03:17 +0000632 MachineBasicBlock &MBB = *MI.getParent();
633 MachineFunction &MF = *MBB.getParent();
634
635 // Ask the target to do the actual folding.
636 MachineInstr *NewMI = nullptr;
637 int FrameIndex = 0;
638
639 if ((MI.getOpcode() == TargetOpcode::STACKMAP ||
640 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
641 MI.getOpcode() == TargetOpcode::STATEPOINT) &&
642 isLoadFromStackSlot(LoadMI, FrameIndex)) {
643 // Fold stackmap/patchpoint.
644 NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this);
645 if (NewMI)
646 NewMI = &*MBB.insert(MI, NewMI);
647 } else {
648 // Ask the target to do the actual folding.
649 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS);
650 }
651
652 if (!NewMI)
653 return nullptr;
654
655 // Copy the memoperands from the load to the folded instruction.
656 if (MI.memoperands_empty()) {
657 NewMI->setMemRefs(LoadMI.memoperands_begin(), LoadMI.memoperands_end());
658 } else {
659 // Handle the rare case of folding multiple loads.
660 NewMI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
661 for (MachineInstr::mmo_iterator I = LoadMI.memoperands_begin(),
662 E = LoadMI.memoperands_end();
663 I != E; ++I) {
664 NewMI->addMemOperand(MF, *I);
665 }
666 }
667 return NewMI;
668}
669
Chad Rosier03a47302015-09-21 15:09:11 +0000670bool TargetInstrInfo::hasReassociableOperands(
671 const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
672 const MachineOperand &Op1 = Inst.getOperand(1);
673 const MachineOperand &Op2 = Inst.getOperand(2);
674 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
675
676 // We need virtual register definitions for the operands that we will
677 // reassociate.
678 MachineInstr *MI1 = nullptr;
679 MachineInstr *MI2 = nullptr;
680 if (Op1.isReg() && TargetRegisterInfo::isVirtualRegister(Op1.getReg()))
681 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
682 if (Op2.isReg() && TargetRegisterInfo::isVirtualRegister(Op2.getReg()))
683 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
684
685 // And they need to be in the trace (otherwise, they won't have a depth).
Rafael Espindola84921b92015-10-24 23:11:13 +0000686 return MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB;
Chad Rosier03a47302015-09-21 15:09:11 +0000687}
688
689bool TargetInstrInfo::hasReassociableSibling(const MachineInstr &Inst,
690 bool &Commuted) const {
691 const MachineBasicBlock *MBB = Inst.getParent();
692 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
693 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg());
694 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
695 unsigned AssocOpcode = Inst.getOpcode();
696
697 // If only one operand has the same opcode and it's the second source operand,
698 // the operands must be commuted.
699 Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode;
700 if (Commuted)
701 std::swap(MI1, MI2);
702
703 // 1. The previous instruction must be the same type as Inst.
704 // 2. The previous instruction must have virtual register definitions for its
705 // operands in the same basic block as Inst.
706 // 3. The previous instruction's result must only be used by Inst.
Rafael Espindola84921b92015-10-24 23:11:13 +0000707 return MI1->getOpcode() == AssocOpcode &&
708 hasReassociableOperands(*MI1, MBB) &&
709 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
Chad Rosier03a47302015-09-21 15:09:11 +0000710}
711
712// 1. The operation must be associative and commutative.
713// 2. The instruction must have virtual register definitions for its
714// operands in the same basic block.
715// 3. The instruction must have a reassociable sibling.
716bool TargetInstrInfo::isReassociationCandidate(const MachineInstr &Inst,
717 bool &Commuted) const {
Rafael Espindola84921b92015-10-24 23:11:13 +0000718 return isAssociativeAndCommutative(Inst) &&
719 hasReassociableOperands(Inst, Inst.getParent()) &&
720 hasReassociableSibling(Inst, Commuted);
Chad Rosier03a47302015-09-21 15:09:11 +0000721}
722
723// The concept of the reassociation pass is that these operations can benefit
724// from this kind of transformation:
725//
726// A = ? op ?
727// B = A op X (Prev)
728// C = B op Y (Root)
729// -->
730// A = ? op ?
731// B = X op Y
732// C = A op B
733//
734// breaking the dependency between A and B, allowing them to be executed in
735// parallel (or back-to-back in a pipeline) instead of depending on each other.
736
737// FIXME: This has the potential to be expensive (compile time) while not
738// improving the code at all. Some ways to limit the overhead:
739// 1. Track successful transforms; bail out if hit rate gets too low.
740// 2. Only enable at -O3 or some other non-default optimization level.
741// 3. Pre-screen pattern candidates here: if an operand of the previous
742// instruction is known to not increase the critical path, then don't match
743// that pattern.
744bool TargetInstrInfo::getMachineCombinerPatterns(
745 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000746 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Chad Rosier03a47302015-09-21 15:09:11 +0000747 bool Commute;
748 if (isReassociationCandidate(Root, Commute)) {
749 // We found a sequence of instructions that may be suitable for a
750 // reassociation of operands to increase ILP. Specify each commutation
751 // possibility for the Prev instruction in the sequence and let the
752 // machine combiner decide if changing the operands is worthwhile.
753 if (Commute) {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000754 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_YB);
755 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_YB);
Chad Rosier03a47302015-09-21 15:09:11 +0000756 } else {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000757 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_BY);
758 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_BY);
Chad Rosier03a47302015-09-21 15:09:11 +0000759 }
760 return true;
761 }
762
763 return false;
764}
Sanjay Patel7998d372017-10-02 14:03:17 +0000765
Gerolf Hoflehner01b3a6182016-04-24 05:14:01 +0000766/// Return true when a code sequence can improve loop throughput.
767bool
768TargetInstrInfo::isThroughputPattern(MachineCombinerPattern Pattern) const {
769 return false;
770}
Sanjay Patel7998d372017-10-02 14:03:17 +0000771
Chad Rosier03a47302015-09-21 15:09:11 +0000772/// Attempt the reassociation transformation to reduce critical path length.
773/// See the above comments before getMachineCombinerPatterns().
774void TargetInstrInfo::reassociateOps(
775 MachineInstr &Root, MachineInstr &Prev,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000776 MachineCombinerPattern Pattern,
Chad Rosier03a47302015-09-21 15:09:11 +0000777 SmallVectorImpl<MachineInstr *> &InsInstrs,
778 SmallVectorImpl<MachineInstr *> &DelInstrs,
779 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000780 MachineFunction *MF = Root.getMF();
Chad Rosier03a47302015-09-21 15:09:11 +0000781 MachineRegisterInfo &MRI = MF->getRegInfo();
782 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
783 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
784 const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI);
785
786 // This array encodes the operand index for each parameter because the
787 // operands may be commuted. Each row corresponds to a pattern value,
788 // and each column specifies the index of A, B, X, Y.
789 unsigned OpIdx[4][4] = {
790 { 1, 1, 2, 2 },
791 { 1, 2, 2, 1 },
792 { 2, 1, 1, 2 },
793 { 2, 2, 1, 1 }
794 };
795
Sanjay Patel387e66e2015-11-05 19:34:57 +0000796 int Row;
797 switch (Pattern) {
798 case MachineCombinerPattern::REASSOC_AX_BY: Row = 0; break;
799 case MachineCombinerPattern::REASSOC_AX_YB: Row = 1; break;
800 case MachineCombinerPattern::REASSOC_XA_BY: Row = 2; break;
801 case MachineCombinerPattern::REASSOC_XA_YB: Row = 3; break;
802 default: llvm_unreachable("unexpected MachineCombinerPattern");
803 }
804
805 MachineOperand &OpA = Prev.getOperand(OpIdx[Row][0]);
806 MachineOperand &OpB = Root.getOperand(OpIdx[Row][1]);
807 MachineOperand &OpX = Prev.getOperand(OpIdx[Row][2]);
808 MachineOperand &OpY = Root.getOperand(OpIdx[Row][3]);
Chad Rosier03a47302015-09-21 15:09:11 +0000809 MachineOperand &OpC = Root.getOperand(0);
810
811 unsigned RegA = OpA.getReg();
812 unsigned RegB = OpB.getReg();
813 unsigned RegX = OpX.getReg();
814 unsigned RegY = OpY.getReg();
815 unsigned RegC = OpC.getReg();
816
817 if (TargetRegisterInfo::isVirtualRegister(RegA))
818 MRI.constrainRegClass(RegA, RC);
819 if (TargetRegisterInfo::isVirtualRegister(RegB))
820 MRI.constrainRegClass(RegB, RC);
821 if (TargetRegisterInfo::isVirtualRegister(RegX))
822 MRI.constrainRegClass(RegX, RC);
823 if (TargetRegisterInfo::isVirtualRegister(RegY))
824 MRI.constrainRegClass(RegY, RC);
825 if (TargetRegisterInfo::isVirtualRegister(RegC))
826 MRI.constrainRegClass(RegC, RC);
827
828 // Create a new virtual register for the result of (X op Y) instead of
829 // recycling RegB because the MachineCombiner's computation of the critical
830 // path requires a new register definition rather than an existing one.
831 unsigned NewVR = MRI.createVirtualRegister(RC);
832 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
833
834 unsigned Opcode = Root.getOpcode();
835 bool KillA = OpA.isKill();
836 bool KillX = OpX.isKill();
837 bool KillY = OpY.isKill();
838
839 // Create new instructions for insertion.
840 MachineInstrBuilder MIB1 =
841 BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR)
842 .addReg(RegX, getKillRegState(KillX))
843 .addReg(RegY, getKillRegState(KillY));
844 MachineInstrBuilder MIB2 =
845 BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC)
846 .addReg(RegA, getKillRegState(KillA))
847 .addReg(NewVR, getKillRegState(true));
848
849 setSpecialOperandAttr(Root, Prev, *MIB1, *MIB2);
850
851 // Record new instructions for insertion and old instructions for deletion.
852 InsInstrs.push_back(MIB1);
853 InsInstrs.push_back(MIB2);
854 DelInstrs.push_back(&Prev);
855 DelInstrs.push_back(&Root);
856}
857
858void TargetInstrInfo::genAlternativeCodeSequence(
Sanjay Patel387e66e2015-11-05 19:34:57 +0000859 MachineInstr &Root, MachineCombinerPattern Pattern,
Chad Rosier03a47302015-09-21 15:09:11 +0000860 SmallVectorImpl<MachineInstr *> &InsInstrs,
861 SmallVectorImpl<MachineInstr *> &DelInstrs,
862 DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000863 MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
Chad Rosier03a47302015-09-21 15:09:11 +0000864
865 // Select the previous instruction in the sequence based on the input pattern.
866 MachineInstr *Prev = nullptr;
867 switch (Pattern) {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000868 case MachineCombinerPattern::REASSOC_AX_BY:
869 case MachineCombinerPattern::REASSOC_XA_BY:
Chad Rosier03a47302015-09-21 15:09:11 +0000870 Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
871 break;
Sanjay Patel387e66e2015-11-05 19:34:57 +0000872 case MachineCombinerPattern::REASSOC_AX_YB:
873 case MachineCombinerPattern::REASSOC_XA_YB:
Chad Rosier03a47302015-09-21 15:09:11 +0000874 Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
875 break;
876 default:
877 break;
878 }
879
880 assert(Prev && "Unknown pattern for machine combiner");
881
882 reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg);
Chad Rosier03a47302015-09-21 15:09:11 +0000883}
884
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000885bool TargetInstrInfo::isReallyTriviallyReMaterializableGeneric(
886 const MachineInstr &MI, AliasAnalysis *AA) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000887 const MachineFunction &MF = *MI.getMF();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000888 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000889
890 // Remat clients assume operand 0 is the defined register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000891 if (!MI.getNumOperands() || !MI.getOperand(0).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000892 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000893 unsigned DefReg = MI.getOperand(0).getReg();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000894
895 // A sub-register definition can only be rematerialized if the instruction
896 // doesn't read the other parts of the register. Otherwise it is really a
897 // read-modify-write operation on the full virtual register which cannot be
898 // moved safely.
899 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000900 MI.getOperand(0).getSubReg() && MI.readsVirtualRegister(DefReg))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000901 return false;
902
903 // A load from a fixed stack slot can be rematerialized. This may be
904 // redundant with subsequent checks, but it's target-independent,
905 // simple, and a common case.
906 int FrameIdx = 0;
Eric Christopher9d916792014-07-23 22:12:03 +0000907 if (isLoadFromStackSlot(MI, FrameIdx) &&
Matthias Braun941a7052016-07-28 18:40:00 +0000908 MF.getFrameInfo().isImmutableObjectIndex(FrameIdx))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000909 return true;
910
911 // Avoid instructions obviously unsafe for remat.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000912 if (MI.isNotDuplicable() || MI.mayStore() || MI.hasUnmodeledSideEffects())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000913 return false;
914
915 // Don't remat inline asm. We have no idea how expensive it is
916 // even if it's side effect free.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000917 if (MI.isInlineAsm())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000918 return false;
919
920 // Avoid instructions which load from potentially varying memory.
Justin Lebard98cf002016-09-10 01:03:20 +0000921 if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000922 return false;
923
924 // If any of the registers accessed are non-constant, conservatively assume
925 // the instruction is not rematerializable.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000926 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
927 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000928 if (!MO.isReg()) continue;
929 unsigned Reg = MO.getReg();
930 if (Reg == 0)
931 continue;
932
933 // Check for a well-behaved physical register.
934 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
935 if (MO.isUse()) {
936 // If the physreg has no defs anywhere, it's just an ambient register
937 // and we can freely move its uses. Alternatively, if it's allocatable,
938 // it could get allocated to something with a def during allocation.
Matthias Braunde8c1b32016-10-28 18:05:09 +0000939 if (!MRI.isConstantPhysReg(Reg))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000940 return false;
941 } else {
942 // A physreg def. We can't remat it.
943 return false;
944 }
945 continue;
946 }
947
948 // Only allow one virtual-register def. There may be multiple defs of the
949 // same virtual register, though.
950 if (MO.isDef() && Reg != DefReg)
951 return false;
952
953 // Don't allow any virtual-register uses. Rematting an instruction with
954 // virtual register uses would length the live ranges of the uses, which
955 // is not necessarily a good idea, certainly not "trivial".
956 if (MO.isUse())
957 return false;
958 }
959
960 // Everything checked out.
961 return true;
962}
963
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000964int TargetInstrInfo::getSPAdjust(const MachineInstr &MI) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000965 const MachineFunction *MF = MI.getMF();
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000966 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
967 bool StackGrowsDown =
968 TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
969
Matthias Braunfa3872e2015-05-18 20:27:55 +0000970 unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
971 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000972
Serge Pavlov49acf9c2017-04-13 14:10:52 +0000973 if (!isFrameInstr(MI))
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000974 return 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000975
Serge Pavlov49acf9c2017-04-13 14:10:52 +0000976 int SPAdj = TFI->alignSPAdjust(getFrameSize(MI));
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000977
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000978 if ((!StackGrowsDown && MI.getOpcode() == FrameSetupOpcode) ||
979 (StackGrowsDown && MI.getOpcode() == FrameDestroyOpcode))
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000980 SPAdj = -SPAdj;
981
982 return SPAdj;
983}
984
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000985/// isSchedulingBoundary - Test if the given instruction should be
986/// considered a scheduling boundary. This primarily includes labels
987/// and terminators.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000988bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000989 const MachineBasicBlock *MBB,
990 const MachineFunction &MF) const {
991 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000992 if (MI.isTerminator() || MI.isPosition())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000993 return true;
994
995 // Don't attempt to schedule around any instruction that defines
996 // a stack-oriented pointer, as it's unlikely to be profitable. This
997 // saves compile time, because it doesn't require every single
998 // stack slot reference to depend on the instruction that does the
999 // modification.
Eric Christopherfc6de422014-08-05 02:39:49 +00001000 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
1001 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001002 return MI.modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001003}
1004
1005// Provide a global flag for disabling the PreRA hazard recognizer that targets
1006// may choose to honor.
1007bool TargetInstrInfo::usePreRAHazardRecognizer() const {
1008 return !DisableHazardRecognizer;
1009}
1010
1011// Default implementation of CreateTargetRAHazardRecognizer.
1012ScheduleHazardRecognizer *TargetInstrInfo::
Eric Christopherf047bfd2014-06-13 22:38:52 +00001013CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001014 const ScheduleDAG *DAG) const {
1015 // Dummy hazard recognizer allows all instructions to issue.
1016 return new ScheduleHazardRecognizer();
1017}
1018
1019// Default implementation of CreateTargetMIHazardRecognizer.
1020ScheduleHazardRecognizer *TargetInstrInfo::
1021CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
1022 const ScheduleDAG *DAG) const {
1023 return (ScheduleHazardRecognizer *)
1024 new ScoreboardHazardRecognizer(II, DAG, "misched");
1025}
1026
1027// Default implementation of CreateTargetPostRAHazardRecognizer.
1028ScheduleHazardRecognizer *TargetInstrInfo::
1029CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
1030 const ScheduleDAG *DAG) const {
1031 return (ScheduleHazardRecognizer *)
1032 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
1033}
1034
1035//===----------------------------------------------------------------------===//
1036// SelectionDAG latency interface.
1037//===----------------------------------------------------------------------===//
1038
1039int
1040TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1041 SDNode *DefNode, unsigned DefIdx,
1042 SDNode *UseNode, unsigned UseIdx) const {
1043 if (!ItinData || ItinData->isEmpty())
1044 return -1;
1045
1046 if (!DefNode->isMachineOpcode())
1047 return -1;
1048
1049 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
1050 if (!UseNode->isMachineOpcode())
1051 return ItinData->getOperandCycle(DefClass, DefIdx);
1052 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
1053 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1054}
1055
1056int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1057 SDNode *N) const {
1058 if (!ItinData || ItinData->isEmpty())
1059 return 1;
1060
1061 if (!N->isMachineOpcode())
1062 return 1;
1063
1064 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
1065}
1066
1067//===----------------------------------------------------------------------===//
1068// MachineInstr latency interface.
1069//===----------------------------------------------------------------------===//
1070
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001071unsigned TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1072 const MachineInstr &MI) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001073 if (!ItinData || ItinData->isEmpty())
1074 return 1;
1075
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001076 unsigned Class = MI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001077 int UOps = ItinData->Itineraries[Class].NumMicroOps;
1078 if (UOps >= 0)
1079 return UOps;
1080
1081 // The # of u-ops is dynamically determined. The specific target should
1082 // override this function to return the right number.
1083 return 1;
1084}
1085
1086/// Return the default expected latency for a def based on it's opcode.
Pete Cooper11759452014-09-02 17:43:54 +00001087unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel &SchedModel,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001088 const MachineInstr &DefMI) const {
1089 if (DefMI.isTransient())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001090 return 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001091 if (DefMI.mayLoad())
Pete Cooper11759452014-09-02 17:43:54 +00001092 return SchedModel.LoadLatency;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001093 if (isHighLatencyDef(DefMI.getOpcode()))
Pete Cooper11759452014-09-02 17:43:54 +00001094 return SchedModel.HighLatency;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001095 return 1;
1096}
1097
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001098unsigned TargetInstrInfo::getPredicationCost(const MachineInstr &) const {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00001099 return 0;
1100}
1101
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001102unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1103 const MachineInstr &MI,
1104 unsigned *PredCost) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001105 // Default to one cycle for no itinerary. However, an "empty" itinerary may
1106 // still have a MinLatency property, which getStageLatency checks.
1107 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001108 return MI.mayLoad() ? 2 : 1;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001109
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001110 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001111}
1112
Matthias Braun88e21312015-06-13 03:42:11 +00001113bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001114 const MachineInstr &DefMI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001115 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00001116 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001117 if (!ItinData || ItinData->isEmpty())
1118 return false;
1119
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001120 unsigned DefClass = DefMI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001121 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1122 return (DefCycle != -1 && DefCycle <= 1);
1123}
1124
1125/// Both DefMI and UseMI must be valid. By default, call directly to the
1126/// itinerary. This may be overriden by the target.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001127int TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1128 const MachineInstr &DefMI,
1129 unsigned DefIdx,
1130 const MachineInstr &UseMI,
1131 unsigned UseIdx) const {
1132 unsigned DefClass = DefMI.getDesc().getSchedClass();
1133 unsigned UseClass = UseMI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001134 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1135}
1136
1137/// If we can determine the operand latency from the def only, without itinerary
1138/// lookup, do so. Otherwise return -1.
1139int TargetInstrInfo::computeDefOperandLatency(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001140 const InstrItineraryData *ItinData, const MachineInstr &DefMI) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001141
1142 // Let the target hook getInstrLatency handle missing itineraries.
1143 if (!ItinData)
1144 return getInstrLatency(ItinData, DefMI);
1145
Andrew Trickde2109e2013-06-15 04:49:57 +00001146 if(ItinData->isEmpty())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001147 return defaultDefLatency(ItinData->SchedModel, DefMI);
1148
1149 // ...operand lookup required
1150 return -1;
1151}
1152
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001153bool TargetInstrInfo::getRegSequenceInputs(
1154 const MachineInstr &MI, unsigned DefIdx,
1155 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
Quentin Colombet8427df92014-08-12 17:11:26 +00001156 assert((MI.isRegSequence() ||
1157 MI.isRegSequenceLike()) && "Instruction do not have the proper type");
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001158
1159 if (!MI.isRegSequence())
1160 return getRegSequenceLikeInputs(MI, DefIdx, InputRegs);
1161
1162 // We are looking at:
1163 // Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
1164 assert(DefIdx == 0 && "REG_SEQUENCE only has one def");
1165 for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
1166 OpIdx += 2) {
1167 const MachineOperand &MOReg = MI.getOperand(OpIdx);
Matthias Braunea4359e2018-01-11 22:30:43 +00001168 if (MOReg.isUndef())
1169 continue;
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001170 const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1);
1171 assert(MOSubIdx.isImm() &&
1172 "One of the subindex of the reg_sequence is not an immediate");
1173 // Record Reg:SubReg, SubIdx.
1174 InputRegs.push_back(RegSubRegPairAndIdx(MOReg.getReg(), MOReg.getSubReg(),
1175 (unsigned)MOSubIdx.getImm()));
1176 }
1177 return true;
1178}
Quentin Colombet7e75cba2014-08-20 21:51:26 +00001179
1180bool TargetInstrInfo::getExtractSubregInputs(
1181 const MachineInstr &MI, unsigned DefIdx,
1182 RegSubRegPairAndIdx &InputReg) const {
1183 assert((MI.isExtractSubreg() ||
1184 MI.isExtractSubregLike()) && "Instruction do not have the proper type");
1185
1186 if (!MI.isExtractSubreg())
1187 return getExtractSubregLikeInputs(MI, DefIdx, InputReg);
1188
1189 // We are looking at:
1190 // Def = EXTRACT_SUBREG v0.sub1, sub0.
1191 assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def");
1192 const MachineOperand &MOReg = MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00001193 if (MOReg.isUndef())
1194 return false;
Quentin Colombet7e75cba2014-08-20 21:51:26 +00001195 const MachineOperand &MOSubIdx = MI.getOperand(2);
1196 assert(MOSubIdx.isImm() &&
1197 "The subindex of the extract_subreg is not an immediate");
1198
1199 InputReg.Reg = MOReg.getReg();
1200 InputReg.SubReg = MOReg.getSubReg();
1201 InputReg.SubIdx = (unsigned)MOSubIdx.getImm();
1202 return true;
1203}
Quentin Colombet7e3da662014-08-20 23:49:36 +00001204
1205bool TargetInstrInfo::getInsertSubregInputs(
1206 const MachineInstr &MI, unsigned DefIdx,
1207 RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const {
1208 assert((MI.isInsertSubreg() ||
1209 MI.isInsertSubregLike()) && "Instruction do not have the proper type");
1210
1211 if (!MI.isInsertSubreg())
1212 return getInsertSubregLikeInputs(MI, DefIdx, BaseReg, InsertedReg);
1213
1214 // We are looking at:
1215 // Def = INSERT_SEQUENCE v0, v1, sub0.
1216 assert(DefIdx == 0 && "INSERT_SUBREG only has one def");
1217 const MachineOperand &MOBaseReg = MI.getOperand(1);
1218 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00001219 if (MOInsertedReg.isUndef())
1220 return false;
Quentin Colombet7e3da662014-08-20 23:49:36 +00001221 const MachineOperand &MOSubIdx = MI.getOperand(3);
1222 assert(MOSubIdx.isImm() &&
1223 "One of the subindex of the reg_sequence is not an immediate");
1224 BaseReg.Reg = MOBaseReg.getReg();
1225 BaseReg.SubReg = MOBaseReg.getSubReg();
1226
1227 InsertedReg.Reg = MOInsertedReg.getReg();
1228 InsertedReg.SubReg = MOInsertedReg.getSubReg();
1229 InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm();
1230 return true;
1231}