1193323Sed//===- CodeGenInstruction.h - Instruction Class Wrapper ---------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines a wrapper class for the 'Instruction' TableGen class.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef CODEGEN_INSTRUCTION_H
15193323Sed#define CODEGEN_INSTRUCTION_H
16193323Sed
17249423Sdim#include "llvm/ADT/StringRef.h"
18193323Sed#include "llvm/CodeGen/ValueTypes.h"
19218893Sdim#include "llvm/Support/SourceMgr.h"
20193323Sed#include <string>
21249423Sdim#include <utility>
22193323Sed#include <vector>
23193323Sed
24193323Sednamespace llvm {
25193323Sed  class Record;
26193323Sed  class DagInit;
27206083Srdivacky  class CodeGenTarget;
28218893Sdim  class StringRef;
29221345Sdim
30218893Sdim  class CGIOperandList {
31193323Sed  public:
32203954Srdivacky    class ConstraintInfo {
33203954Srdivacky      enum { None, EarlyClobber, Tied } Kind;
34203954Srdivacky      unsigned OtherTiedOperand;
35203954Srdivacky    public:
36203954Srdivacky      ConstraintInfo() : Kind(None) {}
37221345Sdim
38203954Srdivacky      static ConstraintInfo getEarlyClobber() {
39203954Srdivacky        ConstraintInfo I;
40203954Srdivacky        I.Kind = EarlyClobber;
41203954Srdivacky        I.OtherTiedOperand = 0;
42203954Srdivacky        return I;
43203954Srdivacky      }
44221345Sdim
45203954Srdivacky      static ConstraintInfo getTied(unsigned Op) {
46203954Srdivacky        ConstraintInfo I;
47203954Srdivacky        I.Kind = Tied;
48203954Srdivacky        I.OtherTiedOperand = Op;
49203954Srdivacky        return I;
50203954Srdivacky      }
51221345Sdim
52203954Srdivacky      bool isNone() const { return Kind == None; }
53203954Srdivacky      bool isEarlyClobber() const { return Kind == EarlyClobber; }
54203954Srdivacky      bool isTied() const { return Kind == Tied; }
55221345Sdim
56203954Srdivacky      unsigned getTiedOperand() const {
57203954Srdivacky        assert(isTied());
58203954Srdivacky        return OtherTiedOperand;
59203954Srdivacky      }
60203954Srdivacky    };
61218893Sdim
62193323Sed    /// OperandInfo - The information we keep track of for each operand in the
63193323Sed    /// operand list for a tablegen instruction.
64193323Sed    struct OperandInfo {
65193323Sed      /// Rec - The definition this operand is declared as.
66193323Sed      ///
67193323Sed      Record *Rec;
68221345Sdim
69193323Sed      /// Name - If this operand was assigned a symbolic name, this is it,
70193323Sed      /// otherwise, it's empty.
71193323Sed      std::string Name;
72221345Sdim
73193323Sed      /// PrinterMethodName - The method used to print operands of this type in
74193323Sed      /// the asmprinter.
75193323Sed      std::string PrinterMethodName;
76221345Sdim
77218893Sdim      /// EncoderMethodName - The method used to get the machine operand value
78218893Sdim      /// for binary encoding. "getMachineOpValue" by default.
79218893Sdim      std::string EncoderMethodName;
80221345Sdim
81224145Sdim      /// OperandType - A value from MCOI::OperandType representing the type of
82224145Sdim      /// the operand.
83224145Sdim      std::string OperandType;
84224145Sdim
85193323Sed      /// MIOperandNo - Currently (this is meant to be phased out), some logical
86193323Sed      /// operands correspond to multiple MachineInstr operands.  In the X86
87193323Sed      /// target for example, one address operand is represented as 4
88193323Sed      /// MachineOperands.  Because of this, the operand number in the
89193323Sed      /// OperandList may not match the MachineInstr operand num.  Until it
90193323Sed      /// does, this contains the MI operand index of this operand.
91193323Sed      unsigned MIOperandNo;
92193323Sed      unsigned MINumOperands;   // The number of operands.
93221345Sdim
94193323Sed      /// DoNotEncode - Bools are set to true in this vector for each operand in
95193323Sed      /// the DisableEncoding list.  These should not be emitted by the code
96193323Sed      /// emitter.
97193323Sed      std::vector<bool> DoNotEncode;
98221345Sdim
99193323Sed      /// MIOperandInfo - Default MI operand type. Note an operand may be made
100193323Sed      /// up of multiple MI operands.
101193323Sed      DagInit *MIOperandInfo;
102221345Sdim
103193323Sed      /// Constraint info for this operand.  This operand can have pieces, so we
104193323Sed      /// track constraint info for each.
105203954Srdivacky      std::vector<ConstraintInfo> Constraints;
106221345Sdim
107218893Sdim      OperandInfo(Record *R, const std::string &N, const std::string &PMN,
108224145Sdim                  const std::string &EMN, const std::string &OT, unsigned MION,
109224145Sdim                  unsigned MINO, DagInit *MIOI)
110218893Sdim      : Rec(R), Name(N), PrinterMethodName(PMN), EncoderMethodName(EMN),
111224145Sdim        OperandType(OT), MIOperandNo(MION), MINumOperands(MINO),
112224145Sdim        MIOperandInfo(MIOI) {}
113221345Sdim
114221345Sdim
115218893Sdim      /// getTiedOperand - If this operand is tied to another one, return the
116218893Sdim      /// other operand number.  Otherwise, return -1.
117218893Sdim      int getTiedRegister() const {
118218893Sdim        for (unsigned j = 0, e = Constraints.size(); j != e; ++j) {
119218893Sdim          const CGIOperandList::ConstraintInfo &CI = Constraints[j];
120218893Sdim          if (CI.isTied()) return CI.getTiedOperand();
121218893Sdim        }
122218893Sdim        return -1;
123218893Sdim      }
124193323Sed    };
125221345Sdim
126218893Sdim    CGIOperandList(Record *D);
127221345Sdim
128218893Sdim    Record *TheDef;            // The actual record containing this OperandList.
129193323Sed
130205407Srdivacky    /// NumDefs - Number of def operands declared, this is the number of
131205407Srdivacky    /// elements in the instruction's (outs) list.
132193323Sed    ///
133193323Sed    unsigned NumDefs;
134221345Sdim
135193323Sed    /// OperandList - The list of declared operands, along with their declared
136193323Sed    /// type (which is a record).
137193323Sed    std::vector<OperandInfo> OperandList;
138221345Sdim
139218893Sdim    // Information gleaned from the operand list.
140193323Sed    bool isPredicable;
141218893Sdim    bool hasOptionalDef;
142193323Sed    bool isVariadic;
143221345Sdim
144218893Sdim    // Provide transparent accessors to the operand list.
145221345Sdim    bool empty() const { return OperandList.empty(); }
146218893Sdim    unsigned size() const { return OperandList.size(); }
147218893Sdim    const OperandInfo &operator[](unsigned i) const { return OperandList[i]; }
148218893Sdim    OperandInfo &operator[](unsigned i) { return OperandList[i]; }
149218893Sdim    OperandInfo &back() { return OperandList.back(); }
150218893Sdim    const OperandInfo &back() const { return OperandList.back(); }
151221345Sdim
152221345Sdim
153218893Sdim    /// getOperandNamed - Return the index of the operand with the specified
154218893Sdim    /// non-empty name.  If the instruction does not have an operand with the
155243830Sdim    /// specified name, abort.
156218893Sdim    unsigned getOperandNamed(StringRef Name) const;
157221345Sdim
158218893Sdim    /// hasOperandNamed - Query whether the instruction has an operand of the
159218893Sdim    /// given name. If so, return true and set OpIdx to the index of the
160218893Sdim    /// operand. Otherwise, return false.
161218893Sdim    bool hasOperandNamed(StringRef Name, unsigned &OpIdx) const;
162221345Sdim
163193323Sed    /// ParseOperandName - Parse an operand name like "$foo" or "$foo.bar",
164193323Sed    /// where $foo is a whole operand and $foo.bar refers to a suboperand.
165243830Sdim    /// This aborts if the name is invalid.  If AllowWholeOp is true, references
166243830Sdim    /// to operands with suboperands are allowed, otherwise not.
167193323Sed    std::pair<unsigned,unsigned> ParseOperandName(const std::string &Op,
168193323Sed                                                  bool AllowWholeOp = true);
169221345Sdim
170193323Sed    /// getFlattenedOperandNumber - Flatten a operand/suboperand pair into a
171193323Sed    /// flat machineinstr operand #.
172193323Sed    unsigned getFlattenedOperandNumber(std::pair<unsigned,unsigned> Op) const {
173193323Sed      return OperandList[Op.first].MIOperandNo + Op.second;
174193323Sed    }
175221345Sdim
176193323Sed    /// getSubOperandNumber - Unflatten a operand number into an
177193323Sed    /// operand/suboperand pair.
178193323Sed    std::pair<unsigned,unsigned> getSubOperandNumber(unsigned Op) const {
179193323Sed      for (unsigned i = 0; ; ++i) {
180193323Sed        assert(i < OperandList.size() && "Invalid flat operand #");
181193323Sed        if (OperandList[i].MIOperandNo+OperandList[i].MINumOperands > Op)
182193323Sed          return std::make_pair(i, Op-OperandList[i].MIOperandNo);
183193323Sed      }
184193323Sed    }
185221345Sdim
186221345Sdim
187193323Sed    /// isFlatOperandNotEmitted - Return true if the specified flat operand #
188193323Sed    /// should not be emitted with the code emitter.
189193323Sed    bool isFlatOperandNotEmitted(unsigned FlatOpNo) const {
190193323Sed      std::pair<unsigned,unsigned> Op = getSubOperandNumber(FlatOpNo);
191193323Sed      if (OperandList[Op.first].DoNotEncode.size() > Op.second)
192193323Sed        return OperandList[Op.first].DoNotEncode[Op.second];
193193323Sed      return false;
194193323Sed    }
195221345Sdim
196218893Sdim    void ProcessDisableEncoding(std::string Value);
197218893Sdim  };
198193323Sed
199221345Sdim
200218893Sdim  class CodeGenInstruction {
201218893Sdim  public:
202218893Sdim    Record *TheDef;            // The actual record defining this instruction.
203218893Sdim    std::string Namespace;     // The namespace the instruction is in.
204193323Sed
205218893Sdim    /// AsmString - The format string used to emit a .s file for the
206218893Sdim    /// instruction.
207218893Sdim    std::string AsmString;
208218893Sdim
209218893Sdim    /// Operands - This is information about the (ins) and (outs) list specified
210218893Sdim    /// to the instruction.
211218893Sdim    CGIOperandList Operands;
212218893Sdim
213218893Sdim    /// ImplicitDefs/ImplicitUses - These are lists of registers that are
214218893Sdim    /// implicitly defined and used by the instruction.
215218893Sdim    std::vector<Record*> ImplicitDefs, ImplicitUses;
216218893Sdim
217218893Sdim    // Various boolean values we track for the instruction.
218218893Sdim    bool isReturn;
219218893Sdim    bool isBranch;
220218893Sdim    bool isIndirectBranch;
221218893Sdim    bool isCompare;
222218893Sdim    bool isMoveImm;
223221345Sdim    bool isBitcast;
224239462Sdim    bool isSelect;
225218893Sdim    bool isBarrier;
226218893Sdim    bool isCall;
227218893Sdim    bool canFoldAsLoad;
228243830Sdim    bool mayLoad;
229243830Sdim    bool mayLoad_Unset;
230243830Sdim    bool mayStore;
231243830Sdim    bool mayStore_Unset;
232218893Sdim    bool isPredicable;
233218893Sdim    bool isConvertibleToThreeAddress;
234218893Sdim    bool isCommutable;
235218893Sdim    bool isTerminator;
236218893Sdim    bool isReMaterializable;
237218893Sdim    bool hasDelaySlot;
238218893Sdim    bool usesCustomInserter;
239226633Sdim    bool hasPostISelHook;
240218893Sdim    bool hasCtrlDep;
241218893Sdim    bool isNotDuplicable;
242218893Sdim    bool hasSideEffects;
243243830Sdim    bool hasSideEffects_Unset;
244218893Sdim    bool neverHasSideEffects;
245218893Sdim    bool isAsCheapAsAMove;
246218893Sdim    bool hasExtraSrcRegAllocReq;
247218893Sdim    bool hasExtraDefRegAllocReq;
248224145Sdim    bool isCodeGenOnly;
249224145Sdim    bool isPseudo;
250218893Sdim
251263508Sdim    std::string DeprecatedReason;
252263508Sdim    bool HasComplexDeprecationPredicate;
253263508Sdim
254243830Sdim    /// Are there any undefined flags?
255243830Sdim    bool hasUndefFlags() const {
256243830Sdim      return mayLoad_Unset || mayStore_Unset || hasSideEffects_Unset;
257243830Sdim    }
258218893Sdim
259243830Sdim    // The record used to infer instruction flags, or NULL if no flag values
260243830Sdim    // have been inferred.
261243830Sdim    Record *InferredFrom;
262243830Sdim
263218893Sdim    CodeGenInstruction(Record *R);
264218893Sdim
265206083Srdivacky    /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
266206083Srdivacky    /// implicit def and it has a known VT, return the VT, otherwise return
267206083Srdivacky    /// MVT::Other.
268218893Sdim    MVT::SimpleValueType
269206083Srdivacky      HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
270221345Sdim
271221345Sdim
272218893Sdim    /// FlattenAsmStringVariants - Flatten the specified AsmString to only
273218893Sdim    /// include text from the specified variant, returning the new string.
274218893Sdim    static std::string FlattenAsmStringVariants(StringRef AsmString,
275218893Sdim                                                unsigned Variant);
276193323Sed  };
277221345Sdim
278221345Sdim
279218893Sdim  /// CodeGenInstAlias - This represents an InstAlias definition.
280218893Sdim  class CodeGenInstAlias {
281218893Sdim  public:
282218893Sdim    Record *TheDef;            // The actual record defining this InstAlias.
283221345Sdim
284218893Sdim    /// AsmString - The format string used to emit a .s file for the
285218893Sdim    /// instruction.
286218893Sdim    std::string AsmString;
287221345Sdim
288218893Sdim    /// Result - The result instruction.
289218893Sdim    DagInit *Result;
290221345Sdim
291218893Sdim    /// ResultInst - The instruction generated by the alias (decoded from
292218893Sdim    /// Result).
293218893Sdim    CodeGenInstruction *ResultInst;
294221345Sdim
295221345Sdim
296218893Sdim    struct ResultOperand {
297218893Sdim    private:
298239462Sdim      std::string Name;
299218893Sdim      Record *R;
300221345Sdim
301218893Sdim      int64_t Imm;
302221345Sdim    public:
303218893Sdim      enum {
304218893Sdim        K_Record,
305218893Sdim        K_Imm,
306218893Sdim        K_Reg
307218893Sdim      } Kind;
308221345Sdim
309239462Sdim      ResultOperand(std::string N, Record *r) : Name(N), R(r), Kind(K_Record) {}
310218893Sdim      ResultOperand(int64_t I) : Imm(I), Kind(K_Imm) {}
311218893Sdim      ResultOperand(Record *r) : R(r), Kind(K_Reg) {}
312218893Sdim
313218893Sdim      bool isRecord() const { return Kind == K_Record; }
314218893Sdim      bool isImm() const { return Kind == K_Imm; }
315218893Sdim      bool isReg() const { return Kind == K_Reg; }
316221345Sdim
317218893Sdim      StringRef getName() const { assert(isRecord()); return Name; }
318218893Sdim      Record *getRecord() const { assert(isRecord()); return R; }
319218893Sdim      int64_t getImm() const { assert(isImm()); return Imm; }
320218893Sdim      Record *getRegister() const { assert(isReg()); return R; }
321218893Sdim    };
322221345Sdim
323218893Sdim    /// ResultOperands - The decoded operands for the result instruction.
324218893Sdim    std::vector<ResultOperand> ResultOperands;
325218893Sdim
326218893Sdim    /// ResultInstOperandIndex - For each operand, this vector holds a pair of
327218893Sdim    /// indices to identify the corresponding operand in the result
328218893Sdim    /// instruction.  The first index specifies the operand and the second
329218893Sdim    /// index specifies the suboperand.  If there are no suboperands or if all
330218893Sdim    /// of them are matched by the operand, the second value should be -1.
331218893Sdim    std::vector<std::pair<unsigned, int> > ResultInstOperandIndex;
332221345Sdim
333218893Sdim    CodeGenInstAlias(Record *R, CodeGenTarget &T);
334218893Sdim
335218893Sdim    bool tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
336243830Sdim                         Record *InstOpRec, bool hasSubOps, ArrayRef<SMLoc> Loc,
337218893Sdim                         CodeGenTarget &T, ResultOperand &ResOp);
338221345Sdim  };
339193323Sed}
340193323Sed
341193323Sed#endif
342