blob: 2a17af391105cb8ac7fc7f1bd9f0f575218e16d7 [file] [log] [blame]
Chris Lattner08084142003-01-13 00:26:36 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner93fa7052002-10-28 23:55:33 +00009//
Chris Lattner167b10c2005-01-19 06:53:34 +000010// This file implements the TargetInstrInfo class.
Chris Lattner93fa7052002-10-28 23:55:33 +000011//
12//===----------------------------------------------------------------------===//
13
David Blaikie48319232017-11-08 01:01:31 +000014#include "llvm/CodeGen/TargetInstrInfo.h"
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Lang Hames1cbca512013-11-29 03:07:54 +000016#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenfa2d9862012-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 Hames1cbca512013-11-29 03:07:54 +000021#include "llvm/CodeGen/StackMaps.h"
David Blaikie48319232017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetLowering.h"
24#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun6fee0b02015-06-13 03:42:11 +000025#include "llvm/CodeGen/TargetSchedule.h"
Andrew Trickbb756ca2013-11-17 01:36:23 +000026#include "llvm/IR/DataLayout.h"
Evan Chenga0792de2010-10-06 06:27:31 +000027#include "llvm/MC/MCAsmInfo.h"
Evan Chengab8be962011-06-29 01:14:12 +000028#include "llvm/MC/MCInstrItineraries.h"
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +000029#include "llvm/Support/CommandLine.h"
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000030#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +000031#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +000032#include "llvm/Target/TargetMachine.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000033#include <cctype>
Eugene Zelenko380d47d2016-02-02 18:20:45 +000034
Chris Lattner167b10c2005-01-19 06:53:34 +000035using namespace llvm;
Chris Lattner93fa7052002-10-28 23:55:33 +000036
Jakob Stoklund Olesenfa2d9862012-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 Lattnerd90183d2009-08-02 05:20:37 +000040
Chris Lattner08084142003-01-13 00:26:36 +000041TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000042}
43
Evan Cheng15993f82011-06-27 21:26:13 +000044const TargetRegisterClass*
Evan Chenge837dea2011-06-28 19:10:37 +000045TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000046 const TargetRegisterInfo *TRI,
47 const MachineFunction &MF) const {
Evan Chenge837dea2011-06-28 19:10:37 +000048 if (OpNum >= MCID.getNumOperands())
Craig Topper4ba84432014-04-14 00:51:57 +000049 return nullptr;
Evan Cheng15993f82011-06-27 21:26:13 +000050
Evan Chenge837dea2011-06-28 19:10:37 +000051 short RegClass = MCID.OpInfo[OpNum].RegClass;
52 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000053 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng15993f82011-06-27 21:26:13 +000054
55 // Instructions like INSERT_SUBREG do not have fixed register classes.
56 if (RegClass < 0)
Craig Topper4ba84432014-04-14 00:51:57 +000057 return nullptr;
Evan Cheng15993f82011-06-27 21:26:13 +000058
59 // Otherwise just look it up normally.
60 return TRI->getRegClass(RegClass);
61}
62
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000063/// insertNoop - Insert a noop into the instruction stream at the specified
64/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +000065void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000066 MachineBasicBlock::iterator MI) const {
67 llvm_unreachable("Target didn't implement insertNoop!");
68}
69
Alex Bradburyf0e3ca12017-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 Lattnerd90183d2009-08-02 05:20:37 +000075/// Measure the specified inline asm to determine an approximation of its
76/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +000077/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +000078/// count as an instruction.
79/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +000080/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-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 Bradburyf0e3ca12017-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 Lattnerd90183d2009-08-02 05:20:37 +000089unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +000090 const MCAsmInfo &MAI) const {
Chris Lattnerd90183d2009-08-02 05:20:37 +000091 // Count the number of instructions in the asm.
Alex Bradburyf0e3ca12017-09-28 09:31:46 +000092 bool AtInsnStart = true;
93 unsigned Length = 0;
Chris Lattnerd90183d2009-08-02 05:20:37 +000094 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +000095 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
Matt Arsenault4a371392016-07-01 23:26:50 +000096 strlen(MAI.getSeparatorString())) == 0) {
Alex Bradburyf0e3ca12017-09-28 09:31:46 +000097 AtInsnStart = true;
98 } else if (isAsmComment(Str, MAI)) {
Matt Arsenault4a371392016-07-01 23:26:50 +000099 // Stop counting as an instruction after a comment until the next
100 // separator.
Alex Bradburyf0e3ca12017-09-28 09:31:46 +0000101 AtInsnStart = false;
Chris Lattnerd90183d2009-08-02 05:20:37 +0000102 }
Matt Arsenault4a371392016-07-01 23:26:50 +0000103
Alex Bradburyf0e3ca12017-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 Arsenault4a371392016-07-01 23:26:50 +0000119 }
Chris Lattnerd90183d2009-08-02 05:20:37 +0000120 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000121
Alex Bradburyf0e3ca12017-09-28 09:31:46 +0000122 return Length;
Chris Lattnerd90183d2009-08-02 05:20:37 +0000123}
Jakob Stoklund Olesenfa2d9862012-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 Bogner91af31a2016-03-25 18:38:48 +0000136 // Save off the debug loc before erasing the instruction.
137 DebugLoc DL = Tail->getDebugLoc();
138
Jakob Stoklund Olesenfa2d9862012-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 Arsenaultb1a710d2016-09-14 17:24:15 +0000144 insertBranch(*MBB, NewDest, nullptr, SmallVector<MachineOperand, 0>(), DL);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000145 MBB->addSuccessor(NewDest);
146}
147
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000148MachineInstr *TargetInstrInfo::commuteInstructionImpl(MachineInstr &MI,
149 bool NewMI, unsigned Idx1,
Andrew Kayloraac3c942015-09-28 20:33:22 +0000150 unsigned Idx2) const {
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000151 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000152 bool HasDef = MCID.getNumDefs();
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000153 if (HasDef && !MI.getOperand(0).isReg())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000154 // No idea how to commute this instruction. Target should implement its own.
Craig Topper4ba84432014-04-14 00:51:57 +0000155 return nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000156
Richard Trieu7df74562015-09-28 22:54:43 +0000157 unsigned CommutableOpIdx1 = Idx1; (void)CommutableOpIdx1;
158 unsigned CommutableOpIdx2 = Idx2; (void)CommutableOpIdx2;
Andrew Kayloraac3c942015-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 Smith567409d2016-06-30 00:01:54 +0000162 assert(MI.getOperand(Idx1).isReg() && MI.getOperand(Idx2).isReg() &&
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000163 "This only knows how to commute register operands so far");
Andrew Kayloraac3c942015-09-28 20:33:22 +0000164
Duncan P. N. Exon Smith567409d2016-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 Berryff75b992018-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 Olesenfa2d9862012-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 Smith567409d2016-06-30 00:01:54 +0000188 MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000189 Reg2IsKill = false;
190 Reg0 = Reg2;
191 SubReg0 = SubReg2;
192 } else if (HasDef && Reg0 == Reg2 &&
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000193 MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000194 Reg1IsKill = false;
195 Reg0 = Reg1;
196 SubReg0 = SubReg1;
197 }
198
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000199 MachineInstr *CommutedMI = nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000200 if (NewMI) {
201 // Create a new instruction.
Justin Bogner1842f4a2017-10-10 23:50:49 +0000202 MachineFunction &MF = *MI.getMF();
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000203 CommutedMI = MF.CloneMachineInstr(&MI);
204 } else {
205 CommutedMI = &MI;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000206 }
207
208 if (HasDef) {
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000209 CommutedMI->getOperand(0).setReg(Reg0);
210 CommutedMI->getOperand(0).setSubReg(SubReg0);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000211 }
Duncan P. N. Exon Smith567409d2016-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 Berryff75b992018-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 Smith567409d2016-06-30 00:01:54 +0000228 return CommutedMI;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000229}
230
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000231MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr &MI, bool NewMI,
Andrew Kayloraac3c942015-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 Smith567409d2016-06-30 00:01:54 +0000239 assert(MI.isCommutable() &&
Andrew Kayloraac3c942015-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 Smith567409d2016-06-30 00:01:54 +0000277bool TargetInstrInfo::findCommutedOpIndices(MachineInstr &MI,
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000278 unsigned &SrcOpIdx1,
279 unsigned &SrcOpIdx2) const {
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000280 assert(!MI.isBundle() &&
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000281 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
282
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000283 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000284 if (!MCID.isCommutable())
285 return false;
Andrew Kayloraac3c942015-09-28 20:33:22 +0000286
Jakob Stoklund Olesenfa2d9862012-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 Kayloraac3c942015-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 Smith567409d2016-06-30 00:01:54 +0000295 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000296 // No idea.
297 return false;
298 return true;
299}
300
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000301bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
302 if (!MI.isTerminator()) return false;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000303
304 // Conditional branch is a special case.
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000305 if (MI.isBranch() && !MI.isBarrier())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000306 return true;
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000307 if (!MI.isPredicable())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000308 return true;
309 return !isPredicated(MI);
310}
311
Ahmed Bougachafd83cb22015-06-11 19:30:37 +0000312bool TargetInstrInfo::PredicateInstruction(
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000313 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000314 bool MadeChange = false;
315
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000316 assert(!MI.isBundle() &&
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000317 "TargetInstrInfo::PredicateInstruction() can't handle bundles");
318
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000319 const MCInstrDesc &MCID = MI.getDesc();
320 if (!MI.isPredicable())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000321 return false;
322
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000323 for (unsigned j = 0, i = 0, e = MI.getNumOperands(); i != e; ++i) {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000324 if (MCID.OpInfo[i].isPredicate()) {
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +0000325 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenfa2d9862012-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
Sander de Smalen1d40abd2018-09-03 09:15:58 +0000342bool TargetInstrInfo::hasLoadFromStackSlot(
Sander de Smalen73369542018-09-05 08:59:50 +0000343 const MachineInstr &MI,
344 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Sander de Smalen1d40abd2018-09-03 09:15:58 +0000345 size_t StartSize = Accesses.size();
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000346 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
347 oe = MI.memoperands_end();
348 o != oe; ++o) {
Sander de Smalen73369542018-09-05 08:59:50 +0000349 if ((*o)->isLoad() &&
350 dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
351 Accesses.push_back(*o);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000352 }
Sander de Smalen1d40abd2018-09-03 09:15:58 +0000353 return Accesses.size() != StartSize;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000354}
355
Sander de Smalen1d40abd2018-09-03 09:15:58 +0000356bool TargetInstrInfo::hasStoreToStackSlot(
Sander de Smalen73369542018-09-05 08:59:50 +0000357 const MachineInstr &MI,
358 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Sander de Smalen1d40abd2018-09-03 09:15:58 +0000359 size_t StartSize = Accesses.size();
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000360 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
361 oe = MI.memoperands_end();
362 o != oe; ++o) {
Sander de Smalen73369542018-09-05 08:59:50 +0000363 if ((*o)->isStore() &&
364 dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
365 Accesses.push_back(*o);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000366 }
Sander de Smalen1d40abd2018-09-03 09:15:58 +0000367 return Accesses.size() != StartSize;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000368}
369
Andrew Trickbb756ca2013-11-17 01:36:23 +0000370bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC,
371 unsigned SubIdx, unsigned &Size,
372 unsigned &Offset,
Eric Christopher051c9e72015-03-19 23:06:21 +0000373 const MachineFunction &MF) const {
Krzysztof Parzyszek36d7c2b2017-04-24 18:55:33 +0000374 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Andrew Trickbb756ca2013-11-17 01:36:23 +0000375 if (!SubIdx) {
Krzysztof Parzyszek36d7c2b2017-04-24 18:55:33 +0000376 Size = TRI->getSpillSize(*RC);
Andrew Trickbb756ca2013-11-17 01:36:23 +0000377 Offset = 0;
378 return true;
379 }
Eric Christopher051c9e72015-03-19 23:06:21 +0000380 unsigned BitSize = TRI->getSubRegIdxSize(SubIdx);
Bjorn Petterssonead8b392018-08-09 15:19:07 +0000381 // Convert bit size to byte size.
Andrew Trickbb756ca2013-11-17 01:36:23 +0000382 if (BitSize % 8)
383 return false;
384
Eric Christopher051c9e72015-03-19 23:06:21 +0000385 int BitOffset = TRI->getSubRegIdxOffset(SubIdx);
Andrew Trickbb756ca2013-11-17 01:36:23 +0000386 if (BitOffset < 0 || BitOffset % 8)
387 return false;
388
389 Size = BitSize /= 8;
390 Offset = (unsigned)BitOffset / 8;
391
Krzysztof Parzyszek36d7c2b2017-04-24 18:55:33 +0000392 assert(TRI->getSpillSize(*RC) >= (Offset + Size) && "bad subregister range");
Andrew Trickbb756ca2013-11-17 01:36:23 +0000393
Mehdi Amini9c5961b2015-07-16 06:11:10 +0000394 if (!MF.getDataLayout().isLittleEndian()) {
Krzysztof Parzyszek36d7c2b2017-04-24 18:55:33 +0000395 Offset = TRI->getSpillSize(*RC) - (Offset + Size);
Andrew Trickbb756ca2013-11-17 01:36:23 +0000396 }
397 return true;
398}
399
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000400void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB,
401 MachineBasicBlock::iterator I,
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000402 unsigned DestReg, unsigned SubIdx,
403 const MachineInstr &Orig,
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000404 const TargetRegisterInfo &TRI) const {
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000405 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000406 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
407 MBB.insert(I, MI);
408}
409
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000410bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0,
411 const MachineInstr &MI1,
412 const MachineRegisterInfo *MRI) const {
413 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000414}
415
Matthias Braun18f24452017-08-22 23:56:30 +0000416MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
417 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const {
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000418 assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated");
Matthias Braun18f24452017-08-22 23:56:30 +0000419 MachineFunction &MF = *MBB.getParent();
420 return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000421}
422
423// If the COPY instruction in MI can be folded to a stack operation, return
424// the register class to use.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000425static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI,
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000426 unsigned FoldIdx) {
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000427 assert(MI.isCopy() && "MI must be a COPY instruction");
428 if (MI.getNumOperands() != 2)
Craig Topper4ba84432014-04-14 00:51:57 +0000429 return nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000430 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
431
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000432 const MachineOperand &FoldOp = MI.getOperand(FoldIdx);
433 const MachineOperand &LiveOp = MI.getOperand(1 - FoldIdx);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000434
435 if (FoldOp.getSubReg() || LiveOp.getSubReg())
Craig Topper4ba84432014-04-14 00:51:57 +0000436 return nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000437
438 unsigned FoldReg = FoldOp.getReg();
439 unsigned LiveReg = LiveOp.getReg();
440
441 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
442 "Cannot fold physregs");
443
Justin Bogner1842f4a2017-10-10 23:50:49 +0000444 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000445 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
446
447 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
Craig Topper4ba84432014-04-14 00:51:57 +0000448 return RC->contains(LiveOp.getReg()) ? RC : nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000449
450 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
451 return RC;
452
453 // FIXME: Allow folding when register classes are memory compatible.
Craig Topper4ba84432014-04-14 00:51:57 +0000454 return nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000455}
456
Hans Wennborg960a40e2017-04-21 21:48:41 +0000457void TargetInstrInfo::getNoop(MCInst &NopInst) const {
458 llvm_unreachable("Not implemented");
Rafael Espindola3f0ce4f2014-09-15 18:32:58 +0000459}
460
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000461static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI,
Benjamin Kramerb22e2f92015-02-28 12:04:00 +0000462 ArrayRef<unsigned> Ops, int FrameIndex,
Lang Hames1cbca512013-11-29 03:07:54 +0000463 const TargetInstrInfo &TII) {
464 unsigned StartIdx = 0;
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000465 switch (MI.getOpcode()) {
Philip Reames87aa10b2016-08-23 21:21:43 +0000466 case TargetOpcode::STACKMAP: {
467 // StackMapLiveValues are foldable
Sanjoy Das08f02f52016-08-30 01:38:59 +0000468 StartIdx = StackMapOpers(&MI).getVarIdx();
Lang Hames1cbca512013-11-29 03:07:54 +0000469 break;
Philip Reames87aa10b2016-08-23 21:21:43 +0000470 }
Lang Hames1cbca512013-11-29 03:07:54 +0000471 case TargetOpcode::PATCHPOINT: {
Philip Reames87aa10b2016-08-23 21:21:43 +0000472 // For PatchPoint, the call args are not foldable (even if reported in the
473 // stackmap e.g. via anyregcc).
Sanjoy Das08f02f52016-08-30 01:38:59 +0000474 StartIdx = PatchPointOpers(&MI).getVarIdx();
Lang Hames1cbca512013-11-29 03:07:54 +0000475 break;
476 }
Philip Reames327ae582016-08-31 15:12:17 +0000477 case TargetOpcode::STATEPOINT: {
478 // For statepoints, fold deopt and gc arguments, but not call arguments.
479 StartIdx = StatepointOpers(&MI).getVarIdx();
480 break;
481 }
Lang Hames1cbca512013-11-29 03:07:54 +0000482 default:
483 llvm_unreachable("unexpected stackmap opcode");
484 }
485
486 // Return false if any operands requested for folding are not foldable (not
487 // part of the stackmap's live values).
Benjamin Kramerb22e2f92015-02-28 12:04:00 +0000488 for (unsigned Op : Ops) {
489 if (Op < StartIdx)
Craig Topper4ba84432014-04-14 00:51:57 +0000490 return nullptr;
Lang Hames1cbca512013-11-29 03:07:54 +0000491 }
492
493 MachineInstr *NewMI =
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000494 MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
Lang Hames1cbca512013-11-29 03:07:54 +0000495 MachineInstrBuilder MIB(MF, NewMI);
496
497 // No need to fold return, the meta data, and function arguments
498 for (unsigned i = 0; i < StartIdx; ++i)
Diana Picus8a478102017-01-13 09:58:52 +0000499 MIB.add(MI.getOperand(i));
Lang Hames1cbca512013-11-29 03:07:54 +0000500
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000501 for (unsigned i = StartIdx; i < MI.getNumOperands(); ++i) {
502 MachineOperand &MO = MI.getOperand(i);
David Majnemer975248e2016-08-11 22:21:41 +0000503 if (is_contained(Ops, i)) {
Lang Hames1cbca512013-11-29 03:07:54 +0000504 unsigned SpillSize;
505 unsigned SpillOffset;
506 // Compute the spill slot size and offset.
507 const TargetRegisterClass *RC =
508 MF.getRegInfo().getRegClass(MO.getReg());
Eric Christopher051c9e72015-03-19 23:06:21 +0000509 bool Valid =
510 TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize, SpillOffset, MF);
Lang Hames1cbca512013-11-29 03:07:54 +0000511 if (!Valid)
512 report_fatal_error("cannot spill patchpoint subregister operand");
513 MIB.addImm(StackMaps::IndirectMemRefOp);
514 MIB.addImm(SpillSize);
515 MIB.addFrameIndex(FrameIndex);
Lang Hamesd4ef8132013-12-07 03:30:59 +0000516 MIB.addImm(SpillOffset);
Lang Hames1cbca512013-11-29 03:07:54 +0000517 }
518 else
Diana Picus8a478102017-01-13 09:58:52 +0000519 MIB.add(MO);
Lang Hames1cbca512013-11-29 03:07:54 +0000520 }
521 return NewMI;
522}
523
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000524MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
525 ArrayRef<unsigned> Ops, int FI,
Jonas Paulsson32db7c32016-05-10 08:09:37 +0000526 LiveIntervals *LIS) const {
Justin Lebar14fc45e2016-07-15 18:26:59 +0000527 auto Flags = MachineMemOperand::MONone;
Sanjay Patel84282c52017-10-02 15:02:06 +0000528 for (unsigned OpIdx : Ops)
529 Flags |= MI.getOperand(OpIdx).isDef() ? MachineMemOperand::MOStore
530 : MachineMemOperand::MOLoad;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000531
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000532 MachineBasicBlock *MBB = MI.getParent();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000533 assert(MBB && "foldMemoryOperand needs an inserted instruction");
534 MachineFunction &MF = *MBB->getParent();
535
Michael Kuperstein4f548472016-11-23 18:33:49 +0000536 // If we're not folding a load into a subreg, the size of the load is the
537 // size of the spill slot. But if we are, we need to figure out what the
538 // actual load size is.
539 int64_t MemSize = 0;
540 const MachineFrameInfo &MFI = MF.getFrameInfo();
541 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
542
543 if (Flags & MachineMemOperand::MOStore) {
544 MemSize = MFI.getObjectSize(FI);
545 } else {
Sanjay Patel84282c52017-10-02 15:02:06 +0000546 for (unsigned OpIdx : Ops) {
Michael Kuperstein4f548472016-11-23 18:33:49 +0000547 int64_t OpSize = MFI.getObjectSize(FI);
548
Sanjay Patel84282c52017-10-02 15:02:06 +0000549 if (auto SubReg = MI.getOperand(OpIdx).getSubReg()) {
Michael Kuperstein4f548472016-11-23 18:33:49 +0000550 unsigned SubRegSize = TRI->getSubRegIdxSize(SubReg);
551 if (SubRegSize > 0 && !(SubRegSize % 8))
552 OpSize = SubRegSize / 8;
553 }
554
555 MemSize = std::max(MemSize, OpSize);
556 }
557 }
558
559 assert(MemSize && "Did not expect a zero-sized stack slot");
560
Craig Topper4ba84432014-04-14 00:51:57 +0000561 MachineInstr *NewMI = nullptr;
Lang Hames1cbca512013-11-29 03:07:54 +0000562
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000563 if (MI.getOpcode() == TargetOpcode::STACKMAP ||
Philip Reames327ae582016-08-31 15:12:17 +0000564 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
565 MI.getOpcode() == TargetOpcode::STATEPOINT) {
Lang Hames1cbca512013-11-29 03:07:54 +0000566 // Fold stackmap/patchpoint.
567 NewMI = foldPatchpoint(MF, MI, Ops, FI, *this);
Keno Fischer4332f862015-06-08 20:09:58 +0000568 if (NewMI)
569 MBB->insert(MI, NewMI);
Lang Hames1cbca512013-11-29 03:07:54 +0000570 } else {
571 // Ask the target to do the actual folding.
Jonas Paulsson32db7c32016-05-10 08:09:37 +0000572 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS);
Lang Hames1cbca512013-11-29 03:07:54 +0000573 }
Keno Fischer4332f862015-06-08 20:09:58 +0000574
Lang Hames1cbca512013-11-29 03:07:54 +0000575 if (NewMI) {
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000576 NewMI->setMemRefs(MF, MI.memoperands());
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000577 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
578 assert((!(Flags & MachineMemOperand::MOStore) ||
579 NewMI->mayStore()) &&
580 "Folded a def to a non-store!");
581 assert((!(Flags & MachineMemOperand::MOLoad) ||
582 NewMI->mayLoad()) &&
583 "Folded a use to a non-load!");
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000584 assert(MFI.getObjectOffset(FI) != -1);
Alex Lorenzde0129a2015-08-11 23:09:45 +0000585 MachineMemOperand *MMO = MF.getMachineMemOperand(
Michael Kuperstein4f548472016-11-23 18:33:49 +0000586 MachinePointerInfo::getFixedStack(MF, FI), Flags, MemSize,
Alex Lorenzde0129a2015-08-11 23:09:45 +0000587 MFI.getObjectAlignment(FI));
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000588 NewMI->addMemOperand(MF, MMO);
589
Keno Fischer4332f862015-06-08 20:09:58 +0000590 return NewMI;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000591 }
592
593 // Straight COPY may fold as load/store.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000594 if (!MI.isCopy() || Ops.size() != 1)
Craig Topper4ba84432014-04-14 00:51:57 +0000595 return nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000596
597 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
598 if (!RC)
Craig Topper4ba84432014-04-14 00:51:57 +0000599 return nullptr;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000600
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000601 const MachineOperand &MO = MI.getOperand(1 - Ops[0]);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000602 MachineBasicBlock::iterator Pos = MI;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000603
604 if (Flags == MachineMemOperand::MOStore)
605 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
606 else
607 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
Duncan P. N. Exon Smith1032b932016-07-01 16:38:28 +0000608 return &*--Pos;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000609}
610
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000611MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
612 ArrayRef<unsigned> Ops,
613 MachineInstr &LoadMI,
614 LiveIntervals *LIS) const {
615 assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!");
616#ifndef NDEBUG
Sanjay Patel84282c52017-10-02 15:02:06 +0000617 for (unsigned OpIdx : Ops)
618 assert(MI.getOperand(OpIdx).isUse() && "Folding load into def!");
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000619#endif
Sanjay Patel84282c52017-10-02 15:02:06 +0000620
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000621 MachineBasicBlock &MBB = *MI.getParent();
622 MachineFunction &MF = *MBB.getParent();
623
624 // Ask the target to do the actual folding.
625 MachineInstr *NewMI = nullptr;
626 int FrameIndex = 0;
627
628 if ((MI.getOpcode() == TargetOpcode::STACKMAP ||
629 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
630 MI.getOpcode() == TargetOpcode::STATEPOINT) &&
631 isLoadFromStackSlot(LoadMI, FrameIndex)) {
632 // Fold stackmap/patchpoint.
633 NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this);
634 if (NewMI)
635 NewMI = &*MBB.insert(MI, NewMI);
636 } else {
637 // Ask the target to do the actual folding.
638 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS);
639 }
640
641 if (!NewMI)
642 return nullptr;
643
644 // Copy the memoperands from the load to the folded instruction.
645 if (MI.memoperands_empty()) {
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000646 NewMI->setMemRefs(MF, LoadMI.memoperands());
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000647 } else {
648 // Handle the rare case of folding multiple loads.
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000649 NewMI->setMemRefs(MF, MI.memoperands());
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000650 for (MachineInstr::mmo_iterator I = LoadMI.memoperands_begin(),
651 E = LoadMI.memoperands_end();
652 I != E; ++I) {
653 NewMI->addMemOperand(MF, *I);
654 }
655 }
656 return NewMI;
657}
658
Chad Rosierc5d45302015-09-21 15:09:11 +0000659bool TargetInstrInfo::hasReassociableOperands(
660 const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
661 const MachineOperand &Op1 = Inst.getOperand(1);
662 const MachineOperand &Op2 = Inst.getOperand(2);
663 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
664
665 // We need virtual register definitions for the operands that we will
666 // reassociate.
667 MachineInstr *MI1 = nullptr;
668 MachineInstr *MI2 = nullptr;
669 if (Op1.isReg() && TargetRegisterInfo::isVirtualRegister(Op1.getReg()))
670 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
671 if (Op2.isReg() && TargetRegisterInfo::isVirtualRegister(Op2.getReg()))
672 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
673
674 // And they need to be in the trace (otherwise, they won't have a depth).
Rafael Espindola2fe94b62015-10-24 23:11:13 +0000675 return MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB;
Chad Rosierc5d45302015-09-21 15:09:11 +0000676}
677
678bool TargetInstrInfo::hasReassociableSibling(const MachineInstr &Inst,
679 bool &Commuted) const {
680 const MachineBasicBlock *MBB = Inst.getParent();
681 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
682 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg());
683 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
684 unsigned AssocOpcode = Inst.getOpcode();
685
686 // If only one operand has the same opcode and it's the second source operand,
687 // the operands must be commuted.
688 Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode;
689 if (Commuted)
690 std::swap(MI1, MI2);
691
692 // 1. The previous instruction must be the same type as Inst.
693 // 2. The previous instruction must have virtual register definitions for its
694 // operands in the same basic block as Inst.
695 // 3. The previous instruction's result must only be used by Inst.
Rafael Espindola2fe94b62015-10-24 23:11:13 +0000696 return MI1->getOpcode() == AssocOpcode &&
697 hasReassociableOperands(*MI1, MBB) &&
698 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
Chad Rosierc5d45302015-09-21 15:09:11 +0000699}
700
701// 1. The operation must be associative and commutative.
702// 2. The instruction must have virtual register definitions for its
703// operands in the same basic block.
704// 3. The instruction must have a reassociable sibling.
705bool TargetInstrInfo::isReassociationCandidate(const MachineInstr &Inst,
706 bool &Commuted) const {
Rafael Espindola2fe94b62015-10-24 23:11:13 +0000707 return isAssociativeAndCommutative(Inst) &&
708 hasReassociableOperands(Inst, Inst.getParent()) &&
709 hasReassociableSibling(Inst, Commuted);
Chad Rosierc5d45302015-09-21 15:09:11 +0000710}
711
712// The concept of the reassociation pass is that these operations can benefit
713// from this kind of transformation:
714//
715// A = ? op ?
716// B = A op X (Prev)
717// C = B op Y (Root)
718// -->
719// A = ? op ?
720// B = X op Y
721// C = A op B
722//
723// breaking the dependency between A and B, allowing them to be executed in
724// parallel (or back-to-back in a pipeline) instead of depending on each other.
725
726// FIXME: This has the potential to be expensive (compile time) while not
727// improving the code at all. Some ways to limit the overhead:
728// 1. Track successful transforms; bail out if hit rate gets too low.
729// 2. Only enable at -O3 or some other non-default optimization level.
730// 3. Pre-screen pattern candidates here: if an operand of the previous
731// instruction is known to not increase the critical path, then don't match
732// that pattern.
733bool TargetInstrInfo::getMachineCombinerPatterns(
734 MachineInstr &Root,
Sanjay Patel834952e2015-11-05 19:34:57 +0000735 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Chad Rosierc5d45302015-09-21 15:09:11 +0000736 bool Commute;
737 if (isReassociationCandidate(Root, Commute)) {
738 // We found a sequence of instructions that may be suitable for a
739 // reassociation of operands to increase ILP. Specify each commutation
740 // possibility for the Prev instruction in the sequence and let the
741 // machine combiner decide if changing the operands is worthwhile.
742 if (Commute) {
Sanjay Patel834952e2015-11-05 19:34:57 +0000743 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_YB);
744 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_YB);
Chad Rosierc5d45302015-09-21 15:09:11 +0000745 } else {
Sanjay Patel834952e2015-11-05 19:34:57 +0000746 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_BY);
747 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_BY);
Chad Rosierc5d45302015-09-21 15:09:11 +0000748 }
749 return true;
750 }
751
752 return false;
753}
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000754
Gerolf Hoflehner3f71db12016-04-24 05:14:01 +0000755/// Return true when a code sequence can improve loop throughput.
756bool
757TargetInstrInfo::isThroughputPattern(MachineCombinerPattern Pattern) const {
758 return false;
759}
Sanjay Pateldcae9be2017-10-02 14:03:17 +0000760
Chad Rosierc5d45302015-09-21 15:09:11 +0000761/// Attempt the reassociation transformation to reduce critical path length.
762/// See the above comments before getMachineCombinerPatterns().
763void TargetInstrInfo::reassociateOps(
764 MachineInstr &Root, MachineInstr &Prev,
Sanjay Patel834952e2015-11-05 19:34:57 +0000765 MachineCombinerPattern Pattern,
Chad Rosierc5d45302015-09-21 15:09:11 +0000766 SmallVectorImpl<MachineInstr *> &InsInstrs,
767 SmallVectorImpl<MachineInstr *> &DelInstrs,
768 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
Justin Bogner1842f4a2017-10-10 23:50:49 +0000769 MachineFunction *MF = Root.getMF();
Chad Rosierc5d45302015-09-21 15:09:11 +0000770 MachineRegisterInfo &MRI = MF->getRegInfo();
771 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
772 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
773 const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI);
774
775 // This array encodes the operand index for each parameter because the
776 // operands may be commuted. Each row corresponds to a pattern value,
777 // and each column specifies the index of A, B, X, Y.
778 unsigned OpIdx[4][4] = {
779 { 1, 1, 2, 2 },
780 { 1, 2, 2, 1 },
781 { 2, 1, 1, 2 },
782 { 2, 2, 1, 1 }
783 };
784
Sanjay Patel834952e2015-11-05 19:34:57 +0000785 int Row;
786 switch (Pattern) {
787 case MachineCombinerPattern::REASSOC_AX_BY: Row = 0; break;
788 case MachineCombinerPattern::REASSOC_AX_YB: Row = 1; break;
789 case MachineCombinerPattern::REASSOC_XA_BY: Row = 2; break;
790 case MachineCombinerPattern::REASSOC_XA_YB: Row = 3; break;
791 default: llvm_unreachable("unexpected MachineCombinerPattern");
792 }
793
794 MachineOperand &OpA = Prev.getOperand(OpIdx[Row][0]);
795 MachineOperand &OpB = Root.getOperand(OpIdx[Row][1]);
796 MachineOperand &OpX = Prev.getOperand(OpIdx[Row][2]);
797 MachineOperand &OpY = Root.getOperand(OpIdx[Row][3]);
Chad Rosierc5d45302015-09-21 15:09:11 +0000798 MachineOperand &OpC = Root.getOperand(0);
799
800 unsigned RegA = OpA.getReg();
801 unsigned RegB = OpB.getReg();
802 unsigned RegX = OpX.getReg();
803 unsigned RegY = OpY.getReg();
804 unsigned RegC = OpC.getReg();
805
806 if (TargetRegisterInfo::isVirtualRegister(RegA))
807 MRI.constrainRegClass(RegA, RC);
808 if (TargetRegisterInfo::isVirtualRegister(RegB))
809 MRI.constrainRegClass(RegB, RC);
810 if (TargetRegisterInfo::isVirtualRegister(RegX))
811 MRI.constrainRegClass(RegX, RC);
812 if (TargetRegisterInfo::isVirtualRegister(RegY))
813 MRI.constrainRegClass(RegY, RC);
814 if (TargetRegisterInfo::isVirtualRegister(RegC))
815 MRI.constrainRegClass(RegC, RC);
816
817 // Create a new virtual register for the result of (X op Y) instead of
818 // recycling RegB because the MachineCombiner's computation of the critical
819 // path requires a new register definition rather than an existing one.
820 unsigned NewVR = MRI.createVirtualRegister(RC);
821 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
822
823 unsigned Opcode = Root.getOpcode();
824 bool KillA = OpA.isKill();
825 bool KillX = OpX.isKill();
826 bool KillY = OpY.isKill();
827
828 // Create new instructions for insertion.
829 MachineInstrBuilder MIB1 =
830 BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR)
831 .addReg(RegX, getKillRegState(KillX))
832 .addReg(RegY, getKillRegState(KillY));
833 MachineInstrBuilder MIB2 =
834 BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC)
835 .addReg(RegA, getKillRegState(KillA))
836 .addReg(NewVR, getKillRegState(true));
837
838 setSpecialOperandAttr(Root, Prev, *MIB1, *MIB2);
839
840 // Record new instructions for insertion and old instructions for deletion.
841 InsInstrs.push_back(MIB1);
842 InsInstrs.push_back(MIB2);
843 DelInstrs.push_back(&Prev);
844 DelInstrs.push_back(&Root);
845}
846
847void TargetInstrInfo::genAlternativeCodeSequence(
Sanjay Patel834952e2015-11-05 19:34:57 +0000848 MachineInstr &Root, MachineCombinerPattern Pattern,
Chad Rosierc5d45302015-09-21 15:09:11 +0000849 SmallVectorImpl<MachineInstr *> &InsInstrs,
850 SmallVectorImpl<MachineInstr *> &DelInstrs,
851 DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const {
Justin Bogner1842f4a2017-10-10 23:50:49 +0000852 MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
Chad Rosierc5d45302015-09-21 15:09:11 +0000853
854 // Select the previous instruction in the sequence based on the input pattern.
855 MachineInstr *Prev = nullptr;
856 switch (Pattern) {
Sanjay Patel834952e2015-11-05 19:34:57 +0000857 case MachineCombinerPattern::REASSOC_AX_BY:
858 case MachineCombinerPattern::REASSOC_XA_BY:
Chad Rosierc5d45302015-09-21 15:09:11 +0000859 Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
860 break;
Sanjay Patel834952e2015-11-05 19:34:57 +0000861 case MachineCombinerPattern::REASSOC_AX_YB:
862 case MachineCombinerPattern::REASSOC_XA_YB:
Chad Rosierc5d45302015-09-21 15:09:11 +0000863 Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
864 break;
865 default:
866 break;
867 }
868
869 assert(Prev && "Unknown pattern for machine combiner");
870
871 reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg);
Chad Rosierc5d45302015-09-21 15:09:11 +0000872}
873
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000874bool TargetInstrInfo::isReallyTriviallyReMaterializableGeneric(
875 const MachineInstr &MI, AliasAnalysis *AA) const {
Justin Bogner1842f4a2017-10-10 23:50:49 +0000876 const MachineFunction &MF = *MI.getMF();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000877 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000878
879 // Remat clients assume operand 0 is the defined register.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000880 if (!MI.getNumOperands() || !MI.getOperand(0).isReg())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000881 return false;
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000882 unsigned DefReg = MI.getOperand(0).getReg();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000883
884 // A sub-register definition can only be rematerialized if the instruction
885 // doesn't read the other parts of the register. Otherwise it is really a
886 // read-modify-write operation on the full virtual register which cannot be
887 // moved safely.
888 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000889 MI.getOperand(0).getSubReg() && MI.readsVirtualRegister(DefReg))
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000890 return false;
891
892 // A load from a fixed stack slot can be rematerialized. This may be
893 // redundant with subsequent checks, but it's target-independent,
894 // simple, and a common case.
895 int FrameIdx = 0;
Eric Christopher3322b7e2014-07-23 22:12:03 +0000896 if (isLoadFromStackSlot(MI, FrameIdx) &&
Matthias Braunf79c57a2016-07-28 18:40:00 +0000897 MF.getFrameInfo().isImmutableObjectIndex(FrameIdx))
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000898 return true;
899
900 // Avoid instructions obviously unsafe for remat.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000901 if (MI.isNotDuplicable() || MI.mayStore() || MI.hasUnmodeledSideEffects())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000902 return false;
903
904 // Don't remat inline asm. We have no idea how expensive it is
905 // even if it's side effect free.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000906 if (MI.isInlineAsm())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000907 return false;
908
909 // Avoid instructions which load from potentially varying memory.
Justin Lebare7555f02016-09-10 01:03:20 +0000910 if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA))
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000911 return false;
912
913 // If any of the registers accessed are non-constant, conservatively assume
914 // the instruction is not rematerializable.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000915 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
916 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000917 if (!MO.isReg()) continue;
918 unsigned Reg = MO.getReg();
919 if (Reg == 0)
920 continue;
921
922 // Check for a well-behaved physical register.
923 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
924 if (MO.isUse()) {
925 // If the physreg has no defs anywhere, it's just an ambient register
926 // and we can freely move its uses. Alternatively, if it's allocatable,
927 // it could get allocated to something with a def during allocation.
Matthias Braun15cdf2c2016-10-28 18:05:09 +0000928 if (!MRI.isConstantPhysReg(Reg))
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000929 return false;
930 } else {
931 // A physreg def. We can't remat it.
932 return false;
933 }
934 continue;
935 }
936
937 // Only allow one virtual-register def. There may be multiple defs of the
938 // same virtual register, though.
939 if (MO.isDef() && Reg != DefReg)
940 return false;
941
942 // Don't allow any virtual-register uses. Rematting an instruction with
943 // virtual register uses would length the live ranges of the uses, which
944 // is not necessarily a good idea, certainly not "trivial".
945 if (MO.isUse())
946 return false;
947 }
948
949 // Everything checked out.
950 return true;
951}
952
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000953int TargetInstrInfo::getSPAdjust(const MachineInstr &MI) const {
Justin Bogner1842f4a2017-10-10 23:50:49 +0000954 const MachineFunction *MF = MI.getMF();
Michael Kuperstein1cea7492015-01-08 11:04:38 +0000955 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
956 bool StackGrowsDown =
957 TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
958
Matthias Braune4603f02015-05-18 20:27:55 +0000959 unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
960 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
Michael Kuperstein1cea7492015-01-08 11:04:38 +0000961
Serge Pavlovcf2d2612017-04-13 14:10:52 +0000962 if (!isFrameInstr(MI))
Michael Kuperstein1cea7492015-01-08 11:04:38 +0000963 return 0;
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000964
Serge Pavlovcf2d2612017-04-13 14:10:52 +0000965 int SPAdj = TFI->alignSPAdjust(getFrameSize(MI));
Michael Kuperstein1cea7492015-01-08 11:04:38 +0000966
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000967 if ((!StackGrowsDown && MI.getOpcode() == FrameSetupOpcode) ||
968 (StackGrowsDown && MI.getOpcode() == FrameDestroyOpcode))
Michael Kuperstein1cea7492015-01-08 11:04:38 +0000969 SPAdj = -SPAdj;
970
971 return SPAdj;
972}
973
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000974/// isSchedulingBoundary - Test if the given instruction should be
975/// considered a scheduling boundary. This primarily includes labels
976/// and terminators.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000977bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000978 const MachineBasicBlock *MBB,
979 const MachineFunction &MF) const {
980 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000981 if (MI.isTerminator() || MI.isPosition())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000982 return true;
983
984 // Don't attempt to schedule around any instruction that defines
985 // a stack-oriented pointer, as it's unlikely to be profitable. This
986 // saves compile time, because it doesn't require every single
987 // stack slot reference to depend on the instruction that does the
988 // modification.
Eric Christopher60355182014-08-05 02:39:49 +0000989 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
990 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +0000991 return MI.modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI);
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +0000992}
993
994// Provide a global flag for disabling the PreRA hazard recognizer that targets
995// may choose to honor.
996bool TargetInstrInfo::usePreRAHazardRecognizer() const {
997 return !DisableHazardRecognizer;
998}
999
1000// Default implementation of CreateTargetRAHazardRecognizer.
1001ScheduleHazardRecognizer *TargetInstrInfo::
Eric Christopher7354a3f2014-06-13 22:38:52 +00001002CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001003 const ScheduleDAG *DAG) const {
1004 // Dummy hazard recognizer allows all instructions to issue.
1005 return new ScheduleHazardRecognizer();
1006}
1007
1008// Default implementation of CreateTargetMIHazardRecognizer.
1009ScheduleHazardRecognizer *TargetInstrInfo::
1010CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
1011 const ScheduleDAG *DAG) const {
1012 return (ScheduleHazardRecognizer *)
1013 new ScoreboardHazardRecognizer(II, DAG, "misched");
1014}
1015
1016// Default implementation of CreateTargetPostRAHazardRecognizer.
1017ScheduleHazardRecognizer *TargetInstrInfo::
1018CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
1019 const ScheduleDAG *DAG) const {
1020 return (ScheduleHazardRecognizer *)
1021 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
1022}
1023
1024//===----------------------------------------------------------------------===//
1025// SelectionDAG latency interface.
1026//===----------------------------------------------------------------------===//
1027
1028int
1029TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1030 SDNode *DefNode, unsigned DefIdx,
1031 SDNode *UseNode, unsigned UseIdx) const {
1032 if (!ItinData || ItinData->isEmpty())
1033 return -1;
1034
1035 if (!DefNode->isMachineOpcode())
1036 return -1;
1037
1038 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
1039 if (!UseNode->isMachineOpcode())
1040 return ItinData->getOperandCycle(DefClass, DefIdx);
1041 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
1042 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1043}
1044
1045int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1046 SDNode *N) const {
1047 if (!ItinData || ItinData->isEmpty())
1048 return 1;
1049
1050 if (!N->isMachineOpcode())
1051 return 1;
1052
1053 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
1054}
1055
1056//===----------------------------------------------------------------------===//
1057// MachineInstr latency interface.
1058//===----------------------------------------------------------------------===//
1059
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001060unsigned TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1061 const MachineInstr &MI) const {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001062 if (!ItinData || ItinData->isEmpty())
1063 return 1;
1064
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001065 unsigned Class = MI.getDesc().getSchedClass();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001066 int UOps = ItinData->Itineraries[Class].NumMicroOps;
1067 if (UOps >= 0)
1068 return UOps;
1069
1070 // The # of u-ops is dynamically determined. The specific target should
1071 // override this function to return the right number.
1072 return 1;
1073}
1074
1075/// Return the default expected latency for a def based on it's opcode.
Pete Cooper6de6c6a2014-09-02 17:43:54 +00001076unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel &SchedModel,
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001077 const MachineInstr &DefMI) const {
1078 if (DefMI.isTransient())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001079 return 0;
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001080 if (DefMI.mayLoad())
Pete Cooper6de6c6a2014-09-02 17:43:54 +00001081 return SchedModel.LoadLatency;
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001082 if (isHighLatencyDef(DefMI.getOpcode()))
Pete Cooper6de6c6a2014-09-02 17:43:54 +00001083 return SchedModel.HighLatency;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001084 return 1;
1085}
1086
Duncan P. N. Exon Smith5b9b80e2016-02-23 02:46:52 +00001087unsigned TargetInstrInfo::getPredicationCost(const MachineInstr &) const {
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +00001088 return 0;
1089}
1090
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001091unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1092 const MachineInstr &MI,
1093 unsigned *PredCost) const {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001094 // Default to one cycle for no itinerary. However, an "empty" itinerary may
1095 // still have a MinLatency property, which getStageLatency checks.
1096 if (!ItinData)
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001097 return MI.mayLoad() ? 2 : 1;
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001098
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001099 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001100}
1101
Matthias Braun6fee0b02015-06-13 03:42:11 +00001102bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001103 const MachineInstr &DefMI,
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001104 unsigned DefIdx) const {
Matthias Braun6fee0b02015-06-13 03:42:11 +00001105 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001106 if (!ItinData || ItinData->isEmpty())
1107 return false;
1108
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001109 unsigned DefClass = DefMI.getDesc().getSchedClass();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001110 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1111 return (DefCycle != -1 && DefCycle <= 1);
1112}
1113
1114/// Both DefMI and UseMI must be valid. By default, call directly to the
1115/// itinerary. This may be overriden by the target.
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001116int TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1117 const MachineInstr &DefMI,
1118 unsigned DefIdx,
1119 const MachineInstr &UseMI,
1120 unsigned UseIdx) const {
1121 unsigned DefClass = DefMI.getDesc().getSchedClass();
1122 unsigned UseClass = UseMI.getDesc().getSchedClass();
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001123 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1124}
1125
1126/// If we can determine the operand latency from the def only, without itinerary
1127/// lookup, do so. Otherwise return -1.
1128int TargetInstrInfo::computeDefOperandLatency(
Duncan P. N. Exon Smith567409d2016-06-30 00:01:54 +00001129 const InstrItineraryData *ItinData, const MachineInstr &DefMI) const {
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001130
1131 // Let the target hook getInstrLatency handle missing itineraries.
1132 if (!ItinData)
1133 return getInstrLatency(ItinData, DefMI);
1134
Andrew Trickb86a0cd2013-06-15 04:49:57 +00001135 if(ItinData->isEmpty())
Jakob Stoklund Olesenfa2d9862012-11-28 02:35:13 +00001136 return defaultDefLatency(ItinData->SchedModel, DefMI);
1137
1138 // ...operand lookup required
1139 return -1;
1140}
1141
Quentin Colombet1b425402014-08-11 22:17:14 +00001142bool TargetInstrInfo::getRegSequenceInputs(
1143 const MachineInstr &MI, unsigned DefIdx,
1144 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
Quentin Colombet693defb2014-08-12 17:11:26 +00001145 assert((MI.isRegSequence() ||
1146 MI.isRegSequenceLike()) && "Instruction do not have the proper type");
Quentin Colombet1b425402014-08-11 22:17:14 +00001147
1148 if (!MI.isRegSequence())
1149 return getRegSequenceLikeInputs(MI, DefIdx, InputRegs);
1150
1151 // We are looking at:
1152 // Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
1153 assert(DefIdx == 0 && "REG_SEQUENCE only has one def");
1154 for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
1155 OpIdx += 2) {
1156 const MachineOperand &MOReg = MI.getOperand(OpIdx);
Matthias Braund0a44762018-01-11 22:30:43 +00001157 if (MOReg.isUndef())
1158 continue;
Quentin Colombet1b425402014-08-11 22:17:14 +00001159 const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1);
1160 assert(MOSubIdx.isImm() &&
1161 "One of the subindex of the reg_sequence is not an immediate");
1162 // Record Reg:SubReg, SubIdx.
1163 InputRegs.push_back(RegSubRegPairAndIdx(MOReg.getReg(), MOReg.getSubReg(),
1164 (unsigned)MOSubIdx.getImm()));
1165 }
1166 return true;
1167}
Quentin Colombetdac67642014-08-20 21:51:26 +00001168
1169bool TargetInstrInfo::getExtractSubregInputs(
1170 const MachineInstr &MI, unsigned DefIdx,
1171 RegSubRegPairAndIdx &InputReg) const {
1172 assert((MI.isExtractSubreg() ||
1173 MI.isExtractSubregLike()) && "Instruction do not have the proper type");
1174
1175 if (!MI.isExtractSubreg())
1176 return getExtractSubregLikeInputs(MI, DefIdx, InputReg);
1177
1178 // We are looking at:
1179 // Def = EXTRACT_SUBREG v0.sub1, sub0.
1180 assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def");
1181 const MachineOperand &MOReg = MI.getOperand(1);
Matthias Braund0a44762018-01-11 22:30:43 +00001182 if (MOReg.isUndef())
1183 return false;
Quentin Colombetdac67642014-08-20 21:51:26 +00001184 const MachineOperand &MOSubIdx = MI.getOperand(2);
1185 assert(MOSubIdx.isImm() &&
1186 "The subindex of the extract_subreg is not an immediate");
1187
1188 InputReg.Reg = MOReg.getReg();
1189 InputReg.SubReg = MOReg.getSubReg();
1190 InputReg.SubIdx = (unsigned)MOSubIdx.getImm();
1191 return true;
1192}
Quentin Colombet0d152132014-08-20 23:49:36 +00001193
1194bool TargetInstrInfo::getInsertSubregInputs(
1195 const MachineInstr &MI, unsigned DefIdx,
1196 RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const {
1197 assert((MI.isInsertSubreg() ||
1198 MI.isInsertSubregLike()) && "Instruction do not have the proper type");
1199
1200 if (!MI.isInsertSubreg())
1201 return getInsertSubregLikeInputs(MI, DefIdx, BaseReg, InsertedReg);
1202
1203 // We are looking at:
1204 // Def = INSERT_SEQUENCE v0, v1, sub0.
1205 assert(DefIdx == 0 && "INSERT_SUBREG only has one def");
1206 const MachineOperand &MOBaseReg = MI.getOperand(1);
1207 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braund0a44762018-01-11 22:30:43 +00001208 if (MOInsertedReg.isUndef())
1209 return false;
Quentin Colombet0d152132014-08-20 23:49:36 +00001210 const MachineOperand &MOSubIdx = MI.getOperand(3);
1211 assert(MOSubIdx.isImm() &&
1212 "One of the subindex of the reg_sequence is not an immediate");
1213 BaseReg.Reg = MOBaseReg.getReg();
1214 BaseReg.SubReg = MOBaseReg.getSubReg();
1215
1216 InsertedReg.Reg = MOInsertedReg.getReg();
1217 InsertedReg.SubReg = MOInsertedReg.getSubReg();
1218 InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm();
1219 return true;
1220}