blob: b3aeaf8272598b44e62fa40a175f467a4caf6182 [file] [log] [blame]
Chris Lattner0d5644b2003-01-13 00:26:36 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman10468d82005-04-21 22:55:34 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner910b82f2002-10-28 23:55:33 +00008//
Chris Lattnerf6932b72005-01-19 06:53:34 +00009// This file implements the TargetInstrInfo class.
Chris Lattner910b82f2002-10-28 23:55:33 +000010//
11//===----------------------------------------------------------------------===//
12
David Blaikie3f833ed2017-11-08 01:01:31 +000013#include "llvm/CodeGen/TargetInstrInfo.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000014#include "llvm/CodeGen/MachineFrameInfo.h"
Lang Hames39609992013-11-29 03:07:54 +000015#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000016#include "llvm/CodeGen/MachineMemOperand.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/CodeGen/PseudoSourceValue.h"
19#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Lang Hames39609992013-11-29 03:07:54 +000020#include "llvm/CodeGen/StackMaps.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000021#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetLowering.h"
23#include "llvm/CodeGen/TargetRegisterInfo.h"
Matthias Braun88e21312015-06-13 03:42:11 +000024#include "llvm/CodeGen/TargetSchedule.h"
Andrew Trick10d5be42013-11-17 01:36:23 +000025#include "llvm/IR/DataLayout.h"
Evan Cheng49d4c0b2010-10-06 06:27:31 +000026#include "llvm/MC/MCAsmInfo.h"
Evan Cheng8264e272011-06-29 01:14:12 +000027#include "llvm/MC/MCInstrItineraries.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000028#include "llvm/Support/CommandLine.h"
Chris Lattner01614192009-08-02 04:58:19 +000029#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000030#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000031#include "llvm/Target/TargetMachine.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000032#include <cctype>
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000033
Chris Lattnerf6932b72005-01-19 06:53:34 +000034using namespace llvm;
Chris Lattner910b82f2002-10-28 23:55:33 +000035
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +000036static cl::opt<bool> DisableHazardRecognizer(
37 "disable-sched-hazard", cl::Hidden, cl::init(false),
38 cl::desc("Disable hazard detection during preRA scheduling"));
Chris Lattnere98a3c32009-08-02 05:20:37 +000039
Chris Lattner0d5644b2003-01-13 00:26:36 +000040TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner910b82f2002-10-28 23:55:33 +000041}
42
Evan Cheng8d71a752011-06-27 21:26:13 +000043const TargetRegisterClass*
Evan Cheng6cc775f2011-06-28 19:10:37 +000044TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000045 const TargetRegisterInfo *TRI,
46 const MachineFunction &MF) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +000047 if (OpNum >= MCID.getNumOperands())
Craig Topperc0196b12014-04-14 00:51:57 +000048 return nullptr;
Evan Cheng8d71a752011-06-27 21:26:13 +000049
Evan Cheng6cc775f2011-06-28 19:10:37 +000050 short RegClass = MCID.OpInfo[OpNum].RegClass;
51 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000052 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng8d71a752011-06-27 21:26:13 +000053
54 // Instructions like INSERT_SUBREG do not have fixed register classes.
55 if (RegClass < 0)
Craig Topperc0196b12014-04-14 00:51:57 +000056 return nullptr;
Evan Cheng8d71a752011-06-27 21:26:13 +000057
58 // Otherwise just look it up normally.
59 return TRI->getRegClass(RegClass);
60}
61
Chris Lattner01614192009-08-02 04:58:19 +000062/// insertNoop - Insert a noop into the instruction stream at the specified
63/// point.
Andrew Trickc416ba62010-12-24 04:28:06 +000064void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattner01614192009-08-02 04:58:19 +000065 MachineBasicBlock::iterator MI) const {
66 llvm_unreachable("Target didn't implement insertNoop!");
67}
68
Alex Bradbury5518cbf2017-09-28 09:31:46 +000069static bool isAsmComment(const char *Str, const MCAsmInfo &MAI) {
70 return strncmp(Str, MAI.getCommentString().data(),
71 MAI.getCommentString().size()) == 0;
72}
73
Chris Lattnere98a3c32009-08-02 05:20:37 +000074/// Measure the specified inline asm to determine an approximation of its
75/// length.
Jim Grosbacha3df87f2011-03-24 18:46:34 +000076/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnere98a3c32009-08-02 05:20:37 +000077/// count as an instruction.
78/// Any other non-whitespace text is considered an instruction, with
Jim Grosbacha3df87f2011-03-24 18:46:34 +000079/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnere98a3c32009-08-02 05:20:37 +000080/// Variable-length instructions are not handled here; this function
81/// may be overloaded in the target code to do that.
Alex Bradbury5518cbf2017-09-28 09:31:46 +000082/// We implement a special case of the .space directive which takes only a
83/// single integer argument in base 10 that is the size in bytes. This is a
84/// restricted form of the GAS directive in that we only interpret
85/// simple--i.e. not a logical or arithmetic expression--size values without
86/// the optional fill value. This is primarily used for creating arbitrary
87/// sized inline asm blocks for testing purposes.
Chris Lattnere98a3c32009-08-02 05:20:37 +000088unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattnere9a75a62009-08-22 21:43:10 +000089 const MCAsmInfo &MAI) const {
Chris Lattnere98a3c32009-08-02 05:20:37 +000090 // Count the number of instructions in the asm.
Alex Bradbury5518cbf2017-09-28 09:31:46 +000091 bool AtInsnStart = true;
92 unsigned Length = 0;
Chris Lattnere98a3c32009-08-02 05:20:37 +000093 for (; *Str; ++Str) {
Jim Grosbacha3df87f2011-03-24 18:46:34 +000094 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
Matt Arsenaultaccddac2016-07-01 23:26:50 +000095 strlen(MAI.getSeparatorString())) == 0) {
Alex Bradbury5518cbf2017-09-28 09:31:46 +000096 AtInsnStart = true;
97 } else if (isAsmComment(Str, MAI)) {
Matt Arsenaultaccddac2016-07-01 23:26:50 +000098 // Stop counting as an instruction after a comment until the next
99 // separator.
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000100 AtInsnStart = false;
Chris Lattnere98a3c32009-08-02 05:20:37 +0000101 }
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000102
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000103 if (AtInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
104 unsigned AddLength = MAI.getMaxInstLength();
105 if (strncmp(Str, ".space", 6) == 0) {
106 char *EStr;
107 int SpaceSize;
108 SpaceSize = strtol(Str + 6, &EStr, 10);
109 SpaceSize = SpaceSize < 0 ? 0 : SpaceSize;
110 while (*EStr != '\n' && std::isspace(static_cast<unsigned char>(*EStr)))
111 ++EStr;
112 if (*EStr == '\0' || *EStr == '\n' ||
113 isAsmComment(EStr, MAI)) // Successfully parsed .space argument
114 AddLength = SpaceSize;
115 }
116 Length += AddLength;
117 AtInsnStart = false;
Matt Arsenaultaccddac2016-07-01 23:26:50 +0000118 }
Chris Lattnere98a3c32009-08-02 05:20:37 +0000119 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000120
Alex Bradbury5518cbf2017-09-28 09:31:46 +0000121 return Length;
Chris Lattnere98a3c32009-08-02 05:20:37 +0000122}
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000123
124/// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
125/// after it, replacing it with an unconditional branch to NewDest.
126void
127TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
128 MachineBasicBlock *NewDest) const {
129 MachineBasicBlock *MBB = Tail->getParent();
130
131 // Remove all the old successors of MBB from the CFG.
132 while (!MBB->succ_empty())
133 MBB->removeSuccessor(MBB->succ_begin());
134
Justin Bognerec5ea362016-03-25 18:38:48 +0000135 // Save off the debug loc before erasing the instruction.
136 DebugLoc DL = Tail->getDebugLoc();
137
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000138 // Remove all the dead instructions from the end of MBB.
139 MBB->erase(Tail, MBB->end());
140
141 // If MBB isn't immediately before MBB, insert a branch to it.
142 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000143 insertBranch(*MBB, NewDest, nullptr, SmallVector<MachineOperand, 0>(), DL);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000144 MBB->addSuccessor(NewDest);
145}
146
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000147MachineInstr *TargetInstrInfo::commuteInstructionImpl(MachineInstr &MI,
148 bool NewMI, unsigned Idx1,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000149 unsigned Idx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000151 bool HasDef = MCID.getNumDefs();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000152 if (HasDef && !MI.getOperand(0).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000153 // No idea how to commute this instruction. Target should implement its own.
Craig Topperc0196b12014-04-14 00:51:57 +0000154 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000155
Richard Trieue778e872015-09-28 22:54:43 +0000156 unsigned CommutableOpIdx1 = Idx1; (void)CommutableOpIdx1;
157 unsigned CommutableOpIdx2 = Idx2; (void)CommutableOpIdx2;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000158 assert(findCommutedOpIndices(MI, CommutableOpIdx1, CommutableOpIdx2) &&
159 CommutableOpIdx1 == Idx1 && CommutableOpIdx2 == Idx2 &&
160 "TargetInstrInfo::CommuteInstructionImpl(): not commutable operands.");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000161 assert(MI.getOperand(Idx1).isReg() && MI.getOperand(Idx2).isReg() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000162 "This only knows how to commute register operands so far");
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000163
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000164 unsigned Reg0 = HasDef ? MI.getOperand(0).getReg() : 0;
165 unsigned Reg1 = MI.getOperand(Idx1).getReg();
166 unsigned Reg2 = MI.getOperand(Idx2).getReg();
167 unsigned SubReg0 = HasDef ? MI.getOperand(0).getSubReg() : 0;
168 unsigned SubReg1 = MI.getOperand(Idx1).getSubReg();
169 unsigned SubReg2 = MI.getOperand(Idx2).getSubReg();
170 bool Reg1IsKill = MI.getOperand(Idx1).isKill();
171 bool Reg2IsKill = MI.getOperand(Idx2).isKill();
172 bool Reg1IsUndef = MI.getOperand(Idx1).isUndef();
173 bool Reg2IsUndef = MI.getOperand(Idx2).isUndef();
174 bool Reg1IsInternal = MI.getOperand(Idx1).isInternalRead();
175 bool Reg2IsInternal = MI.getOperand(Idx2).isInternalRead();
Geoff Berryd37dc772018-01-29 18:47:48 +0000176 // Avoid calling isRenamable for virtual registers since we assert that
177 // renamable property is only queried/set for physical registers.
178 bool Reg1IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg1)
179 ? MI.getOperand(Idx1).isRenamable()
180 : false;
181 bool Reg2IsRenamable = TargetRegisterInfo::isPhysicalRegister(Reg2)
182 ? MI.getOperand(Idx2).isRenamable()
183 : false;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000184 // If destination is tied to either of the commuted source register, then
185 // it must be updated.
186 if (HasDef && Reg0 == Reg1 &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000187 MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000188 Reg2IsKill = false;
189 Reg0 = Reg2;
190 SubReg0 = SubReg2;
191 } else if (HasDef && Reg0 == Reg2 &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000192 MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000193 Reg1IsKill = false;
194 Reg0 = Reg1;
195 SubReg0 = SubReg1;
196 }
197
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000198 MachineInstr *CommutedMI = nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000199 if (NewMI) {
200 // Create a new instruction.
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000201 MachineFunction &MF = *MI.getMF();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000202 CommutedMI = MF.CloneMachineInstr(&MI);
203 } else {
204 CommutedMI = &MI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000205 }
206
207 if (HasDef) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000208 CommutedMI->getOperand(0).setReg(Reg0);
209 CommutedMI->getOperand(0).setSubReg(SubReg0);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000210 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000211 CommutedMI->getOperand(Idx2).setReg(Reg1);
212 CommutedMI->getOperand(Idx1).setReg(Reg2);
213 CommutedMI->getOperand(Idx2).setSubReg(SubReg1);
214 CommutedMI->getOperand(Idx1).setSubReg(SubReg2);
215 CommutedMI->getOperand(Idx2).setIsKill(Reg1IsKill);
216 CommutedMI->getOperand(Idx1).setIsKill(Reg2IsKill);
217 CommutedMI->getOperand(Idx2).setIsUndef(Reg1IsUndef);
218 CommutedMI->getOperand(Idx1).setIsUndef(Reg2IsUndef);
219 CommutedMI->getOperand(Idx2).setIsInternalRead(Reg1IsInternal);
220 CommutedMI->getOperand(Idx1).setIsInternalRead(Reg2IsInternal);
Geoff Berryd37dc772018-01-29 18:47:48 +0000221 // Avoid calling setIsRenamable for virtual registers since we assert that
222 // renamable property is only queried/set for physical registers.
223 if (TargetRegisterInfo::isPhysicalRegister(Reg1))
224 CommutedMI->getOperand(Idx2).setIsRenamable(Reg1IsRenamable);
225 if (TargetRegisterInfo::isPhysicalRegister(Reg2))
226 CommutedMI->getOperand(Idx1).setIsRenamable(Reg2IsRenamable);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000227 return CommutedMI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000228}
229
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr &MI, bool NewMI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000231 unsigned OpIdx1,
232 unsigned OpIdx2) const {
233 // If OpIdx1 or OpIdx2 is not specified, then this method is free to choose
234 // any commutable operand, which is done in findCommutedOpIndices() method
235 // called below.
236 if ((OpIdx1 == CommuteAnyOperandIndex || OpIdx2 == CommuteAnyOperandIndex) &&
237 !findCommutedOpIndices(MI, OpIdx1, OpIdx2)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000238 assert(MI.isCommutable() &&
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000239 "Precondition violation: MI must be commutable.");
240 return nullptr;
241 }
242 return commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
243}
244
245bool TargetInstrInfo::fixCommutedOpIndices(unsigned &ResultIdx1,
246 unsigned &ResultIdx2,
247 unsigned CommutableOpIdx1,
248 unsigned CommutableOpIdx2) {
249 if (ResultIdx1 == CommuteAnyOperandIndex &&
250 ResultIdx2 == CommuteAnyOperandIndex) {
251 ResultIdx1 = CommutableOpIdx1;
252 ResultIdx2 = CommutableOpIdx2;
253 } else if (ResultIdx1 == CommuteAnyOperandIndex) {
254 if (ResultIdx2 == CommutableOpIdx1)
255 ResultIdx1 = CommutableOpIdx2;
256 else if (ResultIdx2 == CommutableOpIdx2)
257 ResultIdx1 = CommutableOpIdx1;
258 else
259 return false;
260 } else if (ResultIdx2 == CommuteAnyOperandIndex) {
261 if (ResultIdx1 == CommutableOpIdx1)
262 ResultIdx2 = CommutableOpIdx2;
263 else if (ResultIdx1 == CommutableOpIdx2)
264 ResultIdx2 = CommutableOpIdx1;
265 else
266 return false;
267 } else
268 // Check that the result operand indices match the given commutable
269 // operand indices.
270 return (ResultIdx1 == CommutableOpIdx1 && ResultIdx2 == CommutableOpIdx2) ||
271 (ResultIdx1 == CommutableOpIdx2 && ResultIdx2 == CommutableOpIdx1);
272
273 return true;
274}
275
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000276bool TargetInstrInfo::findCommutedOpIndices(MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000277 unsigned &SrcOpIdx1,
278 unsigned &SrcOpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000279 assert(!MI.isBundle() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000280 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
281
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000282 const MCInstrDesc &MCID = MI.getDesc();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000283 if (!MCID.isCommutable())
284 return false;
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000285
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000286 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
287 // is not true, then the target must implement this.
Andrew Kaylor16c4da02015-09-28 20:33:22 +0000288 unsigned CommutableOpIdx1 = MCID.getNumDefs();
289 unsigned CommutableOpIdx2 = CommutableOpIdx1 + 1;
290 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2,
291 CommutableOpIdx1, CommutableOpIdx2))
292 return false;
293
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000294 if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000295 // No idea.
296 return false;
297 return true;
298}
299
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000300bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
301 if (!MI.isTerminator()) return false;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000302
303 // Conditional branch is a special case.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000304 if (MI.isBranch() && !MI.isBarrier())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000305 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000306 if (!MI.isPredicable())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000307 return true;
308 return !isPredicated(MI);
309}
310
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000311bool TargetInstrInfo::PredicateInstruction(
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000312 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000313 bool MadeChange = false;
314
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000315 assert(!MI.isBundle() &&
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000316 "TargetInstrInfo::PredicateInstruction() can't handle bundles");
317
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000318 const MCInstrDesc &MCID = MI.getDesc();
319 if (!MI.isPredicable())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000320 return false;
321
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000322 for (unsigned j = 0, i = 0, e = MI.getNumOperands(); i != e; ++i) {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000323 if (MCID.OpInfo[i].isPredicate()) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000324 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000325 if (MO.isReg()) {
326 MO.setReg(Pred[j].getReg());
327 MadeChange = true;
328 } else if (MO.isImm()) {
329 MO.setImm(Pred[j].getImm());
330 MadeChange = true;
331 } else if (MO.isMBB()) {
332 MO.setMBB(Pred[j].getMBB());
333 MadeChange = true;
334 }
335 ++j;
336 }
337 }
338 return MadeChange;
339}
340
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000341bool TargetInstrInfo::hasLoadFromStackSlot(
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000342 const MachineInstr &MI,
343 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000344 size_t StartSize = Accesses.size();
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) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000348 if ((*o)->isLoad() &&
349 dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
350 Accesses.push_back(*o);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000351 }
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000352 return Accesses.size() != StartSize;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000353}
354
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000355bool TargetInstrInfo::hasStoreToStackSlot(
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000356 const MachineInstr &MI,
357 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000358 size_t StartSize = Accesses.size();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000359 for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
360 oe = MI.memoperands_end();
361 o != oe; ++o) {
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000362 if ((*o)->isStore() &&
363 dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
364 Accesses.push_back(*o);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000365 }
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000366 return Accesses.size() != StartSize;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000367}
368
Andrew Trick10d5be42013-11-17 01:36:23 +0000369bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC,
370 unsigned SubIdx, unsigned &Size,
371 unsigned &Offset,
Eric Christopher7585fb22015-03-19 23:06:21 +0000372 const MachineFunction &MF) const {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000373 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Andrew Trick10d5be42013-11-17 01:36:23 +0000374 if (!SubIdx) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000375 Size = TRI->getSpillSize(*RC);
Andrew Trick10d5be42013-11-17 01:36:23 +0000376 Offset = 0;
377 return true;
378 }
Eric Christopher7585fb22015-03-19 23:06:21 +0000379 unsigned BitSize = TRI->getSubRegIdxSize(SubIdx);
Bjorn Petterssonc8b782c2018-08-09 15:19:07 +0000380 // Convert bit size to byte size.
Andrew Trick10d5be42013-11-17 01:36:23 +0000381 if (BitSize % 8)
382 return false;
383
Eric Christopher7585fb22015-03-19 23:06:21 +0000384 int BitOffset = TRI->getSubRegIdxOffset(SubIdx);
Andrew Trick10d5be42013-11-17 01:36:23 +0000385 if (BitOffset < 0 || BitOffset % 8)
386 return false;
387
388 Size = BitSize /= 8;
389 Offset = (unsigned)BitOffset / 8;
390
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000391 assert(TRI->getSpillSize(*RC) >= (Offset + Size) && "bad subregister range");
Andrew Trick10d5be42013-11-17 01:36:23 +0000392
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000393 if (!MF.getDataLayout().isLittleEndian()) {
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000394 Offset = TRI->getSpillSize(*RC) - (Offset + Size);
Andrew Trick10d5be42013-11-17 01:36:23 +0000395 }
396 return true;
397}
398
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000399void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB,
400 MachineBasicBlock::iterator I,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000401 unsigned DestReg, unsigned SubIdx,
402 const MachineInstr &Orig,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000403 const TargetRegisterInfo &TRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000404 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000405 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
406 MBB.insert(I, MI);
407}
408
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000409bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0,
410 const MachineInstr &MI1,
411 const MachineRegisterInfo *MRI) const {
412 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000413}
414
Matthias Braun55bc9b32017-08-22 23:56:30 +0000415MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
416 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000417 assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated");
Matthias Braun55bc9b32017-08-22 23:56:30 +0000418 MachineFunction &MF = *MBB.getParent();
419 return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000420}
421
422// If the COPY instruction in MI can be folded to a stack operation, return
423// the register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000424static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000425 unsigned FoldIdx) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000426 assert(MI.isCopy() && "MI must be a COPY instruction");
427 if (MI.getNumOperands() != 2)
Craig Topperc0196b12014-04-14 00:51:57 +0000428 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000429 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
430
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000431 const MachineOperand &FoldOp = MI.getOperand(FoldIdx);
432 const MachineOperand &LiveOp = MI.getOperand(1 - FoldIdx);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000433
434 if (FoldOp.getSubReg() || LiveOp.getSubReg())
Craig Topperc0196b12014-04-14 00:51:57 +0000435 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000436
437 unsigned FoldReg = FoldOp.getReg();
438 unsigned LiveReg = LiveOp.getReg();
439
440 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
441 "Cannot fold physregs");
442
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000443 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000444 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
445
446 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
Craig Topperc0196b12014-04-14 00:51:57 +0000447 return RC->contains(LiveOp.getReg()) ? RC : nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000448
449 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
450 return RC;
451
452 // FIXME: Allow folding when register classes are memory compatible.
Craig Topperc0196b12014-04-14 00:51:57 +0000453 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000454}
455
Hans Wennborg9b9a5352017-04-21 21:48:41 +0000456void TargetInstrInfo::getNoop(MCInst &NopInst) const {
457 llvm_unreachable("Not implemented");
Rafael Espindola6865d6f2014-09-15 18:32:58 +0000458}
459
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000460static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000461 ArrayRef<unsigned> Ops, int FrameIndex,
Lang Hames39609992013-11-29 03:07:54 +0000462 const TargetInstrInfo &TII) {
463 unsigned StartIdx = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000464 switch (MI.getOpcode()) {
Philip Reames570dd002016-08-23 21:21:43 +0000465 case TargetOpcode::STACKMAP: {
466 // StackMapLiveValues are foldable
Sanjoy Das6d3c9132016-08-30 01:38:59 +0000467 StartIdx = StackMapOpers(&MI).getVarIdx();
Lang Hames39609992013-11-29 03:07:54 +0000468 break;
Philip Reames570dd002016-08-23 21:21:43 +0000469 }
Lang Hames39609992013-11-29 03:07:54 +0000470 case TargetOpcode::PATCHPOINT: {
Philip Reames570dd002016-08-23 21:21:43 +0000471 // For PatchPoint, the call args are not foldable (even if reported in the
472 // stackmap e.g. via anyregcc).
Sanjoy Das6d3c9132016-08-30 01:38:59 +0000473 StartIdx = PatchPointOpers(&MI).getVarIdx();
Lang Hames39609992013-11-29 03:07:54 +0000474 break;
475 }
Philip Reames2b1084a2016-08-31 15:12:17 +0000476 case TargetOpcode::STATEPOINT: {
477 // For statepoints, fold deopt and gc arguments, but not call arguments.
478 StartIdx = StatepointOpers(&MI).getVarIdx();
479 break;
480 }
Lang Hames39609992013-11-29 03:07:54 +0000481 default:
482 llvm_unreachable("unexpected stackmap opcode");
483 }
484
485 // Return false if any operands requested for folding are not foldable (not
486 // part of the stackmap's live values).
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000487 for (unsigned Op : Ops) {
488 if (Op < StartIdx)
Craig Topperc0196b12014-04-14 00:51:57 +0000489 return nullptr;
Lang Hames39609992013-11-29 03:07:54 +0000490 }
491
492 MachineInstr *NewMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000493 MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
Lang Hames39609992013-11-29 03:07:54 +0000494 MachineInstrBuilder MIB(MF, NewMI);
495
496 // No need to fold return, the meta data, and function arguments
497 for (unsigned i = 0; i < StartIdx; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000498 MIB.add(MI.getOperand(i));
Lang Hames39609992013-11-29 03:07:54 +0000499
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000500 for (unsigned i = StartIdx; i < MI.getNumOperands(); ++i) {
501 MachineOperand &MO = MI.getOperand(i);
David Majnemer0d955d02016-08-11 22:21:41 +0000502 if (is_contained(Ops, i)) {
Lang Hames39609992013-11-29 03:07:54 +0000503 unsigned SpillSize;
504 unsigned SpillOffset;
505 // Compute the spill slot size and offset.
506 const TargetRegisterClass *RC =
507 MF.getRegInfo().getRegClass(MO.getReg());
Eric Christopher7585fb22015-03-19 23:06:21 +0000508 bool Valid =
509 TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize, SpillOffset, MF);
Lang Hames39609992013-11-29 03:07:54 +0000510 if (!Valid)
511 report_fatal_error("cannot spill patchpoint subregister operand");
512 MIB.addImm(StackMaps::IndirectMemRefOp);
513 MIB.addImm(SpillSize);
514 MIB.addFrameIndex(FrameIndex);
Lang Hames2ce64a72013-12-07 03:30:59 +0000515 MIB.addImm(SpillOffset);
Lang Hames39609992013-11-29 03:07:54 +0000516 }
517 else
Diana Picus116bbab2017-01-13 09:58:52 +0000518 MIB.add(MO);
Lang Hames39609992013-11-29 03:07:54 +0000519 }
520 return NewMI;
521}
522
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000523MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
524 ArrayRef<unsigned> Ops, int FI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000525 LiveIntervals *LIS) const {
Justin Lebar0af80cd2016-07-15 18:26:59 +0000526 auto Flags = MachineMemOperand::MONone;
Sanjay Patel232669d2017-10-02 15:02:06 +0000527 for (unsigned OpIdx : Ops)
528 Flags |= MI.getOperand(OpIdx).isDef() ? MachineMemOperand::MOStore
529 : MachineMemOperand::MOLoad;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000530
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000531 MachineBasicBlock *MBB = MI.getParent();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000532 assert(MBB && "foldMemoryOperand needs an inserted instruction");
533 MachineFunction &MF = *MBB->getParent();
534
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000535 // If we're not folding a load into a subreg, the size of the load is the
536 // size of the spill slot. But if we are, we need to figure out what the
537 // actual load size is.
538 int64_t MemSize = 0;
539 const MachineFrameInfo &MFI = MF.getFrameInfo();
540 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
541
542 if (Flags & MachineMemOperand::MOStore) {
543 MemSize = MFI.getObjectSize(FI);
544 } else {
Sanjay Patel232669d2017-10-02 15:02:06 +0000545 for (unsigned OpIdx : Ops) {
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000546 int64_t OpSize = MFI.getObjectSize(FI);
547
Sanjay Patel232669d2017-10-02 15:02:06 +0000548 if (auto SubReg = MI.getOperand(OpIdx).getSubReg()) {
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000549 unsigned SubRegSize = TRI->getSubRegIdxSize(SubReg);
550 if (SubRegSize > 0 && !(SubRegSize % 8))
551 OpSize = SubRegSize / 8;
552 }
553
554 MemSize = std::max(MemSize, OpSize);
555 }
556 }
557
558 assert(MemSize && "Did not expect a zero-sized stack slot");
559
Craig Topperc0196b12014-04-14 00:51:57 +0000560 MachineInstr *NewMI = nullptr;
Lang Hames39609992013-11-29 03:07:54 +0000561
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000562 if (MI.getOpcode() == TargetOpcode::STACKMAP ||
Philip Reames2b1084a2016-08-31 15:12:17 +0000563 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
564 MI.getOpcode() == TargetOpcode::STATEPOINT) {
Lang Hames39609992013-11-29 03:07:54 +0000565 // Fold stackmap/patchpoint.
566 NewMI = foldPatchpoint(MF, MI, Ops, FI, *this);
Keno Fischere70b31f2015-06-08 20:09:58 +0000567 if (NewMI)
568 MBB->insert(MI, NewMI);
Lang Hames39609992013-11-29 03:07:54 +0000569 } else {
570 // Ask the target to do the actual folding.
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000571 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS);
Lang Hames39609992013-11-29 03:07:54 +0000572 }
Keno Fischere70b31f2015-06-08 20:09:58 +0000573
Lang Hames39609992013-11-29 03:07:54 +0000574 if (NewMI) {
Chandler Carruthc73c0302018-08-16 21:30:05 +0000575 NewMI->setMemRefs(MF, MI.memoperands());
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000576 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
577 assert((!(Flags & MachineMemOperand::MOStore) ||
578 NewMI->mayStore()) &&
579 "Folded a def to a non-store!");
580 assert((!(Flags & MachineMemOperand::MOLoad) ||
581 NewMI->mayLoad()) &&
582 "Folded a use to a non-load!");
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000583 assert(MFI.getObjectOffset(FI) != -1);
Alex Lorenze40c8a22015-08-11 23:09:45 +0000584 MachineMemOperand *MMO = MF.getMachineMemOperand(
Michael Kuperstein47eb85a2016-11-23 18:33:49 +0000585 MachinePointerInfo::getFixedStack(MF, FI), Flags, MemSize,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000586 MFI.getObjectAlignment(FI));
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000587 NewMI->addMemOperand(MF, MMO);
588
Keno Fischere70b31f2015-06-08 20:09:58 +0000589 return NewMI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000590 }
591
592 // Straight COPY may fold as load/store.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000593 if (!MI.isCopy() || Ops.size() != 1)
Craig Topperc0196b12014-04-14 00:51:57 +0000594 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000595
596 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
597 if (!RC)
Craig Topperc0196b12014-04-14 00:51:57 +0000598 return nullptr;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000599
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000600 const MachineOperand &MO = MI.getOperand(1 - Ops[0]);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000601 MachineBasicBlock::iterator Pos = MI;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000602
603 if (Flags == MachineMemOperand::MOStore)
604 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
605 else
606 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
Duncan P. N. Exon Smithaae6f3c2016-07-01 16:38:28 +0000607 return &*--Pos;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000608}
609
Sanjay Patel7998d372017-10-02 14:03:17 +0000610MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
611 ArrayRef<unsigned> Ops,
612 MachineInstr &LoadMI,
613 LiveIntervals *LIS) const {
614 assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!");
615#ifndef NDEBUG
Sanjay Patel232669d2017-10-02 15:02:06 +0000616 for (unsigned OpIdx : Ops)
617 assert(MI.getOperand(OpIdx).isUse() && "Folding load into def!");
Sanjay Patel7998d372017-10-02 14:03:17 +0000618#endif
Sanjay Patel232669d2017-10-02 15:02:06 +0000619
Sanjay Patel7998d372017-10-02 14:03:17 +0000620 MachineBasicBlock &MBB = *MI.getParent();
621 MachineFunction &MF = *MBB.getParent();
622
623 // Ask the target to do the actual folding.
624 MachineInstr *NewMI = nullptr;
625 int FrameIndex = 0;
626
627 if ((MI.getOpcode() == TargetOpcode::STACKMAP ||
628 MI.getOpcode() == TargetOpcode::PATCHPOINT ||
629 MI.getOpcode() == TargetOpcode::STATEPOINT) &&
630 isLoadFromStackSlot(LoadMI, FrameIndex)) {
631 // Fold stackmap/patchpoint.
632 NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this);
633 if (NewMI)
634 NewMI = &*MBB.insert(MI, NewMI);
635 } else {
636 // Ask the target to do the actual folding.
637 NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS);
638 }
639
640 if (!NewMI)
641 return nullptr;
642
643 // Copy the memoperands from the load to the folded instruction.
644 if (MI.memoperands_empty()) {
Chandler Carruthc73c0302018-08-16 21:30:05 +0000645 NewMI->setMemRefs(MF, LoadMI.memoperands());
Sanjay Patel7998d372017-10-02 14:03:17 +0000646 } else {
647 // Handle the rare case of folding multiple loads.
Chandler Carruthc73c0302018-08-16 21:30:05 +0000648 NewMI->setMemRefs(MF, MI.memoperands());
Sanjay Patel7998d372017-10-02 14:03:17 +0000649 for (MachineInstr::mmo_iterator I = LoadMI.memoperands_begin(),
650 E = LoadMI.memoperands_end();
651 I != E; ++I) {
652 NewMI->addMemOperand(MF, *I);
653 }
654 }
655 return NewMI;
656}
657
Chad Rosier03a47302015-09-21 15:09:11 +0000658bool TargetInstrInfo::hasReassociableOperands(
659 const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
660 const MachineOperand &Op1 = Inst.getOperand(1);
661 const MachineOperand &Op2 = Inst.getOperand(2);
662 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
663
664 // We need virtual register definitions for the operands that we will
665 // reassociate.
666 MachineInstr *MI1 = nullptr;
667 MachineInstr *MI2 = nullptr;
668 if (Op1.isReg() && TargetRegisterInfo::isVirtualRegister(Op1.getReg()))
669 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
670 if (Op2.isReg() && TargetRegisterInfo::isVirtualRegister(Op2.getReg()))
671 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
672
673 // And they need to be in the trace (otherwise, they won't have a depth).
Rafael Espindola84921b92015-10-24 23:11:13 +0000674 return MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB;
Chad Rosier03a47302015-09-21 15:09:11 +0000675}
676
677bool TargetInstrInfo::hasReassociableSibling(const MachineInstr &Inst,
678 bool &Commuted) const {
679 const MachineBasicBlock *MBB = Inst.getParent();
680 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
681 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg());
682 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
683 unsigned AssocOpcode = Inst.getOpcode();
684
685 // If only one operand has the same opcode and it's the second source operand,
686 // the operands must be commuted.
687 Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode;
688 if (Commuted)
689 std::swap(MI1, MI2);
690
691 // 1. The previous instruction must be the same type as Inst.
692 // 2. The previous instruction must have virtual register definitions for its
693 // operands in the same basic block as Inst.
694 // 3. The previous instruction's result must only be used by Inst.
Rafael Espindola84921b92015-10-24 23:11:13 +0000695 return MI1->getOpcode() == AssocOpcode &&
696 hasReassociableOperands(*MI1, MBB) &&
697 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
Chad Rosier03a47302015-09-21 15:09:11 +0000698}
699
700// 1. The operation must be associative and commutative.
701// 2. The instruction must have virtual register definitions for its
702// operands in the same basic block.
703// 3. The instruction must have a reassociable sibling.
704bool TargetInstrInfo::isReassociationCandidate(const MachineInstr &Inst,
705 bool &Commuted) const {
Rafael Espindola84921b92015-10-24 23:11:13 +0000706 return isAssociativeAndCommutative(Inst) &&
707 hasReassociableOperands(Inst, Inst.getParent()) &&
708 hasReassociableSibling(Inst, Commuted);
Chad Rosier03a47302015-09-21 15:09:11 +0000709}
710
711// The concept of the reassociation pass is that these operations can benefit
712// from this kind of transformation:
713//
714// A = ? op ?
715// B = A op X (Prev)
716// C = B op Y (Root)
717// -->
718// A = ? op ?
719// B = X op Y
720// C = A op B
721//
722// breaking the dependency between A and B, allowing them to be executed in
723// parallel (or back-to-back in a pipeline) instead of depending on each other.
724
725// FIXME: This has the potential to be expensive (compile time) while not
726// improving the code at all. Some ways to limit the overhead:
727// 1. Track successful transforms; bail out if hit rate gets too low.
728// 2. Only enable at -O3 or some other non-default optimization level.
729// 3. Pre-screen pattern candidates here: if an operand of the previous
730// instruction is known to not increase the critical path, then don't match
731// that pattern.
732bool TargetInstrInfo::getMachineCombinerPatterns(
733 MachineInstr &Root,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000734 SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
Chad Rosier03a47302015-09-21 15:09:11 +0000735 bool Commute;
736 if (isReassociationCandidate(Root, Commute)) {
737 // We found a sequence of instructions that may be suitable for a
738 // reassociation of operands to increase ILP. Specify each commutation
739 // possibility for the Prev instruction in the sequence and let the
740 // machine combiner decide if changing the operands is worthwhile.
741 if (Commute) {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000742 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_YB);
743 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_YB);
Chad Rosier03a47302015-09-21 15:09:11 +0000744 } else {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000745 Patterns.push_back(MachineCombinerPattern::REASSOC_AX_BY);
746 Patterns.push_back(MachineCombinerPattern::REASSOC_XA_BY);
Chad Rosier03a47302015-09-21 15:09:11 +0000747 }
748 return true;
749 }
750
751 return false;
752}
Sanjay Patel7998d372017-10-02 14:03:17 +0000753
Gerolf Hoflehner01b3a6182016-04-24 05:14:01 +0000754/// Return true when a code sequence can improve loop throughput.
755bool
756TargetInstrInfo::isThroughputPattern(MachineCombinerPattern Pattern) const {
757 return false;
758}
Sanjay Patel7998d372017-10-02 14:03:17 +0000759
Chad Rosier03a47302015-09-21 15:09:11 +0000760/// Attempt the reassociation transformation to reduce critical path length.
761/// See the above comments before getMachineCombinerPatterns().
762void TargetInstrInfo::reassociateOps(
763 MachineInstr &Root, MachineInstr &Prev,
Sanjay Patel387e66e2015-11-05 19:34:57 +0000764 MachineCombinerPattern Pattern,
Chad Rosier03a47302015-09-21 15:09:11 +0000765 SmallVectorImpl<MachineInstr *> &InsInstrs,
766 SmallVectorImpl<MachineInstr *> &DelInstrs,
767 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000768 MachineFunction *MF = Root.getMF();
Chad Rosier03a47302015-09-21 15:09:11 +0000769 MachineRegisterInfo &MRI = MF->getRegInfo();
770 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
771 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
772 const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI);
773
774 // This array encodes the operand index for each parameter because the
775 // operands may be commuted. Each row corresponds to a pattern value,
776 // and each column specifies the index of A, B, X, Y.
777 unsigned OpIdx[4][4] = {
778 { 1, 1, 2, 2 },
779 { 1, 2, 2, 1 },
780 { 2, 1, 1, 2 },
781 { 2, 2, 1, 1 }
782 };
783
Sanjay Patel387e66e2015-11-05 19:34:57 +0000784 int Row;
785 switch (Pattern) {
786 case MachineCombinerPattern::REASSOC_AX_BY: Row = 0; break;
787 case MachineCombinerPattern::REASSOC_AX_YB: Row = 1; break;
788 case MachineCombinerPattern::REASSOC_XA_BY: Row = 2; break;
789 case MachineCombinerPattern::REASSOC_XA_YB: Row = 3; break;
790 default: llvm_unreachable("unexpected MachineCombinerPattern");
791 }
792
793 MachineOperand &OpA = Prev.getOperand(OpIdx[Row][0]);
794 MachineOperand &OpB = Root.getOperand(OpIdx[Row][1]);
795 MachineOperand &OpX = Prev.getOperand(OpIdx[Row][2]);
796 MachineOperand &OpY = Root.getOperand(OpIdx[Row][3]);
Chad Rosier03a47302015-09-21 15:09:11 +0000797 MachineOperand &OpC = Root.getOperand(0);
798
799 unsigned RegA = OpA.getReg();
800 unsigned RegB = OpB.getReg();
801 unsigned RegX = OpX.getReg();
802 unsigned RegY = OpY.getReg();
803 unsigned RegC = OpC.getReg();
804
805 if (TargetRegisterInfo::isVirtualRegister(RegA))
806 MRI.constrainRegClass(RegA, RC);
807 if (TargetRegisterInfo::isVirtualRegister(RegB))
808 MRI.constrainRegClass(RegB, RC);
809 if (TargetRegisterInfo::isVirtualRegister(RegX))
810 MRI.constrainRegClass(RegX, RC);
811 if (TargetRegisterInfo::isVirtualRegister(RegY))
812 MRI.constrainRegClass(RegY, RC);
813 if (TargetRegisterInfo::isVirtualRegister(RegC))
814 MRI.constrainRegClass(RegC, RC);
815
816 // Create a new virtual register for the result of (X op Y) instead of
817 // recycling RegB because the MachineCombiner's computation of the critical
818 // path requires a new register definition rather than an existing one.
819 unsigned NewVR = MRI.createVirtualRegister(RC);
820 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
821
822 unsigned Opcode = Root.getOpcode();
823 bool KillA = OpA.isKill();
824 bool KillX = OpX.isKill();
825 bool KillY = OpY.isKill();
826
827 // Create new instructions for insertion.
828 MachineInstrBuilder MIB1 =
829 BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR)
830 .addReg(RegX, getKillRegState(KillX))
831 .addReg(RegY, getKillRegState(KillY));
832 MachineInstrBuilder MIB2 =
833 BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC)
834 .addReg(RegA, getKillRegState(KillA))
835 .addReg(NewVR, getKillRegState(true));
836
837 setSpecialOperandAttr(Root, Prev, *MIB1, *MIB2);
838
839 // Record new instructions for insertion and old instructions for deletion.
840 InsInstrs.push_back(MIB1);
841 InsInstrs.push_back(MIB2);
842 DelInstrs.push_back(&Prev);
843 DelInstrs.push_back(&Root);
844}
845
846void TargetInstrInfo::genAlternativeCodeSequence(
Sanjay Patel387e66e2015-11-05 19:34:57 +0000847 MachineInstr &Root, MachineCombinerPattern Pattern,
Chad Rosier03a47302015-09-21 15:09:11 +0000848 SmallVectorImpl<MachineInstr *> &InsInstrs,
849 SmallVectorImpl<MachineInstr *> &DelInstrs,
850 DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000851 MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
Chad Rosier03a47302015-09-21 15:09:11 +0000852
853 // Select the previous instruction in the sequence based on the input pattern.
854 MachineInstr *Prev = nullptr;
855 switch (Pattern) {
Sanjay Patel387e66e2015-11-05 19:34:57 +0000856 case MachineCombinerPattern::REASSOC_AX_BY:
857 case MachineCombinerPattern::REASSOC_XA_BY:
Chad Rosier03a47302015-09-21 15:09:11 +0000858 Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
859 break;
Sanjay Patel387e66e2015-11-05 19:34:57 +0000860 case MachineCombinerPattern::REASSOC_AX_YB:
861 case MachineCombinerPattern::REASSOC_XA_YB:
Chad Rosier03a47302015-09-21 15:09:11 +0000862 Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
863 break;
864 default:
865 break;
866 }
867
868 assert(Prev && "Unknown pattern for machine combiner");
869
870 reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg);
Chad Rosier03a47302015-09-21 15:09:11 +0000871}
872
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000873bool TargetInstrInfo::isReallyTriviallyReMaterializableGeneric(
874 const MachineInstr &MI, AliasAnalysis *AA) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000875 const MachineFunction &MF = *MI.getMF();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000876 const MachineRegisterInfo &MRI = MF.getRegInfo();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000877
878 // Remat clients assume operand 0 is the defined register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000879 if (!MI.getNumOperands() || !MI.getOperand(0).isReg())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000880 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000881 unsigned DefReg = MI.getOperand(0).getReg();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000882
883 // A sub-register definition can only be rematerialized if the instruction
884 // doesn't read the other parts of the register. Otherwise it is really a
885 // read-modify-write operation on the full virtual register which cannot be
886 // moved safely.
887 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000888 MI.getOperand(0).getSubReg() && MI.readsVirtualRegister(DefReg))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000889 return false;
890
891 // A load from a fixed stack slot can be rematerialized. This may be
892 // redundant with subsequent checks, but it's target-independent,
893 // simple, and a common case.
894 int FrameIdx = 0;
Eric Christopher9d916792014-07-23 22:12:03 +0000895 if (isLoadFromStackSlot(MI, FrameIdx) &&
Matthias Braun941a7052016-07-28 18:40:00 +0000896 MF.getFrameInfo().isImmutableObjectIndex(FrameIdx))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000897 return true;
898
899 // Avoid instructions obviously unsafe for remat.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000900 if (MI.isNotDuplicable() || MI.mayStore() || MI.hasUnmodeledSideEffects())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000901 return false;
902
903 // Don't remat inline asm. We have no idea how expensive it is
904 // even if it's side effect free.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000905 if (MI.isInlineAsm())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000906 return false;
907
908 // Avoid instructions which load from potentially varying memory.
Justin Lebard98cf002016-09-10 01:03:20 +0000909 if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000910 return false;
911
912 // If any of the registers accessed are non-constant, conservatively assume
913 // the instruction is not rematerializable.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000914 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
915 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000916 if (!MO.isReg()) continue;
917 unsigned Reg = MO.getReg();
918 if (Reg == 0)
919 continue;
920
921 // Check for a well-behaved physical register.
922 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
923 if (MO.isUse()) {
924 // If the physreg has no defs anywhere, it's just an ambient register
925 // and we can freely move its uses. Alternatively, if it's allocatable,
926 // it could get allocated to something with a def during allocation.
Matthias Braunde8c1b32016-10-28 18:05:09 +0000927 if (!MRI.isConstantPhysReg(Reg))
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000928 return false;
929 } else {
930 // A physreg def. We can't remat it.
931 return false;
932 }
933 continue;
934 }
935
936 // Only allow one virtual-register def. There may be multiple defs of the
937 // same virtual register, though.
938 if (MO.isDef() && Reg != DefReg)
939 return false;
940
941 // Don't allow any virtual-register uses. Rematting an instruction with
942 // virtual register uses would length the live ranges of the uses, which
943 // is not necessarily a good idea, certainly not "trivial".
944 if (MO.isUse())
945 return false;
946 }
947
948 // Everything checked out.
949 return true;
950}
951
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000952int TargetInstrInfo::getSPAdjust(const MachineInstr &MI) const {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000953 const MachineFunction *MF = MI.getMF();
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000954 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
955 bool StackGrowsDown =
956 TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
957
Matthias Braunfa3872e2015-05-18 20:27:55 +0000958 unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
959 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000960
Serge Pavlov49acf9c2017-04-13 14:10:52 +0000961 if (!isFrameInstr(MI))
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000962 return 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000963
Serge Pavlov49acf9c2017-04-13 14:10:52 +0000964 int SPAdj = TFI->alignSPAdjust(getFrameSize(MI));
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000965
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000966 if ((!StackGrowsDown && MI.getOpcode() == FrameSetupOpcode) ||
967 (StackGrowsDown && MI.getOpcode() == FrameDestroyOpcode))
Michael Kuperstein8c65e312015-01-08 11:04:38 +0000968 SPAdj = -SPAdj;
969
970 return SPAdj;
971}
972
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000973/// isSchedulingBoundary - Test if the given instruction should be
974/// considered a scheduling boundary. This primarily includes labels
975/// and terminators.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000976bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000977 const MachineBasicBlock *MBB,
978 const MachineFunction &MF) const {
979 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000980 if (MI.isTerminator() || MI.isPosition())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000981 return true;
982
983 // Don't attempt to schedule around any instruction that defines
984 // a stack-oriented pointer, as it's unlikely to be profitable. This
985 // saves compile time, because it doesn't require every single
986 // stack slot reference to depend on the instruction that does the
987 // modification.
Eric Christopherfc6de422014-08-05 02:39:49 +0000988 const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
989 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000990 return MI.modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI);
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +0000991}
992
993// Provide a global flag for disabling the PreRA hazard recognizer that targets
994// may choose to honor.
995bool TargetInstrInfo::usePreRAHazardRecognizer() const {
996 return !DisableHazardRecognizer;
997}
998
999// Default implementation of CreateTargetRAHazardRecognizer.
1000ScheduleHazardRecognizer *TargetInstrInfo::
Eric Christopherf047bfd2014-06-13 22:38:52 +00001001CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001002 const ScheduleDAG *DAG) const {
1003 // Dummy hazard recognizer allows all instructions to issue.
1004 return new ScheduleHazardRecognizer();
1005}
1006
1007// Default implementation of CreateTargetMIHazardRecognizer.
1008ScheduleHazardRecognizer *TargetInstrInfo::
1009CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
1010 const ScheduleDAG *DAG) const {
1011 return (ScheduleHazardRecognizer *)
1012 new ScoreboardHazardRecognizer(II, DAG, "misched");
1013}
1014
1015// Default implementation of CreateTargetPostRAHazardRecognizer.
1016ScheduleHazardRecognizer *TargetInstrInfo::
1017CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
1018 const ScheduleDAG *DAG) const {
1019 return (ScheduleHazardRecognizer *)
1020 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
1021}
1022
1023//===----------------------------------------------------------------------===//
1024// SelectionDAG latency interface.
1025//===----------------------------------------------------------------------===//
1026
1027int
1028TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1029 SDNode *DefNode, unsigned DefIdx,
1030 SDNode *UseNode, unsigned UseIdx) const {
1031 if (!ItinData || ItinData->isEmpty())
1032 return -1;
1033
1034 if (!DefNode->isMachineOpcode())
1035 return -1;
1036
1037 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
1038 if (!UseNode->isMachineOpcode())
1039 return ItinData->getOperandCycle(DefClass, DefIdx);
1040 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
1041 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1042}
1043
1044int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1045 SDNode *N) const {
1046 if (!ItinData || ItinData->isEmpty())
1047 return 1;
1048
1049 if (!N->isMachineOpcode())
1050 return 1;
1051
1052 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
1053}
1054
1055//===----------------------------------------------------------------------===//
1056// MachineInstr latency interface.
1057//===----------------------------------------------------------------------===//
1058
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001059unsigned TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1060 const MachineInstr &MI) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001061 if (!ItinData || ItinData->isEmpty())
1062 return 1;
1063
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001064 unsigned Class = MI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001065 int UOps = ItinData->Itineraries[Class].NumMicroOps;
1066 if (UOps >= 0)
1067 return UOps;
1068
1069 // The # of u-ops is dynamically determined. The specific target should
1070 // override this function to return the right number.
1071 return 1;
1072}
1073
1074/// Return the default expected latency for a def based on it's opcode.
Pete Cooper11759452014-09-02 17:43:54 +00001075unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel &SchedModel,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001076 const MachineInstr &DefMI) const {
1077 if (DefMI.isTransient())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001078 return 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001079 if (DefMI.mayLoad())
Pete Cooper11759452014-09-02 17:43:54 +00001080 return SchedModel.LoadLatency;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001081 if (isHighLatencyDef(DefMI.getOpcode()))
Pete Cooper11759452014-09-02 17:43:54 +00001082 return SchedModel.HighLatency;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001083 return 1;
1084}
1085
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001086unsigned TargetInstrInfo::getPredicationCost(const MachineInstr &) const {
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00001087 return 0;
1088}
1089
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001090unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1091 const MachineInstr &MI,
1092 unsigned *PredCost) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001093 // Default to one cycle for no itinerary. However, an "empty" itinerary may
1094 // still have a MinLatency property, which getStageLatency checks.
1095 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001096 return MI.mayLoad() ? 2 : 1;
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001097
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001098 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001099}
1100
Matthias Braun88e21312015-06-13 03:42:11 +00001101bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001102 const MachineInstr &DefMI,
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001103 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00001104 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001105 if (!ItinData || ItinData->isEmpty())
1106 return false;
1107
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001108 unsigned DefClass = DefMI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001109 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1110 return (DefCycle != -1 && DefCycle <= 1);
1111}
1112
1113/// Both DefMI and UseMI must be valid. By default, call directly to the
1114/// itinerary. This may be overriden by the target.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001115int TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1116 const MachineInstr &DefMI,
1117 unsigned DefIdx,
1118 const MachineInstr &UseMI,
1119 unsigned UseIdx) const {
1120 unsigned DefClass = DefMI.getDesc().getSchedClass();
1121 unsigned UseClass = UseMI.getDesc().getSchedClass();
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001122 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1123}
1124
1125/// If we can determine the operand latency from the def only, without itinerary
1126/// lookup, do so. Otherwise return -1.
1127int TargetInstrInfo::computeDefOperandLatency(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001128 const InstrItineraryData *ItinData, const MachineInstr &DefMI) const {
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001129
1130 // Let the target hook getInstrLatency handle missing itineraries.
1131 if (!ItinData)
1132 return getInstrLatency(ItinData, DefMI);
1133
Andrew Trickde2109e2013-06-15 04:49:57 +00001134 if(ItinData->isEmpty())
Jakob Stoklund Olesenc351aed2012-11-28 02:35:13 +00001135 return defaultDefLatency(ItinData->SchedModel, DefMI);
1136
1137 // ...operand lookup required
1138 return -1;
1139}
1140
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001141bool TargetInstrInfo::getRegSequenceInputs(
1142 const MachineInstr &MI, unsigned DefIdx,
1143 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
Quentin Colombet8427df92014-08-12 17:11:26 +00001144 assert((MI.isRegSequence() ||
1145 MI.isRegSequenceLike()) && "Instruction do not have the proper type");
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001146
1147 if (!MI.isRegSequence())
1148 return getRegSequenceLikeInputs(MI, DefIdx, InputRegs);
1149
1150 // We are looking at:
1151 // Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
1152 assert(DefIdx == 0 && "REG_SEQUENCE only has one def");
1153 for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
1154 OpIdx += 2) {
1155 const MachineOperand &MOReg = MI.getOperand(OpIdx);
Matthias Braunea4359e2018-01-11 22:30:43 +00001156 if (MOReg.isUndef())
1157 continue;
Quentin Colombetd533cdf2014-08-11 22:17:14 +00001158 const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1);
1159 assert(MOSubIdx.isImm() &&
1160 "One of the subindex of the reg_sequence is not an immediate");
1161 // Record Reg:SubReg, SubIdx.
1162 InputRegs.push_back(RegSubRegPairAndIdx(MOReg.getReg(), MOReg.getSubReg(),
1163 (unsigned)MOSubIdx.getImm()));
1164 }
1165 return true;
1166}
Quentin Colombet7e75cba2014-08-20 21:51:26 +00001167
1168bool TargetInstrInfo::getExtractSubregInputs(
1169 const MachineInstr &MI, unsigned DefIdx,
1170 RegSubRegPairAndIdx &InputReg) const {
1171 assert((MI.isExtractSubreg() ||
1172 MI.isExtractSubregLike()) && "Instruction do not have the proper type");
1173
1174 if (!MI.isExtractSubreg())
1175 return getExtractSubregLikeInputs(MI, DefIdx, InputReg);
1176
1177 // We are looking at:
1178 // Def = EXTRACT_SUBREG v0.sub1, sub0.
1179 assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def");
1180 const MachineOperand &MOReg = MI.getOperand(1);
Matthias Braunea4359e2018-01-11 22:30:43 +00001181 if (MOReg.isUndef())
1182 return false;
Quentin Colombet7e75cba2014-08-20 21:51:26 +00001183 const MachineOperand &MOSubIdx = MI.getOperand(2);
1184 assert(MOSubIdx.isImm() &&
1185 "The subindex of the extract_subreg is not an immediate");
1186
1187 InputReg.Reg = MOReg.getReg();
1188 InputReg.SubReg = MOReg.getSubReg();
1189 InputReg.SubIdx = (unsigned)MOSubIdx.getImm();
1190 return true;
1191}
Quentin Colombet7e3da662014-08-20 23:49:36 +00001192
1193bool TargetInstrInfo::getInsertSubregInputs(
1194 const MachineInstr &MI, unsigned DefIdx,
1195 RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const {
1196 assert((MI.isInsertSubreg() ||
1197 MI.isInsertSubregLike()) && "Instruction do not have the proper type");
1198
1199 if (!MI.isInsertSubreg())
1200 return getInsertSubregLikeInputs(MI, DefIdx, BaseReg, InsertedReg);
1201
1202 // We are looking at:
1203 // Def = INSERT_SEQUENCE v0, v1, sub0.
1204 assert(DefIdx == 0 && "INSERT_SUBREG only has one def");
1205 const MachineOperand &MOBaseReg = MI.getOperand(1);
1206 const MachineOperand &MOInsertedReg = MI.getOperand(2);
Matthias Braunea4359e2018-01-11 22:30:43 +00001207 if (MOInsertedReg.isUndef())
1208 return false;
Quentin Colombet7e3da662014-08-20 23:49:36 +00001209 const MachineOperand &MOSubIdx = MI.getOperand(3);
1210 assert(MOSubIdx.isImm() &&
1211 "One of the subindex of the reg_sequence is not an immediate");
1212 BaseReg.Reg = MOBaseReg.getReg();
1213 BaseReg.SubReg = MOBaseReg.getSubReg();
1214
1215 InsertedReg.Reg = MOInsertedReg.getReg();
1216 InsertedReg.SubReg = MOInsertedReg.getSubReg();
1217 InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm();
1218 return true;
1219}