blob: ae38732065c780b128a49299827826c992e155ec [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 Trickb7e02892012-06-05 21:11:27 +000086/// computeOperandLatency - Compute and return the latency of the given data
87/// dependent def and use. DefMI must be a valid def. UseMI may be NULL for an
88/// unknown use. Depending on the subtarget's itinerary properties, this may or
89/// may not need to call getOperandLatency().
90///
91/// FindMin may be set to get the minimum vs. expected latency. Minimum
92/// latency is used for scheduling groups, while expected latency is for
93/// instruction cost and critical path.
94///
95/// For most subtargets, we don't need DefIdx or UseIdx to compute min latency.
96/// DefMI must be a valid definition, but UseMI may be NULL for an unknown use.
97unsigned TargetInstrInfo::
98computeOperandLatency(const InstrItineraryData *ItinData,
99 const TargetRegisterInfo *TRI,
100 const MachineInstr *DefMI, const MachineInstr *UseMI,
101 unsigned Reg, bool FindMin) const {
102
103 // Default to one cycle for missing itinerary. Empty itineraries still have
104 // a properties. We have one hard-coded exception for loads, to preserve
105 // existing behavior.
106 if (!ItinData)
107 return DefMI->mayLoad() ? 2 : 1;
108
109 // Return a latency based on the itinerary properties and defining instruction
110 // if possible. Some common subtargets don't require per-operand latency,
111 // especially for minimum latencies.
112 if (FindMin) {
113 // If MinLatency is valid, call getInstrLatency. This uses Stage latency if
114 // it exists before defaulting to MinLatency.
115 if (ItinData->Props.MinLatency >= 0)
116 return getInstrLatency(ItinData, DefMI);
117
118 // If MinLatency is invalid, OperandLatency is interpreted as MinLatency.
119 // For empty itineraries, short-cirtuit the check and default to one cycle.
120 if (ItinData->isEmpty())
121 return 1;
122 }
123 else if(ItinData->isEmpty())
124 return defaultDefLatency(ItinData, DefMI);
125
126 // ...operand lookup required
127
128 // Find the definition of the register in the defining instruction.
129 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
130 if (DefIdx != -1) {
131 const MachineOperand &MO = DefMI->getOperand(DefIdx);
132 if (MO.isReg() && MO.isImplicit() &&
133 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
134 // This is an implicit def, getOperandLatency() won't return the correct
135 // latency. e.g.
136 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
137 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
138 // What we want is to compute latency between def of %D6/%D7 and use of
139 // %Q3 instead.
140 unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
141 if (DefMI->getOperand(Op2).isReg())
142 DefIdx = Op2;
143 }
144 // For all uses of the register, calculate the maxmimum latency
145 int OperLatency = -1;
146
147 // UseMI is null, then it must be a scheduling barrier.
148 if (!UseMI) {
149 unsigned DefClass = DefMI->getDesc().getSchedClass();
150 OperLatency = ItinData->getOperandCycle(DefClass, DefIdx);
151 }
152 else {
153 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
154 const MachineOperand &MO = UseMI->getOperand(i);
155 if (!MO.isReg() || !MO.isUse())
156 continue;
157 unsigned MOReg = MO.getReg();
158 if (MOReg != Reg)
159 continue;
160
161 int UseCycle = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, i);
162 OperLatency = std::max(OperLatency, UseCycle);
163 }
164 }
165 // If we found an operand latency, we're done.
166 if (OperLatency >= 0)
167 return OperLatency;
168 }
169 // No operand latency was found.
170 unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
171 // Expected latency is the max of the stage latency and itinerary props.
172 if (!FindMin)
173 InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI));
174 return InstrLatency;
175}
176
177unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
178 const MachineInstr *MI,
179 unsigned *PredCost) const {
180 // Default to one cycle for no itinerary. However, an "empty" itinerary may
181 // still have a MinLatency property, which getStageLatency checks.
182 if (!ItinData)
Evan Cheng8239daf2010-11-03 00:45:17 +0000183 return 1;
184
185 return ItinData->getStageLatency(MI->getDesc().getSchedClass());
186}
187
Evan Chengc8141df2010-10-26 02:08:50 +0000188bool TargetInstrInfo::hasLowDefLatency(const InstrItineraryData *ItinData,
189 const MachineInstr *DefMI,
190 unsigned DefIdx) const {
191 if (!ItinData || ItinData->isEmpty())
192 return false;
193
194 unsigned DefClass = DefMI->getDesc().getSchedClass();
195 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
196 return (DefCycle != -1 && DefCycle <= 1);
197}
Evan Chenga0792de2010-10-06 06:27:31 +0000198
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +0000199/// insertNoop - Insert a noop into the instruction stream at the specified
200/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000201void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +0000202 MachineBasicBlock::iterator MI) const {
203 llvm_unreachable("Target didn't implement insertNoop!");
204}
205
206
Chris Lattnerd90183d2009-08-02 05:20:37 +0000207/// Measure the specified inline asm to determine an approximation of its
208/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +0000209/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +0000210/// count as an instruction.
211/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +0000212/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-08-02 05:20:37 +0000213/// Variable-length instructions are not handled here; this function
214/// may be overloaded in the target code to do that.
215unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000216 const MCAsmInfo &MAI) const {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000217
218
Chris Lattnerd90183d2009-08-02 05:20:37 +0000219 // Count the number of instructions in the asm.
220 bool atInsnStart = true;
221 unsigned Length = 0;
222 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +0000223 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
224 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000225 atInsnStart = true;
Nick Lewycky24021232010-12-19 20:42:43 +0000226 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000227 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +0000228 atInsnStart = false;
229 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000230 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
231 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000232 atInsnStart = false;
233 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000234
Chris Lattnerd90183d2009-08-02 05:20:37 +0000235 return Length;
236}