blob: 613efea73182484ef0d38ad83232df6a2f66de4b [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
Chris Lattner3501fea2003-01-14 22:00:31 +000014#include "llvm/Target/TargetInstrInfo.h"
Evan Chengd923fc62009-05-05 00:30:09 +000015#include "llvm/Target/TargetRegisterInfo.h"
Evan Chenga0792de2010-10-06 06:27:31 +000016#include "llvm/MC/MCAsmInfo.h"
Evan Chengab8be962011-06-29 01:14:12 +000017#include "llvm/MC/MCInstrItineraries.h"
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000018#include "llvm/Support/ErrorHandling.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000019#include <cctype>
Chris Lattner167b10c2005-01-19 06:53:34 +000020using namespace llvm;
Chris Lattner93fa7052002-10-28 23:55:33 +000021
Chris Lattnerd90183d2009-08-02 05:20:37 +000022//===----------------------------------------------------------------------===//
Chris Lattnerd90183d2009-08-02 05:20:37 +000023// TargetInstrInfo
24//===----------------------------------------------------------------------===//
25
Chris Lattner08084142003-01-13 00:26:36 +000026TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000027}
28
Evan Cheng15993f82011-06-27 21:26:13 +000029const TargetRegisterClass*
Evan Chenge837dea2011-06-28 19:10:37 +000030TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000031 const TargetRegisterInfo *TRI,
32 const MachineFunction &MF) const {
Evan Chenge837dea2011-06-28 19:10:37 +000033 if (OpNum >= MCID.getNumOperands())
Evan Cheng15993f82011-06-27 21:26:13 +000034 return 0;
35
Evan Chenge837dea2011-06-28 19:10:37 +000036 short RegClass = MCID.OpInfo[OpNum].RegClass;
37 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000038 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng15993f82011-06-27 21:26:13 +000039
40 // Instructions like INSERT_SUBREG do not have fixed register classes.
41 if (RegClass < 0)
42 return 0;
43
44 // Otherwise just look it up normally.
45 return TRI->getRegClass(RegClass);
46}
47
Evan Cheng5f54ce32010-09-09 18:18:55 +000048unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +000049TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
50 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +000051 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +000052 return 1;
53
54 unsigned Class = MI->getDesc().getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +000055 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +000056 if (UOps)
57 return UOps;
58
59 // The # of u-ops is dynamically determined. The specific target should
60 // override this function to return the right number.
61 return 1;
62}
63
Andrew Trickb7e02892012-06-05 21:11:27 +000064/// Return the default expected latency for a def based on it's opcode.
65unsigned TargetInstrInfo::defaultDefLatency(const InstrItineraryData *ItinData,
66 const MachineInstr *DefMI) const {
67 if (DefMI->mayLoad())
68 return ItinData->Props.LoadLatency;
69 if (isHighLatencyDef(DefMI->getOpcode()))
70 return ItinData->Props.HighLatency;
71 return 1;
72}
73
74/// Both DefMI and UseMI must be valid. By default, call directly to the
75/// itinerary. This may be overriden by the target.
Evan Chenga0792de2010-10-06 06:27:31 +000076int
77TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Andrew Trickb7e02892012-06-05 21:11:27 +000078 const MachineInstr *DefMI, unsigned DefIdx,
79 const MachineInstr *UseMI,
80 unsigned UseIdx) const {
Evan Chenga0792de2010-10-06 06:27:31 +000081 unsigned DefClass = DefMI->getDesc().getSchedClass();
82 unsigned UseClass = UseMI->getDesc().getSchedClass();
83 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
84}
85
Andrew Trick397f4e32012-06-07 19:42:04 +000086/// If we can determine the operand latency from the def only, without itinerary
87/// lookup, do so. Otherwise return -1.
88static int computeDefOperandLatency(
89 const TargetInstrInfo *TII, const InstrItineraryData *ItinData,
90 const MachineInstr *DefMI, bool FindMin) {
91
92 // Let the target hook getInstrLatency handle missing itineraries.
93 if (!ItinData)
94 return TII->getInstrLatency(ItinData, DefMI);
95
96 // Return a latency based on the itinerary properties and defining instruction
97 // if possible. Some common subtargets don't require per-operand latency,
98 // especially for minimum latencies.
99 if (FindMin) {
100 // If MinLatency is valid, call getInstrLatency. This uses Stage latency if
101 // it exists before defaulting to MinLatency.
102 if (ItinData->Props.MinLatency >= 0)
103 return TII->getInstrLatency(ItinData, DefMI);
104
105 // If MinLatency is invalid, OperandLatency is interpreted as MinLatency.
106 // For empty itineraries, short-cirtuit the check and default to one cycle.
107 if (ItinData->isEmpty())
108 return 1;
109 }
110 else if(ItinData->isEmpty())
111 return TII->defaultDefLatency(ItinData, DefMI);
112
113 // ...operand lookup required
114return -1;
115}
116
117/// computeOperandLatency - Compute and return the latency of the given data
118/// dependent def and use when the operand indices are already known.
119///
120/// FindMin may be set to get the minimum vs. expected latency.
121unsigned TargetInstrInfo::
122computeOperandLatency(const InstrItineraryData *ItinData,
123 const MachineInstr *DefMI, unsigned DefIdx,
124 const MachineInstr *UseMI, unsigned UseIdx,
125 bool FindMin) const {
126
127 int DefLatency = computeDefOperandLatency(this, ItinData, DefMI, FindMin);
128 if (DefLatency >= 0)
129 return DefLatency;
130
131 assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail");
132
133 int OperLatency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
134 if (OperLatency >= 0)
135 return OperLatency;
136
137 // No operand latency was found.
138 unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
139
140 // Expected latency is the max of the stage latency and itinerary props.
141 if (!FindMin)
142 InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI));
143 return InstrLatency;
144}
145
Andrew Trickb7e02892012-06-05 21:11:27 +0000146/// computeOperandLatency - Compute and return the latency of the given data
147/// dependent def and use. DefMI must be a valid def. UseMI may be NULL for an
148/// unknown use. Depending on the subtarget's itinerary properties, this may or
149/// may not need to call getOperandLatency().
150///
151/// FindMin may be set to get the minimum vs. expected latency. Minimum
152/// latency is used for scheduling groups, while expected latency is for
153/// instruction cost and critical path.
154///
155/// For most subtargets, we don't need DefIdx or UseIdx to compute min latency.
156/// DefMI must be a valid definition, but UseMI may be NULL for an unknown use.
157unsigned TargetInstrInfo::
158computeOperandLatency(const InstrItineraryData *ItinData,
159 const TargetRegisterInfo *TRI,
160 const MachineInstr *DefMI, const MachineInstr *UseMI,
161 unsigned Reg, bool FindMin) const {
162
Andrew Trick397f4e32012-06-07 19:42:04 +0000163 int DefLatency = computeDefOperandLatency(this, ItinData, DefMI, FindMin);
164 if (DefLatency >= 0)
165 return DefLatency;
Andrew Trickb7e02892012-06-05 21:11:27 +0000166
Andrew Trick397f4e32012-06-07 19:42:04 +0000167 assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail");
Andrew Trickb7e02892012-06-05 21:11:27 +0000168
169 // Find the definition of the register in the defining instruction.
170 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
171 if (DefIdx != -1) {
172 const MachineOperand &MO = DefMI->getOperand(DefIdx);
173 if (MO.isReg() && MO.isImplicit() &&
174 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
175 // This is an implicit def, getOperandLatency() won't return the correct
176 // latency. e.g.
177 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
178 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
179 // What we want is to compute latency between def of %D6/%D7 and use of
180 // %Q3 instead.
181 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
182 if (DefMI->getOperand(Op2).isReg())
183 DefIdx = Op2;
184 }
185 // For all uses of the register, calculate the maxmimum latency
186 int OperLatency = -1;
187
188 // UseMI is null, then it must be a scheduling barrier.
189 if (!UseMI) {
190 unsigned DefClass = DefMI->getDesc().getSchedClass();
191 OperLatency = ItinData->getOperandCycle(DefClass, DefIdx);
192 }
193 else {
194 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
195 const MachineOperand &MO = UseMI->getOperand(i);
196 if (!MO.isReg() || !MO.isUse())
197 continue;
198 unsigned MOReg = MO.getReg();
199 if (MOReg != Reg)
200 continue;
201
202 int UseCycle = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, i);
203 OperLatency = std::max(OperLatency, UseCycle);
204 }
205 }
206 // If we found an operand latency, we're done.
207 if (OperLatency >= 0)
208 return OperLatency;
209 }
210 // No operand latency was found.
211 unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
Andrew Trick397f4e32012-06-07 19:42:04 +0000212
Andrew Trickb7e02892012-06-05 21:11:27 +0000213 // Expected latency is the max of the stage latency and itinerary props.
214 if (!FindMin)
215 InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI));
216 return InstrLatency;
217}
218
219unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
220 const MachineInstr *MI,
221 unsigned *PredCost) const {
222 // Default to one cycle for no itinerary. However, an "empty" itinerary may
223 // still have a MinLatency property, which getStageLatency checks.
224 if (!ItinData)
Andrew Trick397f4e32012-06-07 19:42:04 +0000225 return MI->mayLoad() ? 2 : 1;
Evan Cheng8239daf2010-11-03 00:45:17 +0000226
227 return ItinData->getStageLatency(MI->getDesc().getSchedClass());
228}
229
Evan Chengc8141df2010-10-26 02:08:50 +0000230bool TargetInstrInfo::hasLowDefLatency(const InstrItineraryData *ItinData,
231 const MachineInstr *DefMI,
232 unsigned DefIdx) const {
233 if (!ItinData || ItinData->isEmpty())
234 return false;
235
236 unsigned DefClass = DefMI->getDesc().getSchedClass();
237 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
238 return (DefCycle != -1 && DefCycle <= 1);
239}
Evan Chenga0792de2010-10-06 06:27:31 +0000240
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +0000241/// insertNoop - Insert a noop into the instruction stream at the specified
242/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000243void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +0000244 MachineBasicBlock::iterator MI) const {
245 llvm_unreachable("Target didn't implement insertNoop!");
246}
247
248
Chris Lattnerd90183d2009-08-02 05:20:37 +0000249/// Measure the specified inline asm to determine an approximation of its
250/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +0000251/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +0000252/// count as an instruction.
253/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +0000254/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-08-02 05:20:37 +0000255/// Variable-length instructions are not handled here; this function
256/// may be overloaded in the target code to do that.
257unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000258 const MCAsmInfo &MAI) const {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000259
260
Chris Lattnerd90183d2009-08-02 05:20:37 +0000261 // Count the number of instructions in the asm.
262 bool atInsnStart = true;
263 unsigned Length = 0;
264 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +0000265 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
266 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000267 atInsnStart = true;
Nick Lewycky24021232010-12-19 20:42:43 +0000268 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000269 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +0000270 atInsnStart = false;
271 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000272 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
273 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000274 atInsnStart = false;
275 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000276
Chris Lattnerd90183d2009-08-02 05:20:37 +0000277 return Length;
278}