blob: 24bdbe2794b064bb9740c2a1b23d4b828036e6fe [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 Lattnerec352402004-08-01 05:04:00 +000018#include <string>
19#include <vector>
20#include <utility>
21
22namespace llvm {
23 class Record;
Chris Lattner65303d62005-11-19 07:05:57 +000024 class DagInit;
Chris Lattner9414ae52010-03-27 20:09:24 +000025 class CodeGenTarget;
Chris Lattner4d43d0f2010-11-01 01:07:14 +000026 class StringRef;
Chris Lattnerc240bb02010-11-01 04:03:32 +000027
28 class CGIOperandList {
Jeff Cohend41b30d2006-11-05 19:31:28 +000029 public:
Chris Lattnera7d479c2010-02-10 01:45:28 +000030 class ConstraintInfo {
31 enum { None, EarlyClobber, Tied } Kind;
32 unsigned OtherTiedOperand;
33 public:
34 ConstraintInfo() : Kind(None) {}
Chris Lattnerc240bb02010-11-01 04:03:32 +000035
Chris Lattnera7d479c2010-02-10 01:45:28 +000036 static ConstraintInfo getEarlyClobber() {
37 ConstraintInfo I;
38 I.Kind = EarlyClobber;
Chris Lattnere555c9f2010-02-10 21:22:51 +000039 I.OtherTiedOperand = 0;
Chris Lattnera7d479c2010-02-10 01:45:28 +000040 return I;
41 }
Chris Lattnerc240bb02010-11-01 04:03:32 +000042
Chris Lattnera7d479c2010-02-10 01:45:28 +000043 static ConstraintInfo getTied(unsigned Op) {
44 ConstraintInfo I;
45 I.Kind = Tied;
46 I.OtherTiedOperand = Op;
47 return I;
48 }
Chris Lattnerc240bb02010-11-01 04:03:32 +000049
Chris Lattnera7d479c2010-02-10 01:45:28 +000050 bool isNone() const { return Kind == None; }
51 bool isEarlyClobber() const { return Kind == EarlyClobber; }
52 bool isTied() const { return Kind == Tied; }
Chris Lattnerc240bb02010-11-01 04:03:32 +000053
Chris Lattnera7d479c2010-02-10 01:45:28 +000054 unsigned getTiedOperand() const {
55 assert(isTied());
56 return OtherTiedOperand;
57 }
58 };
Jim Grosbach9ed2cee2010-10-08 18:09:59 +000059
Chris Lattnercf03da02004-08-11 02:22:39 +000060 /// OperandInfo - The information we keep track of for each operand in the
61 /// operand list for a tablegen instruction.
Chris Lattner87c59052004-08-01 07:42:39 +000062 struct OperandInfo {
Chris Lattnercf03da02004-08-11 02:22:39 +000063 /// Rec - The definition this operand is declared as.
Chris Lattner0e384b62005-08-19 16:57:28 +000064 ///
Chris Lattner87c59052004-08-01 07:42:39 +000065 Record *Rec;
Chris Lattnerc240bb02010-11-01 04:03:32 +000066
Chris Lattnercf03da02004-08-11 02:22:39 +000067 /// Name - If this operand was assigned a symbolic name, this is it,
68 /// otherwise, it's empty.
Chris Lattner87c59052004-08-01 07:42:39 +000069 std::string Name;
Chris Lattnerc240bb02010-11-01 04:03:32 +000070
Chris Lattnercf03da02004-08-11 02:22:39 +000071 /// PrinterMethodName - The method used to print operands of this type in
72 /// the asmprinter.
73 std::string PrinterMethodName;
Chris Lattnerc240bb02010-11-01 04:03:32 +000074
Jim Grosbach5013f742010-10-12 22:21:57 +000075 /// EncoderMethodName - The method used to get the machine operand value
76 /// for binary encoding. "getMachineOpValue" by default.
77 std::string EncoderMethodName;
Chris Lattnerc240bb02010-11-01 04:03:32 +000078
Chris Lattnercf03da02004-08-11 02:22:39 +000079 /// MIOperandNo - Currently (this is meant to be phased out), some logical
80 /// operands correspond to multiple MachineInstr operands. In the X86
81 /// target for example, one address operand is represented as 4
82 /// MachineOperands. Because of this, the operand number in the
83 /// OperandList may not match the MachineInstr operand num. Until it
84 /// does, this contains the MI operand index of this operand.
85 unsigned MIOperandNo;
Chris Lattnercfbf96a2005-08-18 23:38:41 +000086 unsigned MINumOperands; // The number of operands.
Chris Lattnerc240bb02010-11-01 04:03:32 +000087
Chris Lattnerf64f9a42006-11-15 23:23:02 +000088 /// DoNotEncode - Bools are set to true in this vector for each operand in
89 /// the DisableEncoding list. These should not be emitted by the code
90 /// emitter.
91 std::vector<bool> DoNotEncode;
Chris Lattnerc240bb02010-11-01 04:03:32 +000092
Nate Begeman8ef9d162005-11-30 23:58:18 +000093 /// MIOperandInfo - Default MI operand type. Note an operand may be made
94 /// up of multiple MI operands.
Chris Lattner65303d62005-11-19 07:05:57 +000095 DagInit *MIOperandInfo;
Chris Lattnerc240bb02010-11-01 04:03:32 +000096
Chris Lattner0bb75002006-11-15 02:38:17 +000097 /// Constraint info for this operand. This operand can have pieces, so we
98 /// track constraint info for each.
Chris Lattnera7d479c2010-02-10 01:45:28 +000099 std::vector<ConstraintInfo> Constraints;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000100
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000101 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
Jim Grosbach5013f742010-10-12 22:21:57 +0000102 const std::string &EMN, unsigned MION, unsigned MINO,
103 DagInit *MIOI)
Chris Lattnerc240bb02010-11-01 04:03:32 +0000104 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
Chris Lattner9b0d4bf2010-11-02 22:55:03 +0000105 MIOperandNo(MION), MINumOperands(MINO), MIOperandInfo(MIOI) {}
106
107
108 /// getTiedOperand - If this operand is tied to another one, return the
109 /// other operand number. Otherwise, return -1.
110 int getTiedRegister() const {
111 for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
112 const CGIOperandList::ConstraintInfo &CI = Constraints[j];
113 if (CI.isTied()) return CI.getTiedOperand();
114 }
115 return -1;
116 }
Chris Lattner87c59052004-08-01 07:42:39 +0000117 };
Chris Lattnerc240bb02010-11-01 04:03:32 +0000118
119 CGIOperandList(Record *D);
120
121 Record *TheDef; // The actual record containing this OperandList.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000122
Chris Lattnercedef1c2010-03-18 20:50:52 +0000123 /// NumDefs - Number of def operands declared, this is the number of
124 /// elements in the instruction's (outs) list.
Evan Cheng64d80e32007-07-19 01:14:50 +0000125 ///
126 unsigned NumDefs;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000127
Chris Lattnerec352402004-08-01 05:04:00 +0000128 /// OperandList - The list of declared operands, along with their declared
129 /// type (which is a record).
Chris Lattner87c59052004-08-01 07:42:39 +0000130 std::vector<OperandInfo> OperandList;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000131
132 // Information gleaned from the operand list.
133 bool isPredicable;
134 bool hasOptionalDef;
135 bool isVariadic;
136
137 // Provide transparent accessors to the operand list.
138 unsigned size() const { return OperandList.size(); }
139 const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
140 OperandInfo &operator[](unsigned i) { return OperandList[i]; }
141 OperandInfo &back() { return OperandList.back(); }
142 const OperandInfo &back() const { return OperandList.back(); }
143
144
145 /// getOperandNamed - Return the index of the operand with the specified
146 /// non-empty name. If the instruction does not have an operand with the
147 /// specified name, throw an exception.
148 unsigned getOperandNamed(StringRef Name) const;
149
150 /// hasOperandNamed - Query whether the instruction has an operand of the
151 /// given name. If so, return true and set OpIdx to the index of the
152 /// operand. Otherwise, return false.
153 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
154
155 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
156 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
157 /// This throws an exception if the name is invalid. If AllowWholeOp is
158 /// true, references to operands with suboperands are allowed, otherwise
159 /// not.
160 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
161 bool AllowWholeOp = true);
162
163 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
164 /// flat machineinstr operand #.
165 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
166 return OperandList[Op.first].MIOperandNo + Op.second;
167 }
168
169 /// getSubOperandNumber - Unflatten a operand number into an
170 /// operand/suboperand pair.
171 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
172 for (unsigned i = 0; ; ++i) {
173 assert(i < OperandList.size() && "Invalid flat operand #");
174 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
175 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
176 }
177 }
178
179
180 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
181 /// should not be emitted with the code emitter.
182 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
183 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
184 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
185 return OperandList[Op.first].DoNotEncode[Op.second];
186 return false;
187 }
188
189 void ProcessDisableEncoding(std::string Value);
190 };
191
192
193 class CodeGenInstruction {
194 public:
195 Record *TheDef; // The actual record defining this instruction.
196 std::string Namespace; // The namespace the instruction is in.
197
198 /// AsmString - The format string used to emit a .s file for the
199 /// instruction.
200 std::string AsmString;
201
202 /// Operands - This is information about the (ins) and (outs) list specified
203 /// to the instruction.
204 CGIOperandList Operands;
Chris Lattnerec352402004-08-01 05:04:00 +0000205
Chris Lattnerf506b6b2010-03-18 21:42:03 +0000206 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
207 /// implicitly defined and used by the instruction.
208 std::vector<Record*> ImplicitDefs, ImplicitUses;
209
Chris Lattnerec352402004-08-01 05:04:00 +0000210 // Various boolean values we track for the instruction.
211 bool isReturn;
212 bool isBranch;
Owen Anderson20ab2902007-11-12 07:39:39 +0000213 bool isIndirectBranch;
Bill Wendling73739d02010-08-08 01:49:35 +0000214 bool isCompare;
Chris Lattnerec352402004-08-01 05:04:00 +0000215 bool isBarrier;
216 bool isCall;
Dan Gohman15511cf2008-12-03 18:15:48 +0000217 bool canFoldAsLoad;
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000218 bool mayLoad, mayStore;
Evan Cheng5127ce02007-05-16 20:45:24 +0000219 bool isPredicable;
Chris Lattneraad75aa2005-01-02 02:29:04 +0000220 bool isConvertibleToThreeAddress;
221 bool isCommutable;
Chris Lattnerec352402004-08-01 05:04:00 +0000222 bool isTerminator;
Dan Gohmand45eddd2007-06-26 00:48:07 +0000223 bool isReMaterializable;
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000224 bool hasDelaySlot;
Dan Gohman533297b2009-10-29 18:10:34 +0000225 bool usesCustomInserter;
Evan Cheng1c3d19e2005-12-04 08:18:16 +0000226 bool hasCtrlDep;
Evan Chengeaa91b02007-06-19 01:26:51 +0000227 bool isNotDuplicable;
Bill Wendling8370d382008-05-28 22:54:52 +0000228 bool hasSideEffects;
Bill Wendling8370d382008-05-28 22:54:52 +0000229 bool neverHasSideEffects;
230 bool isAsCheapAsAMove;
Evan Cheng799d6972009-10-01 08:21:18 +0000231 bool hasExtraSrcRegAllocReq;
232 bool hasExtraDefRegAllocReq;
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000233
Chris Lattnerec352402004-08-01 05:04:00 +0000234
Chris Lattnerf7808112010-11-01 02:15:23 +0000235 CodeGenInstruction(Record *R);
Chris Lattner87c59052004-08-01 07:42:39 +0000236
Chris Lattner9414ae52010-03-27 20:09:24 +0000237 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
238 /// implicit def and it has a known VT, return the VT, otherwise return
239 /// MVT::Other.
Jim Grosbach9ed2cee2010-10-08 18:09:59 +0000240 MVT::SimpleValueType
Chris Lattner9414ae52010-03-27 20:09:24 +0000241 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000242
243
244 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
245 /// include text from the specified variant, returning the new string.
246 static std::string FlattenAsmStringVariants(StringRef AsmString,
247 unsigned Variant);
Chris Lattnerec352402004-08-01 05:04:00 +0000248 };
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000249
250
251 /// CodeGenInstAlias - This represents an InstAlias definition.
252 class CodeGenInstAlias {
253 public:
254 Record *TheDef; // The actual record defining this InstAlias.
255
256 /// AsmString - The format string used to emit a .s file for the
257 /// instruction.
258 std::string AsmString;
259
260 /// Operands - This is information about the (ins) and (outs) list specified
261 /// to the alias.
262 CGIOperandList Operands;
263
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000264 /// Result - The result instruction.
265 DagInit *Result;
266
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000267 CodeGenInstAlias(Record *R);
268 };
269}
Chris Lattnerec352402004-08-01 05:04:00 +0000270
271#endif