blob: d58bfb12968d1c709158148ce754942ea7e705c7 [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"
Chris Lattnerec352402004-08-01 05:04:00 +000019#include <string>
20#include <vector>
21#include <utility>
22
23namespace llvm {
24 class Record;
Chris Lattner65303d62005-11-19 07:05:57 +000025 class DagInit;
Chris Lattner9414ae52010-03-27 20:09:24 +000026 class CodeGenTarget;
Chris Lattner4d43d0f2010-11-01 01:07:14 +000027 class StringRef;
Chris Lattnerc240bb02010-11-01 04:03:32 +000028
29 class CGIOperandList {
Jeff Cohend41b30d2006-11-05 19:31:28 +000030 public:
Chris Lattnera7d479c2010-02-10 01:45:28 +000031 class ConstraintInfo {
32 enum { None, EarlyClobber, Tied } Kind;
33 unsigned OtherTiedOperand;
34 public:
35 ConstraintInfo() : Kind(None) {}
Chris Lattnerc240bb02010-11-01 04:03:32 +000036
Chris Lattnera7d479c2010-02-10 01:45:28 +000037 static ConstraintInfo getEarlyClobber() {
38 ConstraintInfo I;
39 I.Kind = EarlyClobber;
Chris Lattnere555c9f2010-02-10 21:22:51 +000040 I.OtherTiedOperand = 0;
Chris Lattnera7d479c2010-02-10 01:45:28 +000041 return I;
42 }
Chris Lattnerc240bb02010-11-01 04:03:32 +000043
Chris Lattnera7d479c2010-02-10 01:45:28 +000044 static ConstraintInfo getTied(unsigned Op) {
45 ConstraintInfo I;
46 I.Kind = Tied;
47 I.OtherTiedOperand = Op;
48 return I;
49 }
Chris Lattnerc240bb02010-11-01 04:03:32 +000050
Chris Lattnera7d479c2010-02-10 01:45:28 +000051 bool isNone() const { return Kind == None; }
52 bool isEarlyClobber() const { return Kind == EarlyClobber; }
53 bool isTied() const { return Kind == Tied; }
Chris Lattnerc240bb02010-11-01 04:03:32 +000054
Chris Lattnera7d479c2010-02-10 01:45:28 +000055 unsigned getTiedOperand() const {
56 assert(isTied());
57 return OtherTiedOperand;
58 }
59 };
Jim Grosbach9ed2cee2010-10-08 18:09:59 +000060
Chris Lattnercf03da02004-08-11 02:22:39 +000061 /// OperandInfo - The information we keep track of for each operand in the
62 /// operand list for a tablegen instruction.
Chris Lattner87c59052004-08-01 07:42:39 +000063 struct OperandInfo {
Chris Lattnercf03da02004-08-11 02:22:39 +000064 /// Rec - The definition this operand is declared as.
Chris Lattner0e384b62005-08-19 16:57:28 +000065 ///
Chris Lattner87c59052004-08-01 07:42:39 +000066 Record *Rec;
Chris Lattnerc240bb02010-11-01 04:03:32 +000067
Chris Lattnercf03da02004-08-11 02:22:39 +000068 /// Name - If this operand was assigned a symbolic name, this is it,
69 /// otherwise, it's empty.
Chris Lattner87c59052004-08-01 07:42:39 +000070 std::string Name;
Chris Lattnerc240bb02010-11-01 04:03:32 +000071
Chris Lattnercf03da02004-08-11 02:22:39 +000072 /// PrinterMethodName - The method used to print operands of this type in
73 /// the asmprinter.
74 std::string PrinterMethodName;
Chris Lattnerc240bb02010-11-01 04:03:32 +000075
Jim Grosbach5013f742010-10-12 22:21:57 +000076 /// EncoderMethodName - The method used to get the machine operand value
77 /// for binary encoding. "getMachineOpValue" by default.
78 std::string EncoderMethodName;
Chris Lattnerc240bb02010-11-01 04:03:32 +000079
Chris Lattnercf03da02004-08-11 02:22:39 +000080 /// MIOperandNo - Currently (this is meant to be phased out), some logical
81 /// operands correspond to multiple MachineInstr operands. In the X86
82 /// target for example, one address operand is represented as 4
83 /// MachineOperands. Because of this, the operand number in the
84 /// OperandList may not match the MachineInstr operand num. Until it
85 /// does, this contains the MI operand index of this operand.
86 unsigned MIOperandNo;
Chris Lattnercfbf96a2005-08-18 23:38:41 +000087 unsigned MINumOperands; // The number of operands.
Chris Lattnerc240bb02010-11-01 04:03:32 +000088
Chris Lattnerf64f9a42006-11-15 23:23:02 +000089 /// DoNotEncode - Bools are set to true in this vector for each operand in
90 /// the DisableEncoding list. These should not be emitted by the code
91 /// emitter.
92 std::vector<bool> DoNotEncode;
Chris Lattnerc240bb02010-11-01 04:03:32 +000093
Nate Begeman8ef9d162005-11-30 23:58:18 +000094 /// MIOperandInfo - Default MI operand type. Note an operand may be made
95 /// up of multiple MI operands.
Chris Lattner65303d62005-11-19 07:05:57 +000096 DagInit *MIOperandInfo;
Chris Lattnerc240bb02010-11-01 04:03:32 +000097
Chris Lattner0bb75002006-11-15 02:38:17 +000098 /// Constraint info for this operand. This operand can have pieces, so we
99 /// track constraint info for each.
Chris Lattnera7d479c2010-02-10 01:45:28 +0000100 std::vector<ConstraintInfo> Constraints;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000101
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000102 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
Jim Grosbach5013f742010-10-12 22:21:57 +0000103 const std::string &EMN, unsigned MION, unsigned MINO,
104 DagInit *MIOI)
Chris Lattnerc240bb02010-11-01 04:03:32 +0000105 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
Chris Lattner9b0d4bf2010-11-02 22:55:03 +0000106 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
107
108
109 /// getTiedOperand - If this operand is tied to another one, return the
110 /// other operand number. Otherwise, return -1.
111 int getTiedRegister() const {
112 for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
113 const CGIOperandList::ConstraintInfo &CI = Constraints[j];
114 if (CI.isTied()) return CI.getTiedOperand();
115 }
116 return -1;
117 }
Chris Lattner87c59052004-08-01 07:42:39 +0000118 };
Chris Lattnerc240bb02010-11-01 04:03:32 +0000119
120 CGIOperandList(Record *D);
121
122 Record *TheDef; // The actual record containing this OperandList.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000123
Chris Lattnercedef1c2010-03-18 20:50:52 +0000124 /// NumDefs - Number of def operands declared, this is the number of
125 /// elements in the instruction's (outs) list.
Evan Cheng64d80e32007-07-19 01:14:50 +0000126 ///
127 unsigned NumDefs;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000128
Chris Lattnerec352402004-08-01 05:04:00 +0000129 /// OperandList - The list of declared operands, along with their declared
130 /// type (which is a record).
Chris Lattner87c59052004-08-01 07:42:39 +0000131 std::vector<OperandInfo> OperandList;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000132
133 // Information gleaned from the operand list.
134 bool isPredicable;
135 bool hasOptionalDef;
136 bool isVariadic;
137
138 // Provide transparent accessors to the operand list.
139 unsigned size() const { return OperandList.size(); }
140 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
141 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
142 OperandInfo &back() { return OperandList.back(); }
143 const OperandInfo &back() const { return OperandList.back(); }
144
145
146 /// getOperandNamed - Return the index of the operand with the specified
147 /// non-empty name. If the instruction does not have an operand with the
148 /// specified name, throw an exception.
149 unsigned getOperandNamed(StringRef Name) const;
150
151 /// hasOperandNamed - Query whether the instruction has an operand of the
152 /// given name. If so, return true and set OpIdx to the index of the
153 /// operand. Otherwise, return false.
154 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
155
156 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
157 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
158 /// This throws an exception if the name is invalid. If AllowWholeOp is
159 /// true, references to operands with suboperands are allowed, otherwise
160 /// not.
161 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
162 bool AllowWholeOp = true);
163
164 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
165 /// flat machineinstr operand #.
166 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
167 return OperandList[Op.first].MIOperandNo + Op.second;
168 }
169
170 /// getSubOperandNumber - Unflatten a operand number into an
171 /// operand/suboperand pair.
172 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
173 for (unsigned i = 0; ; ++i) {
174 assert(i < OperandList.size() && "Invalid flat operand #");
175 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
176 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
177 }
178 }
179
180
181 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
182 /// should not be emitted with the code emitter.
183 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
184 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
185 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
186 return OperandList[Op.first].DoNotEncode[Op.second];
187 return false;
188 }
189
190 void ProcessDisableEncoding(std::string Value);
191 };
192
193
194 class CodeGenInstruction {
195 public:
196 Record *TheDef; // The actual record defining this instruction.
197 std::string Namespace; // The namespace the instruction is in.
198
199 /// AsmString - The format string used to emit a .s file for the
200 /// instruction.
201 std::string AsmString;
202
203 /// Operands - This is information about the (ins) and (outs) list specified
204 /// to the instruction.
205 CGIOperandList Operands;
Chris Lattnerec352402004-08-01 05:04:00 +0000206
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000207 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
208 /// implicitly defined and used by the instruction.
209 std::vector<Record*> ImplicitDefs, ImplicitUses;
210
Chris Lattnerec352402004-08-01 05:04:00 +0000211 // Various boolean values we track for the instruction.
212 bool isReturn;
213 bool isBranch;
Owen Anderson20ab2902007-11-12 07:39:39 +0000214 bool isIndirectBranch;
Bill Wendling73739d02010-08-08 01:49:35 +0000215 bool isCompare;
Evan Chengc4af4632010-11-17 20:13:28 +0000216 bool isMoveImm;
Chris Lattnerec352402004-08-01 05:04:00 +0000217 bool isBarrier;
218 bool isCall;
Dan Gohman15511cf2008-12-03 18:15:48 +0000219 bool canFoldAsLoad;
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000220 bool mayLoad, mayStore;
Evan Cheng5127ce02007-05-16 20:45:24 +0000221 bool isPredicable;
Chris Lattneraad75aa2005-01-02 02:29:04 +0000222 bool isConvertibleToThreeAddress;
223 bool isCommutable;
Chris Lattnerec352402004-08-01 05:04:00 +0000224 bool isTerminator;
Dan Gohmand45eddd2007-06-26 00:48:07 +0000225 bool isReMaterializable;
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000226 bool hasDelaySlot;
Dan Gohman533297b2009-10-29 18:10:34 +0000227 bool usesCustomInserter;
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000228 bool hasCtrlDep;
Evan Chengeaa91b02007-06-19 01:26:51 +0000229 bool isNotDuplicable;
Bill Wendling8370d382008-05-28 22:54:52 +0000230 bool hasSideEffects;
Bill Wendling8370d382008-05-28 22:54:52 +0000231 bool neverHasSideEffects;
232 bool isAsCheapAsAMove;
Evan Cheng799d6972009-10-01 08:21:18 +0000233 bool hasExtraSrcRegAllocReq;
234 bool hasExtraDefRegAllocReq;
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000235
Chris Lattnerec352402004-08-01 05:04:00 +0000236
Chris Lattnerf7808112010-11-01 02:15:23 +0000237 CodeGenInstruction(Record *R);
Chris Lattner87c59052004-08-01 07:42:39 +0000238
Chris Lattner9414ae52010-03-27 20:09:24 +0000239 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
240 /// implicit def and it has a known VT, return the VT, otherwise return
241 /// MVT::Other.
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000242 MVT::SimpleValueType
Chris Lattner9414ae52010-03-27 20:09:24 +0000243 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000244
245
246 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
247 /// include text from the specified variant, returning the new string.
248 static std::string FlattenAsmStringVariants(StringRef AsmString,
249 unsigned Variant);
Chris Lattnerec352402004-08-01 05:04:00 +0000250 };
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000251
252
253 /// CodeGenInstAlias - This represents an InstAlias definition.
254 class CodeGenInstAlias {
255 public:
256 Record *TheDef; // The actual record defining this InstAlias.
257
258 /// AsmString - The format string used to emit a .s file for the
259 /// instruction.
260 std::string AsmString;
261
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000262 /// Result - The result instruction.
263 DagInit *Result;
264
Chris Lattner225549f2010-11-06 06:39:47 +0000265 /// ResultInst - The instruction generated by the alias (decoded from
266 /// Result).
267 CodeGenInstruction *ResultInst;
268
269
270 struct ResultOperand {
Chris Lattner98c870f2010-11-06 19:25:43 +0000271 private:
Chris Lattner225549f2010-11-06 06:39:47 +0000272 StringRef Name;
273 Record *R;
274
Chris Lattner98c870f2010-11-06 19:25:43 +0000275 int64_t Imm;
276 public:
277 enum {
278 K_Record,
Chris Lattner90fd7972010-11-06 19:57:21 +0000279 K_Imm,
280 K_Reg
Chris Lattner98c870f2010-11-06 19:25:43 +0000281 } Kind;
282
283 ResultOperand(StringRef N, Record *r) : Name(N), R(r), Kind(K_Record) {}
284 ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
Chris Lattner90fd7972010-11-06 19:57:21 +0000285 ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
Chris Lattner98c870f2010-11-06 19:25:43 +0000286
287 bool isRecord() const { return Kind == K_Record; }
288 bool isImm() const { return Kind == K_Imm; }
Chris Lattner90fd7972010-11-06 19:57:21 +0000289 bool isReg() const { return Kind == K_Reg; }
Chris Lattner98c870f2010-11-06 19:25:43 +0000290
291 StringRef getName() const { assert(isRecord()); return Name; }
292 Record *getRecord() const { assert(isRecord()); return R; }
293 int64_t getImm() const { assert(isImm()); return Imm; }
Chris Lattner90fd7972010-11-06 19:57:21 +0000294 Record *getRegister() const { assert(isReg()); return R; }
Chris Lattner225549f2010-11-06 06:39:47 +0000295 };
296
297 /// ResultOperands - The decoded operands for the result instruction.
298 std::vector<ResultOperand> ResultOperands;
299
300 CodeGenInstAlias(Record *R, CodeGenTarget &T);
Chris Lattner5bde7342010-11-06 08:20:59 +0000301
302 /// getResultInstOperandIndexForResultOperandIndex - Given an index into the
303 /// ResultOperands array, translate it to a valid index in ResultInst's
304 /// operand list.
305 unsigned getResultInstOperandIndexForResultOperandIndex(unsigned i) const;
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000306 };
307}
Chris Lattnerec352402004-08-01 05:04:00 +0000308
309#endif