• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a wrapper class for the 'Instruction' TableGen class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
15 #define LLVM_UTILS_TABLEGEN_CODEGENINSTRUCTION_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/MachineValueType.h"
19 #include "llvm/Support/SMLoc.h"
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 namespace llvm {
25 template <typename T> class ArrayRef;
26   class Record;
27   class DagInit;
28   class CodeGenTarget;
29 
30   class CGIOperandList {
31   public:
32     class ConstraintInfo {
33       enum { None, EarlyClobber, Tied } Kind;
34       unsigned OtherTiedOperand;
35     public:
ConstraintInfo()36       ConstraintInfo() : Kind(None) {}
37 
getEarlyClobber()38       static ConstraintInfo getEarlyClobber() {
39         ConstraintInfo I;
40         I.Kind = EarlyClobber;
41         I.OtherTiedOperand = 0;
42         return I;
43       }
44 
getTied(unsigned Op)45       static ConstraintInfo getTied(unsigned Op) {
46         ConstraintInfo I;
47         I.Kind = Tied;
48         I.OtherTiedOperand = Op;
49         return I;
50       }
51 
isNone()52       bool isNone() const { return Kind == None; }
isEarlyClobber()53       bool isEarlyClobber() const { return Kind == EarlyClobber; }
isTied()54       bool isTied() const { return Kind == Tied; }
55 
getTiedOperand()56       unsigned getTiedOperand() const {
57         assert(isTied());
58         return OtherTiedOperand;
59       }
60     };
61 
62     /// OperandInfo - The information we keep track of for each operand in the
63     /// operand list for a tablegen instruction.
64     struct OperandInfo {
65       /// Rec - The definition this operand is declared as.
66       ///
67       Record *Rec;
68 
69       /// Name - If this operand was assigned a symbolic name, this is it,
70       /// otherwise, it's empty.
71       std::string Name;
72 
73       /// PrinterMethodName - The method used to print operands of this type in
74       /// the asmprinter.
75       std::string PrinterMethodName;
76 
77       /// EncoderMethodName - The method used to get the machine operand value
78       /// for binary encoding. "getMachineOpValue" by default.
79       std::string EncoderMethodName;
80 
81       /// OperandType - A value from MCOI::OperandType representing the type of
82       /// the operand.
83       std::string OperandType;
84 
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.
91       unsigned MIOperandNo;
92       unsigned MINumOperands;   // The number of operands.
93 
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
96       /// emitter.
97       std::vector<bool> DoNotEncode;
98 
99       /// MIOperandInfo - Default MI operand type. Note an operand may be made
100       /// up of multiple MI operands.
101       DagInit *MIOperandInfo;
102 
103       /// Constraint info for this operand.  This operand can have pieces, so we
104       /// track constraint info for each.
105       std::vector<ConstraintInfo> Constraints;
106 
OperandInfoOperandInfo107       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) {}
113 
114 
115       /// getTiedOperand - If this operand is tied to another one, return the
116       /// other operand number.  Otherwise, return -1.
getTiedRegisterOperandInfo117       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       }
124     };
125 
126     CGIOperandList(Record *D);
127 
128     Record *TheDef;            // The actual record containing this OperandList.
129 
130     /// NumDefs - Number of def operands declared, this is the number of
131     /// elements in the instruction's (outs) list.
132     ///
133     unsigned NumDefs;
134 
135     /// OperandList - The list of declared operands, along with their declared
136     /// type (which is a record).
137     std::vector<OperandInfo> OperandList;
138 
139     // Information gleaned from the operand list.
140     bool isPredicable;
141     bool hasOptionalDef;
142     bool isVariadic;
143 
144     // Provide transparent accessors to the operand list.
empty()145     bool empty() const { return OperandList.empty(); }
size()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]; }
back()149     OperandInfo &back() { return OperandList.back(); }
back()150     const OperandInfo &back() const { return OperandList.back(); }
151 
152     typedef std::vector<OperandInfo>::iterator iterator;
153     typedef std::vector<OperandInfo>::const_iterator const_iterator;
begin()154     iterator begin() { return OperandList.begin(); }
begin()155     const_iterator begin() const { return OperandList.begin(); }
end()156     iterator end() { return OperandList.end(); }
end()157     const_iterator end() const { return OperandList.end(); }
158 
159     /// getOperandNamed - Return the index of the operand with the specified
160     /// non-empty name.  If the instruction does not have an operand with the
161     /// specified name, abort.
162     unsigned getOperandNamed(StringRef Name) const;
163 
164     /// 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;
168 
169     /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
170     /// where $foo is a whole operand and $foo.bar refers to a suboperand.
171     /// This aborts if the name is invalid.  If AllowWholeOp is true, references
172     /// to operands with suboperands are allowed, otherwise not.
173     std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
174                                                   bool AllowWholeOp = true);
175 
176     /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
177     /// flat machineinstr operand #.
getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op)178     unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
179       return OperandList[Op.first].MIOperandNo + Op.second;
180     }
181 
182     /// getSubOperandNumber - Unflatten a operand number into an
183     /// operand/suboperand pair.
getSubOperandNumber(unsigned Op)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     }
191 
192 
193     /// isFlatOperandNotEmitted - Return true if the specified flat operand #
194     /// should not be emitted with the code emitter.
isFlatOperandNotEmitted(unsigned FlatOpNo)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     }
201 
202     void ProcessDisableEncoding(std::string Value);
203   };
204 
205 
206   class CodeGenInstruction {
207   public:
208     Record *TheDef;            // The actual record defining this instruction.
209     StringRef 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;
218 
219     /// ImplicitDefs/ImplicitUses - These are lists of registers that are
220     /// implicitly defined and used by the instruction.
221     std::vector<Record*> ImplicitDefs, ImplicitUses;
222 
223     // Various boolean values we track for the instruction.
224     bool isReturn : 1;
225     bool isBranch : 1;
226     bool isIndirectBranch : 1;
227     bool isCompare : 1;
228     bool isMoveImm : 1;
229     bool isMoveReg : 1;
230     bool isBitcast : 1;
231     bool isSelect : 1;
232     bool isBarrier : 1;
233     bool isCall : 1;
234     bool isAdd : 1;
235     bool isTrap : 1;
236     bool canFoldAsLoad : 1;
237     bool mayLoad : 1;
238     bool mayLoad_Unset : 1;
239     bool mayStore : 1;
240     bool mayStore_Unset : 1;
241     bool isPredicable : 1;
242     bool isConvertibleToThreeAddress : 1;
243     bool isCommutable : 1;
244     bool isTerminator : 1;
245     bool isReMaterializable : 1;
246     bool hasDelaySlot : 1;
247     bool usesCustomInserter : 1;
248     bool hasPostISelHook : 1;
249     bool hasCtrlDep : 1;
250     bool isNotDuplicable : 1;
251     bool hasSideEffects : 1;
252     bool hasSideEffects_Unset : 1;
253     bool isAsCheapAsAMove : 1;
254     bool hasExtraSrcRegAllocReq : 1;
255     bool hasExtraDefRegAllocReq : 1;
256     bool isCodeGenOnly : 1;
257     bool isPseudo : 1;
258     bool isRegSequence : 1;
259     bool isExtractSubreg : 1;
260     bool isInsertSubreg : 1;
261     bool isConvergent : 1;
262     bool hasNoSchedulingInfo : 1;
263     bool FastISelShouldIgnore : 1;
264     bool hasChain : 1;
265     bool hasChain_Inferred : 1;
266 
267     std::string DeprecatedReason;
268     bool HasComplexDeprecationPredicate;
269 
270     /// Are there any undefined flags?
hasUndefFlags()271     bool hasUndefFlags() const {
272       return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;
273     }
274 
275     // The record used to infer instruction flags, or NULL if no flag values
276     // have been inferred.
277     Record *InferredFrom;
278 
279     CodeGenInstruction(Record *R);
280 
281     /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
282     /// implicit def and it has a known VT, return the VT, otherwise return
283     /// MVT::Other.
284     MVT::SimpleValueType
285       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
286 
287 
288     /// FlattenAsmStringVariants - Flatten the specified AsmString to only
289     /// include text from the specified variant, returning the new string.
290     static std::string FlattenAsmStringVariants(StringRef AsmString,
291                                                 unsigned Variant);
292 
293     // Is the specified operand in a generic instruction implicitly a pointer.
294     // This can be used on intructions that use typeN or ptypeN to identify
295     // operands that should be considered as pointers even though SelectionDAG
296     // didn't make a distinction between integer and pointers.
297     bool isOperandAPointer(unsigned i) const;
298   };
299 
300 
301   /// CodeGenInstAlias - This represents an InstAlias definition.
302   class CodeGenInstAlias {
303   public:
304     Record *TheDef;            // The actual record defining this InstAlias.
305 
306     /// AsmString - The format string used to emit a .s file for the
307     /// instruction.
308     std::string AsmString;
309 
310     /// Result - The result instruction.
311     DagInit *Result;
312 
313     /// ResultInst - The instruction generated by the alias (decoded from
314     /// Result).
315     CodeGenInstruction *ResultInst;
316 
317 
318     struct ResultOperand {
319     private:
320       std::string Name;
321       Record *R;
322 
323       int64_t Imm;
324     public:
325       enum {
326         K_Record,
327         K_Imm,
328         K_Reg
329       } Kind;
330 
ResultOperandResultOperand331       ResultOperand(std::string N, Record *r)
332           : Name(std::move(N)), R(r), Kind(K_Record) {}
ResultOperandResultOperand333       ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
ResultOperandResultOperand334       ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
335 
isRecordResultOperand336       bool isRecord() const { return Kind == K_Record; }
isImmResultOperand337       bool isImm() const { return Kind == K_Imm; }
isRegResultOperand338       bool isReg() const { return Kind == K_Reg; }
339 
getNameResultOperand340       StringRef getName() const { assert(isRecord()); return Name; }
getRecordResultOperand341       Record *getRecord() const { assert(isRecord()); return R; }
getImmResultOperand342       int64_t getImm() const { assert(isImm()); return Imm; }
getRegisterResultOperand343       Record *getRegister() const { assert(isReg()); return R; }
344 
345       unsigned getMINumOperands() const;
346     };
347 
348     /// ResultOperands - The decoded operands for the result instruction.
349     std::vector<ResultOperand> ResultOperands;
350 
351     /// ResultInstOperandIndex - For each operand, this vector holds a pair of
352     /// indices to identify the corresponding operand in the result
353     /// instruction.  The first index specifies the operand and the second
354     /// index specifies the suboperand.  If there are no suboperands or if all
355     /// of them are matched by the operand, the second value should be -1.
356     std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
357 
358     CodeGenInstAlias(Record *R, CodeGenTarget &T);
359 
360     bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
361                          Record *InstOpRec, bool hasSubOps, ArrayRef<SMLoc> Loc,
362                          CodeGenTarget &T, ResultOperand &ResOp);
363   };
364 }
365 
366 #endif
367