1 //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a wrapper class for the 'Instruction' TableGen class.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_INSTRUCTION_H
15 #define CODEGEN_INSTRUCTION_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/CodeGen/ValueTypes.h"
19 #include "llvm/Support/SourceMgr.h"
30 class CGIOperandList {
32 class ConstraintInfo {
33 enum { None, EarlyClobber, Tied } Kind;
34 unsigned OtherTiedOperand;
36 ConstraintInfo() : Kind(None) {}
38 static ConstraintInfo getEarlyClobber() {
40 I.Kind = EarlyClobber;
41 I.OtherTiedOperand = 0;
45 static ConstraintInfo getTied(unsigned Op) {
48 I.OtherTiedOperand = Op;
52 bool isNone() const { return Kind == None; }
53 bool isEarlyClobber() const { return Kind == EarlyClobber; }
54 bool isTied() const { return Kind == Tied; }
56 unsigned getTiedOperand() const {
58 return OtherTiedOperand;
62 /// OperandInfo - The information we keep track of for each operand in the
63 /// operand list for a tablegen instruction.
65 /// Rec - The definition this operand is declared as.
69 /// Name - If this operand was assigned a symbolic name, this is it,
70 /// otherwise, it's empty.
73 /// PrinterMethodName - The method used to print operands of this type in
75 std::string PrinterMethodName;
77 /// EncoderMethodName - The method used to get the machine operand value
78 /// for binary encoding. "getMachineOpValue" by default.
79 std::string EncoderMethodName;
81 /// OperandType - A value from MCOI::OperandType representing the type of
83 std::string OperandType;
85 /// 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.
92 unsigned MINumOperands; // The number of operands.
94 /// 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
97 std::vector<bool> DoNotEncode;
99 /// MIOperandInfo - Default MI operand type. Note an operand may be made
100 /// up of multiple MI operands.
101 DagInit *MIOperandInfo;
103 /// Constraint info for this operand. This operand can have pieces, so we
104 /// track constraint info for each.
105 std::vector<ConstraintInfo> Constraints;
107 OperandInfo(Record *R, const std::string &N, const std::string &PMN,
108 const std::string &EMN, const std::string &OT, unsigned MION,
109 unsigned MINO, DagInit *MIOI)
110 : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
111 OperandType(OT), MIOperandNo(MION), MINumOperands(MINO),
112 MIOperandInfo(MIOI) {}
115 /// 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();
126 CGIOperandList(Record *D);
128 Record *TheDef; // The actual record containing this OperandList.
130 /// NumDefs - Number of def operands declared, this is the number of
131 /// elements in the instruction's (outs) list.
135 /// OperandList - The list of declared operands, along with their declared
136 /// type (which is a record).
137 std::vector<OperandInfo> OperandList;
139 // Information gleaned from the operand list.
144 // Provide transparent accessors to the operand list.
145 bool empty() const { return OperandList.empty(); }
146 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(); }
153 /// getOperandNamed - Return the index of the operand with the specified
154 /// non-empty name. If the instruction does not have an operand with the
155 /// specified name, abort.
156 unsigned getOperandNamed(StringRef Name) const;
158 /// hasOperandNamed - Query whether the instruction has an operand of the
159 /// given name. If so, return true and set OpIdx to the index of the
160 /// operand. Otherwise, return false.
161 bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
163 /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
164 /// where $foo is a whole operand and $foo.bar refers to a suboperand.
165 /// This aborts if the name is invalid. If AllowWholeOp is true, references
166 /// to operands with suboperands are allowed, otherwise not.
167 std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
168 bool AllowWholeOp = true);
170 /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
171 /// flat machineinstr operand #.
172 unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
173 return OperandList[Op.first].MIOperandNo + Op.second;
176 /// getSubOperandNumber - Unflatten a operand number into an
177 /// operand/suboperand pair.
178 std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
179 for (unsigned i = 0; ; ++i) {
180 assert(i < OperandList.size() && "Invalid flat operand #");
181 if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
182 return std::make_pair(i, Op-OperandList[i].MIOperandNo);
187 /// isFlatOperandNotEmitted - Return true if the specified flat operand #
188 /// should not be emitted with the code emitter.
189 bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
190 std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
191 if (OperandList[Op.first].DoNotEncode.size() > Op.second)
192 return OperandList[Op.first].DoNotEncode[Op.second];
196 void ProcessDisableEncoding(std::string Value);
200 class CodeGenInstruction {
202 Record *TheDef; // The actual record defining this instruction.
203 std::string Namespace; // The namespace the instruction is in.
205 /// AsmString - The format string used to emit a .s file for the
207 std::string AsmString;
209 /// Operands - This is information about the (ins) and (outs) list specified
210 /// to the instruction.
211 CGIOperandList Operands;
213 /// ImplicitDefs/ImplicitUses - These are lists of registers that are
214 /// implicitly defined and used by the instruction.
215 std::vector<Record*> ImplicitDefs, ImplicitUses;
217 // Various boolean values we track for the instruction.
220 bool isIndirectBranch;
233 bool isConvertibleToThreeAddress;
236 bool isReMaterializable;
238 bool usesCustomInserter;
239 bool hasPostISelHook;
241 bool isNotDuplicable;
243 bool hasSideEffects_Unset;
244 bool neverHasSideEffects;
245 bool isAsCheapAsAMove;
246 bool hasExtraSrcRegAllocReq;
247 bool hasExtraDefRegAllocReq;
251 /// Are there any undefined flags?
252 bool hasUndefFlags() const {
253 return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;
256 // The record used to infer instruction flags, or NULL if no flag values
257 // have been inferred.
258 Record *InferredFrom;
260 CodeGenInstruction(Record *R);
262 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
263 /// implicit def and it has a known VT, return the VT, otherwise return
266 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
269 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
270 /// include text from the specified variant, returning the new string.
271 static std::string FlattenAsmStringVariants(StringRef AsmString,
276 /// CodeGenInstAlias - This represents an InstAlias definition.
277 class CodeGenInstAlias {
279 Record *TheDef; // The actual record defining this InstAlias.
281 /// AsmString - The format string used to emit a .s file for the
283 std::string AsmString;
285 /// Result - The result instruction.
288 /// ResultInst - The instruction generated by the alias (decoded from
290 CodeGenInstruction *ResultInst;
293 struct ResultOperand {
306 ResultOperand(std::string N, Record *r) : Name(N), R(r), Kind(K_Record) {}
307 ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
308 ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
310 bool isRecord() const { return Kind == K_Record; }
311 bool isImm() const { return Kind == K_Imm; }
312 bool isReg() const { return Kind == K_Reg; }
314 StringRef getName() const { assert(isRecord()); return Name; }
315 Record *getRecord() const { assert(isRecord()); return R; }
316 int64_t getImm() const { assert(isImm()); return Imm; }
317 Record *getRegister() const { assert(isReg()); return R; }
320 /// ResultOperands - The decoded operands for the result instruction.
321 std::vector<ResultOperand> ResultOperands;
323 /// ResultInstOperandIndex - For each operand, this vector holds a pair of
324 /// indices to identify the corresponding operand in the result
325 /// instruction. The first index specifies the operand and the second
326 /// index specifies the suboperand. If there are no suboperands or if all
327 /// of them are matched by the operand, the second value should be -1.
328 std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
330 CodeGenInstAlias(Record *R, CodeGenTarget &T);
332 bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
333 Record *InstOpRec, bool hasSubOps, ArrayRef<SMLoc> Loc,
334 CodeGenTarget &T, ResultOperand &ResOp);