blob: ad39722ab0f816a5083d29b50c67de5f55574b1b [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
14#ifndef CODEGEN_INSTRUCTION_H
15#define CODEGEN_INSTRUCTION_H
16
Chris Lattner87c59052004-08-01 07:42:39 +000017#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner225549f2010-11-06 06:39:47 +000018#include "llvm/ADT/StringRef.h"
Bob Wilsona49c7df2011-01-26 19:44:55 +000019#include "llvm/Support/SourceMgr.h"
Chris Lattnerec352402004-08-01 05:04:00 +000020#include <string>
21#include <vector>
22#include <utility>
23
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
Chris Lattnercf03da02004-08-11 02:22:39 +000081 /// MIOperandNo - Currently (this is meant to be phased out), some logical
82 /// operands correspond to multiple MachineInstr operands. In the X86
83 /// target for example, one address operand is represented as 4
84 /// MachineOperands. Because of this, the operand number in the
85 /// OperandList may not match the MachineInstr operand num. Until it
86 /// does, this contains the MI operand index of this operand.
87 unsigned MIOperandNo;
Chris Lattnercfbf96a2005-08-18 23:38:41 +000088 unsigned MINumOperands; // The number of operands.
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000089
Chris Lattnerf64f9a42006-11-15 23:23:02 +000090 /// DoNotEncode - Bools are set to true in this vector for each operand in
91 /// the DisableEncoding list. These should not be emitted by the code
92 /// emitter.
93 std::vector<bool> DoNotEncode;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000094
Nate Begeman8ef9d162005-11-30 23:58:18 +000095 /// MIOperandInfo - Default MI operand type. Note an operand may be made
96 /// up of multiple MI operands.
Chris Lattner65303d62005-11-19 07:05:57 +000097 DagInit *MIOperandInfo;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +000098
Chris Lattner0bb75002006-11-15 02:38:17 +000099 /// Constraint info for this operand. This operand can have pieces, so we
100 /// track constraint info for each.
Chris Lattnera7d479c2010-02-10 01:45:28 +0000101 std::vector<ConstraintInfo> Constraints;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000102
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000103 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
Jim Grosbach5013f742010-10-12 22:21:57 +0000104 const std::string &EMN, unsigned MION, unsigned MINO,
105 DagInit *MIOI)
Chris Lattnerc240bb02010-11-01 04:03:32 +0000106 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
Chris Lattner9b0d4bf2010-11-02 22:55:03 +0000107 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000108
109
Chris Lattner9b0d4bf2010-11-02 22:55:03 +0000110 /// getTiedOperand - If this operand is tied to another one, return the
111 /// other operand number. Otherwise, return -1.
112 int getTiedRegister() const {
113 for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
114 const CGIOperandList::ConstraintInfo &CI = Constraints[j];
115 if (CI.isTied()) return CI.getTiedOperand();
116 }
117 return -1;
118 }
Chris Lattner87c59052004-08-01 07:42:39 +0000119 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000120
Chris Lattnerc240bb02010-11-01 04:03:32 +0000121 CGIOperandList(Record *D);
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000122
Chris Lattnerc240bb02010-11-01 04:03:32 +0000123 Record *TheDef; // The actual record containing this OperandList.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000124
Chris Lattnercedef1c2010-03-18 20:50:52 +0000125 /// NumDefs - Number of def operands declared, this is the number of
126 /// elements in the instruction's (outs) list.
Evan Cheng64d80e32007-07-19 01:14:50 +0000127 ///
128 unsigned NumDefs;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000129
Chris Lattnerec352402004-08-01 05:04:00 +0000130 /// OperandList - The list of declared operands, along with their declared
131 /// type (which is a record).
Chris Lattner87c59052004-08-01 07:42:39 +0000132 std::vector<OperandInfo> OperandList;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000133
Chris Lattnerc240bb02010-11-01 04:03:32 +0000134 // Information gleaned from the operand list.
135 bool isPredicable;
136 bool hasOptionalDef;
137 bool isVariadic;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000138
Chris Lattnerc240bb02010-11-01 04:03:32 +0000139 // Provide transparent accessors to the operand list.
Chris Lattnera90dbc12011-04-17 22:24:13 +0000140 bool empty() const { return OperandList.empty(); }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000141 unsigned size() const { return OperandList.size(); }
142 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
143 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
144 OperandInfo &back() { return OperandList.back(); }
145 const OperandInfo &back() const { return OperandList.back(); }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000146
147
Chris Lattnerc240bb02010-11-01 04:03:32 +0000148 /// getOperandNamed - Return the index of the operand with the specified
149 /// non-empty name. If the instruction does not have an operand with the
150 /// specified name, throw an exception.
151 unsigned getOperandNamed(StringRef Name) const;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000152
Chris Lattnerc240bb02010-11-01 04:03:32 +0000153 /// hasOperandNamed - Query whether the instruction has an operand of the
154 /// given name. If so, return true and set OpIdx to the index of the
155 /// operand. Otherwise, return false.
156 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000157
Chris Lattnerc240bb02010-11-01 04:03:32 +0000158 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
159 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
160 /// This throws an exception if the name is invalid. If AllowWholeOp is
161 /// true, references to operands with suboperands are allowed, otherwise
162 /// not.
163 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
164 bool AllowWholeOp = true);
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000165
Chris Lattnerc240bb02010-11-01 04:03:32 +0000166 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
167 /// flat machineinstr operand #.
168 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
169 return OperandList[Op.first].MIOperandNo + Op.second;
170 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000171
Chris Lattnerc240bb02010-11-01 04:03:32 +0000172 /// getSubOperandNumber - Unflatten a operand number into an
173 /// operand/suboperand pair.
174 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
175 for (unsigned i = 0; ; ++i) {
176 assert(i < OperandList.size() && "Invalid flat operand #");
177 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
178 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
179 }
180 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000181
182
Chris Lattnerc240bb02010-11-01 04:03:32 +0000183 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
184 /// should not be emitted with the code emitter.
185 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
186 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
187 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
188 return OperandList[Op.first].DoNotEncode[Op.second];
189 return false;
190 }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000191
Chris Lattnerc240bb02010-11-01 04:03:32 +0000192 void ProcessDisableEncoding(std::string Value);
193 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000194
Chris Lattnerc240bb02010-11-01 04:03:32 +0000195
196 class CodeGenInstruction {
197 public:
198 Record *TheDef; // The actual record defining this instruction.
199 std::string Namespace; // The namespace the instruction is in.
200
201 /// AsmString - The format string used to emit a .s file for the
202 /// instruction.
203 std::string AsmString;
204
205 /// Operands - This is information about the (ins) and (outs) list specified
206 /// to the instruction.
207 CGIOperandList Operands;
Chris Lattnerec352402004-08-01 05:04:00 +0000208
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000209 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
210 /// implicitly defined and used by the instruction.
211 std::vector<Record*> ImplicitDefs, ImplicitUses;
212
Chris Lattnerec352402004-08-01 05:04:00 +0000213 // Various boolean values we track for the instruction.
214 bool isReturn;
215 bool isBranch;
Owen Anderson20ab2902007-11-12 07:39:39 +0000216 bool isIndirectBranch;
Bill Wendling73739d02010-08-08 01:49:35 +0000217 bool isCompare;
Evan Chengc4af4632010-11-17 20:13:28 +0000218 bool isMoveImm;
Evan Cheng0f040a22011-03-15 05:09:26 +0000219 bool isBitcast;
Chris Lattnerec352402004-08-01 05:04:00 +0000220 bool isBarrier;
221 bool isCall;
Dan Gohman15511cf2008-12-03 18:15:48 +0000222 bool canFoldAsLoad;
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000223 bool mayLoad, mayStore;
Evan Cheng5127ce02007-05-16 20:45:24 +0000224 bool isPredicable;
Chris Lattneraad75aa2005-01-02 02:29:04 +0000225 bool isConvertibleToThreeAddress;
226 bool isCommutable;
Chris Lattnerec352402004-08-01 05:04:00 +0000227 bool isTerminator;
Dan Gohmand45eddd2007-06-26 00:48:07 +0000228 bool isReMaterializable;
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000229 bool hasDelaySlot;
Dan Gohman533297b2009-10-29 18:10:34 +0000230 bool usesCustomInserter;
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000231 bool hasCtrlDep;
Evan Chengeaa91b02007-06-19 01:26:51 +0000232 bool isNotDuplicable;
Bill Wendling8370d382008-05-28 22:54:52 +0000233 bool hasSideEffects;
Bill Wendling8370d382008-05-28 22:54:52 +0000234 bool neverHasSideEffects;
235 bool isAsCheapAsAMove;
Evan Cheng799d6972009-10-01 08:21:18 +0000236 bool hasExtraSrcRegAllocReq;
237 bool hasExtraDefRegAllocReq;
Jim Grosbach806fcc02011-07-06 21:33:38 +0000238 bool isPseudo;
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000239
Chris Lattnerec352402004-08-01 05:04:00 +0000240
Chris Lattnerf7808112010-11-01 02:15:23 +0000241 CodeGenInstruction(Record *R);
Chris Lattner87c59052004-08-01 07:42:39 +0000242
Chris Lattner9414ae52010-03-27 20:09:24 +0000243 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
244 /// implicit def and it has a known VT, return the VT, otherwise return
245 /// MVT::Other.
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000246 MVT::SimpleValueType
Chris Lattner9414ae52010-03-27 20:09:24 +0000247 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000248
249
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000250 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
251 /// include text from the specified variant, returning the new string.
252 static std::string FlattenAsmStringVariants(StringRef AsmString,
253 unsigned Variant);
Chris Lattnerec352402004-08-01 05:04:00 +0000254 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000255
256
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000257 /// CodeGenInstAlias - This represents an InstAlias definition.
258 class CodeGenInstAlias {
259 public:
260 Record *TheDef; // The actual record defining this InstAlias.
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000261
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000262 /// AsmString - The format string used to emit a .s file for the
263 /// instruction.
264 std::string AsmString;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000265
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000266 /// Result - The result instruction.
267 DagInit *Result;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000268
Chris Lattner225549f2010-11-06 06:39:47 +0000269 /// ResultInst - The instruction generated by the alias (decoded from
270 /// Result).
271 CodeGenInstruction *ResultInst;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000272
273
Chris Lattner225549f2010-11-06 06:39:47 +0000274 struct ResultOperand {
Chris Lattner98c870f2010-11-06 19:25:43 +0000275 private:
Chris Lattner225549f2010-11-06 06:39:47 +0000276 StringRef Name;
277 Record *R;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000278
Chris Lattner98c870f2010-11-06 19:25:43 +0000279 int64_t Imm;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000280 public:
Chris Lattner98c870f2010-11-06 19:25:43 +0000281 enum {
282 K_Record,
Chris Lattner90fd7972010-11-06 19:57:21 +0000283 K_Imm,
284 K_Reg
Chris Lattner98c870f2010-11-06 19:25:43 +0000285 } Kind;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000286
Chris Lattner98c870f2010-11-06 19:25:43 +0000287 ResultOperand(StringRef N, Record *r) : Name(N), R(r), Kind(K_Record) {}
288 ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
Chris Lattner90fd7972010-11-06 19:57:21 +0000289 ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
Chris Lattner98c870f2010-11-06 19:25:43 +0000290
291 bool isRecord() const { return Kind == K_Record; }
292 bool isImm() const { return Kind == K_Imm; }
Chris Lattner90fd7972010-11-06 19:57:21 +0000293 bool isReg() const { return Kind == K_Reg; }
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000294
Chris Lattner98c870f2010-11-06 19:25:43 +0000295 StringRef getName() const { assert(isRecord()); return Name; }
296 Record *getRecord() const { assert(isRecord()); return R; }
297 int64_t getImm() const { assert(isImm()); return Imm; }
Chris Lattner90fd7972010-11-06 19:57:21 +0000298 Record *getRegister() const { assert(isReg()); return R; }
Chris Lattner225549f2010-11-06 06:39:47 +0000299 };
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000300
Chris Lattner225549f2010-11-06 06:39:47 +0000301 /// ResultOperands - The decoded operands for the result instruction.
302 std::vector<ResultOperand> ResultOperands;
Bob Wilson5e8f2a62011-01-20 18:38:02 +0000303
Bob Wilsona49c7df2011-01-26 19:44:55 +0000304 /// ResultInstOperandIndex - For each operand, this vector holds a pair of
305 /// indices to identify the corresponding operand in the result
306 /// instruction. The first index specifies the operand and the second
307 /// index specifies the suboperand. If there are no suboperands or if all
308 /// of them are matched by the operand, the second value should be -1.
309 std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000310
Chris Lattner225549f2010-11-06 06:39:47 +0000311 CodeGenInstAlias(Record *R, CodeGenTarget &T);
Bob Wilsona49c7df2011-01-26 19:44:55 +0000312
313 bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
314 Record *InstOpRec, bool hasSubOps, SMLoc Loc,
315 CodeGenTarget &T, ResultOperand &ResOp);
Jim Grosbach0c4d44a2011-03-14 17:32:49 +0000316 };
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000317}
Chris Lattnerec352402004-08-01 05:04:00 +0000318
319#endif