blob: 92aac5fed330d811eec76243379ad78e60a1a415 [file] [log] [blame]
Chris Lattnerec352402004-08-01 05:04:00 +00001//===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Chris Lattnerec352402004-08-01 05:04:00 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Chris Lattnerec352402004-08-01 05:04:00 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a wrapper class for the 'Instruction' TableGen class.
11//
12//===----------------------------------------------------------------------===//
13
Stephen Hines37ed9c12014-12-01 14:51:49 -080014#ifndef LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
15#define LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
Chris Lattnerec352402004-08-01 05:04:00 +000016
Chris Lattner225549f2010-11-06 06:39:47 +000017#include "llvm/ADT/StringRef.h"
Stephen Hines36b56882014-04-23 16:57:46 -070018#include "llvm/CodeGen/MachineValueType.h"
Bob Wilsona49c7df2011-01-26 19:44:55 +000019#include "llvm/Support/SourceMgr.h"
Chris Lattnerec352402004-08-01 05:04:00 +000020#include <string>
Chris Lattnerec352402004-08-01 05:04:00 +000021#include <utility>
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000022#include <vector>
Chris Lattnerec352402004-08-01 05:04:00 +000023
24namespace llvm {
25 class Record;
Chris Lattner65303d62005-11-19 07:05:57 +000026 class DagInit;
Chris Lattner9414ae52010-03-27 20:09:24 +000027 class CodeGenTarget;
Chris Lattner4d43d0f2010-11-01 01:07:14 +000028 class StringRef;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000029
Chris Lattnerc240bb02010-11-01 04:03:32 +000030 class CGIOperandList {
Jeff Cohend41b30d2006-11-05 19:31:28 +000031 public:
Chris Lattnera7d479c2010-02-10 01:45:28 +000032 class ConstraintInfo {
33 enum { None, EarlyClobber, Tied } Kind;
34 unsigned OtherTiedOperand;
35 public:
36 ConstraintInfo() : Kind(None) {}
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000037
Chris Lattnera7d479c2010-02-10 01:45:28 +000038 static ConstraintInfo getEarlyClobber() {
39 ConstraintInfo I;
40 I.Kind = EarlyClobber;
Chris Lattnere555c9f2010-02-10 21:22:51 +000041 I.OtherTiedOperand = 0;
Chris Lattnera7d479c2010-02-10 01:45:28 +000042 return I;
43 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000044
Chris Lattnera7d479c2010-02-10 01:45:28 +000045 static ConstraintInfo getTied(unsigned Op) {
46 ConstraintInfo I;
47 I.Kind = Tied;
48 I.OtherTiedOperand = Op;
49 return I;
50 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000051
Chris Lattnera7d479c2010-02-10 01:45:28 +000052 bool isNone() const { return Kind == None; }
53 bool isEarlyClobber() const { return Kind == EarlyClobber; }
54 bool isTied() const { return Kind == Tied; }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000055
Chris Lattnera7d479c2010-02-10 01:45:28 +000056 unsigned getTiedOperand() const {
57 assert(isTied());
58 return OtherTiedOperand;
59 }
60 };
Jim Grosbach9ed2cee2010-10-08 18:09:59 +000061
Chris Lattnercf03da02004-08-11 02:22:39 +000062 /// OperandInfo - The information we keep track of for each operand in the
63 /// operand list for a tablegen instruction.
Chris Lattner87c59052004-08-01 07:42:39 +000064 struct OperandInfo {
Chris Lattnercf03da02004-08-11 02:22:39 +000065 /// Rec - The definition this operand is declared as.
Chris Lattner0e384b62005-08-19 16:57:28 +000066 ///
Chris Lattner87c59052004-08-01 07:42:39 +000067 Record *Rec;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000068
Chris Lattnercf03da02004-08-11 02:22:39 +000069 /// Name - If this operand was assigned a symbolic name, this is it,
70 /// otherwise, it's empty.
Chris Lattner87c59052004-08-01 07:42:39 +000071 std::string Name;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000072
Chris Lattnercf03da02004-08-11 02:22:39 +000073 /// PrinterMethodName - The method used to print operands of this type in
74 /// the asmprinter.
75 std::string PrinterMethodName;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000076
Jim Grosbach5013f742010-10-12 22:21:57 +000077 /// EncoderMethodName - The method used to get the machine operand value
78 /// for binary encoding. "getMachineOpValue" by default.
79 std::string EncoderMethodName;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000080
Benjamin Kramer5196c122011-07-14 21:47:18 +000081 /// OperandType - A value from MCOI::OperandType representing the type of
82 /// the operand.
83 std::string OperandType;
84
Chris Lattnercf03da02004-08-11 02:22:39 +000085 /// MIOperandNo - Currently (this is meant to be phased out), some logical
86 /// operands correspond to multiple MachineInstr operands. In the X86
87 /// target for example, one address operand is represented as 4
88 /// MachineOperands. Because of this, the operand number in the
89 /// OperandList may not match the MachineInstr operand num. Until it
90 /// does, this contains the MI operand index of this operand.
91 unsigned MIOperandNo;
Chris Lattnercfbf96a2005-08-18 23:38:41 +000092 unsigned MINumOperands; // The number of operands.
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000093
Chris Lattnerf64f9a42006-11-15 23:23:02 +000094 /// DoNotEncode - Bools are set to true in this vector for each operand in
95 /// the DisableEncoding list. These should not be emitted by the code
96 /// emitter.
97 std::vector<bool> DoNotEncode;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000098
Nate Begeman8ef9d162005-11-30 23:58:18 +000099 /// MIOperandInfo - Default MI operand type. Note an operand may be made
100 /// up of multiple MI operands.
David Greene05bce0b2011-07-29 22:43:06 +0000101 DagInit *MIOperandInfo;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000102
Chris Lattner0bb75002006-11-15 02:38:17 +0000103 /// Constraint info for this operand. This operand can have pieces, so we
104 /// track constraint info for each.
Chris Lattnera7d479c2010-02-10 01:45:28 +0000105 std::vector<ConstraintInfo> Constraints;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000106
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000107 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
Benjamin Kramer5196c122011-07-14 21:47:18 +0000108 const std::string &EMN, const std::string &OT, unsigned MION,
David Greene05bce0b2011-07-29 22:43:06 +0000109 unsigned MINO, DagInit *MIOI)
Chris Lattnerc240bb02010-11-01 04:03:32 +0000110 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
Benjamin Kramer5196c122011-07-14 21:47:18 +0000111 OperandType(OT), MIOperandNo(MION), MINumOperands(MINO),
112 MIOperandInfo(MIOI) {}
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000113
114
Chris Lattner9b0d4bf2010-11-02 22:55:03 +0000115 /// getTiedOperand - If this operand is tied to another one, return the
116 /// other operand number. Otherwise, return -1.
117 int getTiedRegister() const {
118 for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
119 const CGIOperandList::ConstraintInfo &CI = Constraints[j];
120 if (CI.isTied()) return CI.getTiedOperand();
121 }
122 return -1;
123 }
Chris Lattner87c59052004-08-01 07:42:39 +0000124 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000125
Chris Lattnerc240bb02010-11-01 04:03:32 +0000126 CGIOperandList(Record *D);
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000127
Chris Lattnerc240bb02010-11-01 04:03:32 +0000128 Record *TheDef; // The actual record containing this OperandList.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000129
Chris Lattnercedef1c2010-03-18 20:50:52 +0000130 /// NumDefs - Number of def operands declared, this is the number of
131 /// elements in the instruction's (outs) list.
Evan Cheng64d80e32007-07-19 01:14:50 +0000132 ///
133 unsigned NumDefs;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000134
Chris Lattnerec352402004-08-01 05:04:00 +0000135 /// OperandList - The list of declared operands, along with their declared
136 /// type (which is a record).
Chris Lattner87c59052004-08-01 07:42:39 +0000137 std::vector<OperandInfo> OperandList;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000138
Chris Lattnerc240bb02010-11-01 04:03:32 +0000139 // Information gleaned from the operand list.
140 bool isPredicable;
141 bool hasOptionalDef;
142 bool isVariadic;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000143
Chris Lattnerc240bb02010-11-01 04:03:32 +0000144 // Provide transparent accessors to the operand list.
Chris Lattnera90dbc12011-04-17 22:24:13 +0000145 bool empty() const { return OperandList.empty(); }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000146 unsigned size() const { return OperandList.size(); }
147 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
148 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
149 OperandInfo &back() { return OperandList.back(); }
150 const OperandInfo &back() const { return OperandList.back(); }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000151
Stephen Hinesdce4a402014-05-29 02:49:00 -0700152 typedef std::vector<OperandInfo>::iterator iterator;
153 typedef std::vector<OperandInfo>::const_iterator const_iterator;
154 iterator begin() { return OperandList.begin(); }
155 const_iterator begin() const { return OperandList.begin(); }
156 iterator end() { return OperandList.end(); }
157 const_iterator end() const { return OperandList.end(); }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000158
Chris Lattnerc240bb02010-11-01 04:03:32 +0000159 /// getOperandNamed - Return the index of the operand with the specified
160 /// non-empty name. If the instruction does not have an operand with the
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000161 /// specified name, abort.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000162 unsigned getOperandNamed(StringRef Name) const;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000163
Chris Lattnerc240bb02010-11-01 04:03:32 +0000164 /// hasOperandNamed - Query whether the instruction has an operand of the
165 /// given name. If so, return true and set OpIdx to the index of the
166 /// operand. Otherwise, return false.
167 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000168
Chris Lattnerc240bb02010-11-01 04:03:32 +0000169 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
170 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000171 /// This aborts if the name is invalid. If AllowWholeOp is true, references
172 /// to operands with suboperands are allowed, otherwise not.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000173 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
174 bool AllowWholeOp = true);
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000175
Chris Lattnerc240bb02010-11-01 04:03:32 +0000176 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
177 /// flat machineinstr operand #.
178 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
179 return OperandList[Op.first].MIOperandNo + Op.second;
180 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000181
Chris Lattnerc240bb02010-11-01 04:03:32 +0000182 /// getSubOperandNumber - Unflatten a operand number into an
183 /// operand/suboperand pair.
184 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
185 for (unsigned i = 0; ; ++i) {
186 assert(i < OperandList.size() && "Invalid flat operand #");
187 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
188 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
189 }
190 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000191
192
Chris Lattnerc240bb02010-11-01 04:03:32 +0000193 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
194 /// should not be emitted with the code emitter.
195 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
196 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
197 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
198 return OperandList[Op.first].DoNotEncode[Op.second];
199 return false;
200 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000201
Chris Lattnerc240bb02010-11-01 04:03:32 +0000202 void ProcessDisableEncoding(std::string Value);
203 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000204
Chris Lattnerc240bb02010-11-01 04:03:32 +0000205
206 class CodeGenInstruction {
207 public:
208 Record *TheDef; // The actual record defining this instruction.
209 std::string Namespace; // The namespace the instruction is in.
210
211 /// AsmString - The format string used to emit a .s file for the
212 /// instruction.
213 std::string AsmString;
214
215 /// Operands - This is information about the (ins) and (outs) list specified
216 /// to the instruction.
217 CGIOperandList Operands;
Chris Lattnerec352402004-08-01 05:04:00 +0000218
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000219 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
220 /// implicitly defined and used by the instruction.
221 std::vector<Record*> ImplicitDefs, ImplicitUses;
222
Chris Lattnerec352402004-08-01 05:04:00 +0000223 // Various boolean values we track for the instruction.
Stephen Hines36b56882014-04-23 16:57:46 -0700224 bool isReturn : 1;
225 bool isBranch : 1;
226 bool isIndirectBranch : 1;
227 bool isCompare : 1;
228 bool isMoveImm : 1;
229 bool isBitcast : 1;
230 bool isSelect : 1;
231 bool isBarrier : 1;
232 bool isCall : 1;
233 bool canFoldAsLoad : 1;
234 bool mayLoad : 1;
235 bool mayLoad_Unset : 1;
236 bool mayStore : 1;
237 bool mayStore_Unset : 1;
238 bool isPredicable : 1;
239 bool isConvertibleToThreeAddress : 1;
240 bool isCommutable : 1;
241 bool isTerminator : 1;
242 bool isReMaterializable : 1;
243 bool hasDelaySlot : 1;
244 bool usesCustomInserter : 1;
245 bool hasPostISelHook : 1;
246 bool hasCtrlDep : 1;
247 bool isNotDuplicable : 1;
248 bool hasSideEffects : 1;
249 bool hasSideEffects_Unset : 1;
250 bool neverHasSideEffects : 1;
251 bool isAsCheapAsAMove : 1;
252 bool hasExtraSrcRegAllocReq : 1;
253 bool hasExtraDefRegAllocReq : 1;
254 bool isCodeGenOnly : 1;
255 bool isPseudo : 1;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800256 bool isRegSequence : 1;
257 bool isExtractSubreg : 1;
258 bool isInsertSubreg : 1;
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000259
Joey Gouly715d98d2013-09-12 10:28:05 +0000260 std::string DeprecatedReason;
261 bool HasComplexDeprecationPredicate;
262
Jakob Stoklund Olesen912519a2012-08-24 00:31:16 +0000263 /// Are there any undefined flags?
264 bool hasUndefFlags() const {
265 return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;
266 }
267
268 // The record used to infer instruction flags, or NULL if no flag values
269 // have been inferred.
270 Record *InferredFrom;
Chris Lattnerec352402004-08-01 05:04:00 +0000271
Chris Lattnerf7808112010-11-01 02:15:23 +0000272 CodeGenInstruction(Record *R);
Chris Lattner87c59052004-08-01 07:42:39 +0000273
Chris Lattner9414ae52010-03-27 20:09:24 +0000274 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
275 /// implicit def and it has a known VT, return the VT, otherwise return
276 /// MVT::Other.
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000277 MVT::SimpleValueType
Chris Lattner9414ae52010-03-27 20:09:24 +0000278 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000279
280
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000281 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
282 /// include text from the specified variant, returning the new string.
283 static std::string FlattenAsmStringVariants(StringRef AsmString,
284 unsigned Variant);
Chris Lattnerec352402004-08-01 05:04:00 +0000285 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000286
287
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000288 /// CodeGenInstAlias - This represents an InstAlias definition.
289 class CodeGenInstAlias {
290 public:
291 Record *TheDef; // The actual record defining this InstAlias.
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000292
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000293 /// AsmString - The format string used to emit a .s file for the
294 /// instruction.
295 std::string AsmString;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000296
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000297 /// Result - The result instruction.
David Greene05bce0b2011-07-29 22:43:06 +0000298 DagInit *Result;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000299
Chris Lattner225549f2010-11-06 06:39:47 +0000300 /// ResultInst - The instruction generated by the alias (decoded from
301 /// Result).
302 CodeGenInstruction *ResultInst;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000303
304
Chris Lattner225549f2010-11-06 06:39:47 +0000305 struct ResultOperand {
Chris Lattner98c870f2010-11-06 19:25:43 +0000306 private:
Owen Anderson7e8921b2012-06-08 00:25:03 +0000307 std::string Name;
Chris Lattner225549f2010-11-06 06:39:47 +0000308 Record *R;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000309
Chris Lattner98c870f2010-11-06 19:25:43 +0000310 int64_t Imm;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000311 public:
Chris Lattner98c870f2010-11-06 19:25:43 +0000312 enum {
313 K_Record,
Chris Lattner90fd7972010-11-06 19:57:21 +0000314 K_Imm,
315 K_Reg
Chris Lattner98c870f2010-11-06 19:25:43 +0000316 } Kind;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000317
Owen Anderson7e8921b2012-06-08 00:25:03 +0000318 ResultOperand(std::string N, Record *r) : Name(N), R(r), Kind(K_Record) {}
Chris Lattner98c870f2010-11-06 19:25:43 +0000319 ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
Chris Lattner90fd7972010-11-06 19:57:21 +0000320 ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
Chris Lattner98c870f2010-11-06 19:25:43 +0000321
322 bool isRecord() const { return Kind == K_Record; }
323 bool isImm() const { return Kind == K_Imm; }
Chris Lattner90fd7972010-11-06 19:57:21 +0000324 bool isReg() const { return Kind == K_Reg; }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000325
Chris Lattner98c870f2010-11-06 19:25:43 +0000326 StringRef getName() const { assert(isRecord()); return Name; }
327 Record *getRecord() const { assert(isRecord()); return R; }
328 int64_t getImm() const { assert(isImm()); return Imm; }
Chris Lattner90fd7972010-11-06 19:57:21 +0000329 Record *getRegister() const { assert(isReg()); return R; }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700330
331 unsigned getMINumOperands() const;
Chris Lattner225549f2010-11-06 06:39:47 +0000332 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000333
Chris Lattner225549f2010-11-06 06:39:47 +0000334 /// ResultOperands - The decoded operands for the result instruction.
335 std::vector<ResultOperand> ResultOperands;
Bob Wilson5e8f2a62011-01-20 18:38:02 +0000336
Bob Wilsona49c7df2011-01-26 19:44:55 +0000337 /// ResultInstOperandIndex - For each operand, this vector holds a pair of
338 /// indices to identify the corresponding operand in the result
339 /// instruction. The first index specifies the operand and the second
340 /// index specifies the suboperand. If there are no suboperands or if all
341 /// of them are matched by the operand, the second value should be -1.
342 std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000343
Stephen Hinesdce4a402014-05-29 02:49:00 -0700344 CodeGenInstAlias(Record *R, unsigned Variant, CodeGenTarget &T);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000345
David Greene05bce0b2011-07-29 22:43:06 +0000346 bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +0000347 Record *InstOpRec, bool hasSubOps, ArrayRef<SMLoc> Loc,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000348 CodeGenTarget &T, ResultOperand &ResOp);
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000349 };
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000350}
Chris Lattnerec352402004-08-01 05:04:00 +0000351
352#endif